


What is the difference between css relative positioning and absolute positioning? Comparative analysis of css relative positioning and absolute positioning
There are relative positioning and absolute positioning in css positioning. So what is the difference between relative positioning and absolute positioning in css? The following article will introduce to you the content of relative positioning and absolute positioning of css and the difference between relative positioning and absolute positioning of css.
1. First, let’s take a look at css relative positioning
position: relative (relative positioning): Relative positioning is relative to the initial position of the element in the document.
First it appears at its position (that is, the position when position is not set), and then by setting the vertical or horizontal position, the element is moved "relative to" its original starting point.
It should be noted that when using relative positioning, the element still occupies the original space regardless of whether it is moved. Therefore, moving the element will cause it to cover other boxes.
#box_relative { position: relative; left: 30px; top: 20px; }
2. Let’s take a look at css absolute positioning:
position: absolute (absolute positioning): Absolute positioning is relative to the element’s nearest positioned ancestor element (That is, an ancestor element with absolute positioning or relative positioning set). If the element does not have a positioned ancestor element, its position is relative to the original containing block (body).
#box_relative { position: absolute; left: 30px; top: 20px; }
Description: Absolute Positioning itself has nothing to do with the document flow, so it does not take up space. The layout of elements in the ordinary document flow is the same as when absolutely positioned elements do not exist, so they can cover other elements on the page and can be controlled through the z-index attribute. The order of each other of these layers.
3. Finally, let’s summarize the difference between CSS relative positioning and absolute positioning
position:absolute (Absolute positioning) float will implicitly change the display type (except display: none).
That is, when the element is set to any one of position:absolute, float:left, float:right, the element will be Displayed in display:inline-block mode (characteristics: the length and width can be set, and the default width does not occupy the parent element). At this time, even if display:inline is deliberately set;
display:block are all invalid.
But the double margin bug of float under IE6 is solved by using display:inline;.
position:relative (relative positioning) does not implicitly change the display type.
By default, relative positioning is positioned relative to the position of the element itself; absolute positioning is positioned relative to the coordinate origin of the upper left corner.
This article ends here. If you want to know more about positioning, please refer to css manual.
Related recommendations:
Detailed explanation of relative positioning and absolute positioning of CSS
CSS positioning: relative positioning, Absolute positioning and fixed positioning
The above is the detailed content of What is the difference between css relative positioning and absolute positioning? Comparative analysis of css relative positioning and absolute positioning. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor
