Home Web Front-end CSS Tutorial Learn position layout: from static to relative, absolute and fixed

Learn position layout: from static to relative, absolute and fixed

Dec 26, 2023 am 09:13 AM
static absolute fixed. position layout relatively

Learn position layout: from static to relative, absolute and fixed

Understand position layout: from static to relative, absolute and fixed, specific code examples are required

In web development, layout is a very important part. The position property of CSS controls the layout of elements. This article will introduce the four types of position layout: static, relative, absolute and fixed, and explain its usage and effects with specific code examples.

  1. Static positioning (static):
    Static positioning is the default positioning method of elements. At this time, elements are arranged according to the flow of the document and will not be affected by other positioning methods. Staticly positioned elements cannot be positioned through attributes such as top, bottom, left, and right, because they will not have any impact on the element. The following is a sample code for static positioning:
.container {
  width: 200px;
  height: 200px;
  background-color: #f2f2f2;
}
Copy after login
<div class="container">
  静态定位元素
</div>
Copy after login
  1. Relative positioning (relative):
    Relative positioning is positioned relative to the original position of the element in the document flow. By setting the top, bottom, left, and right attributes, you can specify the offset of the element relative to its original position. Relative positioning does not affect other elements, so other elements will not change position due to relative positioning. The following is a sample code for relative positioning:
.container {
  width: 200px;
  height: 200px;
  background-color: #f2f2f2;
}

.box {
  position: relative;
  top: 20px;
  left: 30px;
  background-color: #ff0000;
}
Copy after login
<div class="container">
  <div class="box">
    相对定位元素
  </div>
</div>
Copy after login
  1. Absolute positioning (absolute):
    Absolute positioning is relative to the nearest positioned ancestor element (the value of the position attribute is not static ) for positioning. If no positioned ancestor element exists, an absolutely positioned element is positioned relative to the entire page. By setting the top, bottom, left, and right attributes, you can specify the offset of the element relative to the reference element. Absolute positioning will affect the position of other elements, and other elements will be rearranged to adapt to the change of the positioned element. The following is a sample code for absolute positioning:
.container {
  width: 200px;
  height: 200px;
  background-color: #f2f2f2;
  position: relative;
}

.box {
  position: absolute;
  top: 20px;
  left: 30px;
  background-color: #ff0000;
}
Copy after login
<div class="container">
  <div class="box">
    绝对定位元素
  </div>
</div>
Copy after login
  1. Fixed positioning (fixed):
    Fixed positioning is positioned relative to the browser window. By setting the top, bottom, left, and right attributes, you can specify the offset of the element relative to the browser window. Fixed positioning does not change position as the page scrolls, so it can be used to create floating elements such as navigation bars or ads. The following is a sample code for fixed positioning:
.container {
  width: 200px;
  height: 2000px;
  background-color: #f2f2f2;
}

.box {
  position: fixed;
  top: 20px;
  left: 30px;
  background-color: #ff0000;
}
Copy after login
<div class="container">
  <div class="box">
    固定定位元素
  </div>
</div>
Copy after login

Through the above code examples, we can clearly understand the differences and usage of the four position layouts. Static positioning is the default, and elements are arranged according to the flow of the document. Relative positioning allows you to position relative to the original position by specifying an offset. Absolute positioning will affect the position of other elements and requires reference to positioned ancestor elements. Fixed positioning can be positioned relative to the browser window and can be used to create floating elements.

Mastering the different methods of position layout can help us better control the position and layout of elements, thereby improving the design and user experience of web pages.

The above is the detailed content of Learn position layout: from static to relative, absolute and fixed. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Share solutions to fix win11 taskbar Share solutions to fix win11 taskbar Jan 04, 2024 am 08:07 AM

We can generally fix the taskbar by right-clicking the icon in the taskbar, but some users said it cannot be pinned. We may not know how to solve it. In fact, we can put the shortcut into the taskbar folder to pin it to the taskbar. Let's follow the editor to do it. What to do if the win11 taskbar cannot be pinned: 1. First, we double-click to enter "This PC" 2. Then locate the following location "C:\Users\Administrator\AppData\Roaming\Microsoft\Internet Explorer\QuickLaunch\UserPinned\TaskBar" 3. Then Pin our needs to

In-depth analysis of the role and usage of the static keyword in C language In-depth analysis of the role and usage of the static keyword in C language Feb 20, 2024 pm 04:30 PM

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

The role and application scenarios of private static methods in PHP The role and application scenarios of private static methods in PHP Mar 23, 2024 am 10:18 AM

The role and application scenarios of private static methods in PHP In PHP programming, a private static method is a special method type. It can only be accessed within the class in which it is defined and cannot be directly called from the outside. Private static methods are usually used for the internal logic implementation of a class, providing a way to encapsulate and hide details. At the same time, they have the characteristics of static methods and can be called without instantiating the class object. The following will discuss the role and application scenarios of private static methods, and provide specific code examples. Function: encapsulate and hide implementation details: private static

Quickly master the techniques and methods of static relative positioning Quickly master the techniques and methods of static relative positioning Jan 18, 2024 am 11:18 AM

Fast static relative positioning is a very important positioning method in web development. It allows an element to be slightly adjusted relative to its normal position while still maintaining its position in the document flow. In this article, I will introduce in detail the use of fast static relative positioning, as well as some common application scenarios. First, we need to understand the basic concepts of fast static relative positioning. In CSS, there are four ways to position elements: static positioning, relative positioning, absolute positioning and fixed positioning. Static positioning is the default positioning method. The position of the element is determined by the document.

Master the skills and precautions of position layout: the practice of implementing responsive layout Master the skills and precautions of position layout: the practice of implementing responsive layout Dec 26, 2023 pm 12:44 PM

Implementing responsive layout: Overview of practices and considerations for position layout: Responsive layout refers to the layout of web content that automatically adjusts to the user's device screen size and resolution. In responsive layout, position layout is a commonly used method, which can help us achieve element positioning and layout under different screen sizes. 1. The basic principle of position layout. Position layout is based on CSS positioning attributes, including static, relative, and absolute.

The principle of static relocation technology and its application cases The principle of static relocation technology and its application cases Jan 18, 2024 am 11:12 AM

Principles and Applications of Static Relocation Technology Introduction: In modern computer systems, memory management is a very important topic. As the complexity and size of software increases, memory constraints become a challenge. In order to utilize memory resources more efficiently, static relocation technology came into being. This article will introduce the principles and applications of static relocation technology and provide some specific code examples. 1. Principle of static relocation technology Static relocation is a method of moving program code and data from one logical address space to another logical address space.

Optimizing page layout methods: application skills of fast static relative positioning Optimizing page layout methods: application skills of fast static relative positioning Jan 18, 2024 am 10:39 AM

How to use fast static relative positioning to optimize page layout. With the development of the Internet, web design has become more and more important. A good page layout improves user experience and improves the usability and accessibility of your website. Fast static relative positioning is a commonly used layout technique that can effectively optimize page layout. This article will introduce how to use fast static relative positioning to optimize page layout. Fast static relative positioning is a layout technology based on CSS. By using the "position" attribute in the CSS style sheet, it can be used without affecting the text.

Detailed explanation of Go language features: Learn about Go's language features in one article Detailed explanation of Go language features: Learn about Go's language features in one article Mar 05, 2024 am 09:54 AM

Detailed explanation of Go language features: Learn about the language features of Go in one article. The Go language is an open source programming language developed by Google that is statically typed, compiled, concurrent, and has garbage collection capabilities. Since its release in 2009, the Go language has been loved by programmers for its efficient concurrency processing capabilities, concise syntax, and fast compilation speed. This article will introduce some of the main features of the Go language in detail, and help readers better understand these features through specific code examples. One of the biggest features of concurrent programming Go language is its native

See all articles