search
HomeWeb Front-endHTML TutorialDetailed explanation of relative positioning, absolute positioning and fixed positioning

The position of the element in the document flow is determined by the position of the element in (X)HTML. This is the most primitive ordinary flow. The float mentioned earlierCSS Study Notes 08 FloatYou can change the position of the element in the document flow. In addition to this, we can also re-determine the position of the element in the document flow by using the position property of CSS.

position attribute value

  • static: The default document flow layout method. Block-level elements generate a rectangular box as part of the document flow. Inline elements will Creates one or more line boxes, placed within their parent element. (Ignore top, bottom, left, right or z-index declarations).

  • relative: Offset relative to the original position. The completion process is to first generate an element in static (float) mode. Relative to the previous position, the movement direction and amplitude are determined by left , right, top, bottom attributes are determined, the space it originally occupied is still retained.

  • absolute: The element box is completely deleted from the document flow, and the space originally occupied by the element in the normal document flow will be closed,As if the element does not exist originally, positioning is performed according to the parent container (must be a non-static positioned container). The element generates a block-level box after positioning, regardless of what type of box it originally generated in the normal flow.

  • fixed: Fixed at a certain location in the browser and will not change when the browser scrolls.

static is the default layout method and will not be introduced here.

relative Relative positioning

 1 nbsp;html> 2  3  4     <meta> 5     <title>CSS相对定位</title> 6  7     <style> 8         div {height: 100px; width: 100px; border: 1px solid #000; background-color: yellow;} 9     </style>10 11 12     <div>box1</div>13     <div>box2</div>14     <div>box3</div>15 16 

At this time, box1 and box2 are positioned on the page according to the static layout method. Now box2 Relative positioning

#The result is as follows, the position of box2 is offset (this offset is relative to the original position of box2), and does not affect box1 Position with box3

Note that when using relative positioning, the element still occupies the original space regardless of whether it is moved or not. Therefore, moving an element causes it to cover other boxes.

absolute Absolute positioning

Absolute positioning makes the position of the element independent of the document flow, so it does not occupy space. This is different from relative positioning, which is actually considered part of the normal flow positioning model because the element's position is relative to its position in the normal flow.

 1 nbsp;html> 2  3  4     <meta> 5     <title>CSS绝对定位</title> 6  7     <style> 8         .parent {height: 200px; width: 200px; background-color: yellow;} 9         .child {height: 100px; width: 100px; background-color: red; top: 0px; right: 0px; position: absolute;}10     </style>11 12 13     <div>14         parent15         <div>16             child17         </div>18     </div>19 20 

Now also add a positioning attribute to parent

The effect is as follows, you can see The position of the child has changed

From the above phenomenon, one thing can be summarized: the position of an absolutely positioned element is relative to the nearest positioned ancestor If the element has no positioned ancestors, its position is relative to the original containing block (in this case, the body element and parent).

To use absolute positioning, there must be two conditions
 1. The parent must be To add positioning attributes to an element, it is generally recommended to use position:relative
2. Add absolute positioning to the child element position:absolute, and also add direction attributes (referring to left, right, top , bottom attribute)

fixed fixed positioning

is similar to the absolute positioning type, but its relative movement coordinates are the view (web page window within the screen) itself. Since the view itself is fixed, it will not change as the scroll bar of the browser window scrolls, unless you move the screen position of the browser window on the screen, or change the display size of the browser window, so fixedly positioned elements will always be in A position within the view within the browser window that is not affected by the flow of the document.

 1 nbsp;html> 2  3  4     <meta> 5     <title>CSS固定定位</title> 6     <style> 7         .back-top {height: 40px; width: 40px; background-color: blue; color: #fff; position: fixed; bottom: 10px;right: 10px;} 8     </style> 9 10 11     <div>回到顶部</div>    12     <p>段落1</p>13     <p>段落2</p>14     <p>段落3</p>15     ...16     <p>段落49</p>17     <p>段落50</p>18 19 

The effect is as follows. The div back to the top is always in the same position, and the on the blog page is also fixed positioning.

Summary

Absolute positioning is based on the parent element as the reference point. It will break away from the document flow and does not occupy the original position space.
Relative positioning is based on itself as the reference point, leaving the original position, but it will still occupy the original position space
Fixed positioning is based on the browser window as the reference point, it is always in the same position and will not move

The above is the detailed content of Detailed explanation of relative positioning, absolute positioning and fixed positioning. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

What problems will you encounter when using native select on your phone?What problems will you encounter when using native select on your phone?Apr 30, 2025 pm 03:06 PM

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...

See all articles

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment