CSS Position: Relative and Absolute Positioning
Category Programming Technology
I. The Four Values of Position: static, relative, absolute, fixed.
Absolute Positioning: absolute and fixed are collectively referred to as absolute positioning.
Relative Positioning: relative
Default Value: static
II. Differences Between Relative and Absolute Positioning
Example:
HTML Code:
(HTML code not provided)
CSS Code:
(CSS code not provided)
Initial Effect:
(Initial effect not described)
1. Relative: Moves relative to its original position. The element remains in the document flow and does not affect the layout of other elements.
Setting relative on the second box:
The element shifts relative to its original position, maintaining its width and height, and enlarging the container.
2. Absolute: The element is removed from the document flow. Setting an offset affects the positioning of other elements.
Setting absolute on the fifth box only:
Note: If the width is not defined, the width is determined by the content inside the element, similar to the effect of using the float method.
Analysis of Absolute Positioning Principles:
1. If the parent element does not have relative or absolute positioning, the element is positioned relative to the root element (i.e., the html element).
Now, let's give box5 an offset value to verify:
2. If the parent element has relative or absolute positioning, the element is positioned relative to the nearest parent element that has relative or absolute positioning (or the nearest parent element that is not static, as the default is static).
Note: Some online explanations state that the element is positioned relative to the first non-static parent element, which can be misleading. The above is my own definition.
Now, let's give the body element an absolute position (the body element is set to absolute, and the width of the entire container is determined by the longest div, making it narrower):
Now, box5 is positioned relative to the body element.
I've placed the two images of positioning relative to the html element and the body element together for comparison. The image below shows that box5, positioned relative to the body element, is farther from box1. Therefore, without a parent element having relative or absolute positioning, an element with absolute positioning is positioned relative to the html root element.
CSS Code:
(CSS code not provided)
Let's verify this statement: If the parent element has relative or absolute positioning, the element is positioned relative to the nearest parent element that has relative or absolute positioning.
Now, let's nest the boxes in three parent containers, as follows:
HTML Code:
(HTML code not provided)
The CSS styles for the three parent containers are as follows:
(CSS code not provided)
Now, box5 is not positioned relative to the first non-static element (according to this statement, box5 should be offset relative to the outermost container1), but rather relative to the nearest parent element that has relative or absolute positioning:
Now, let's comment out the position of the second and third containers (without position, setting top, left, bottom, right values has no effect), resulting in:
Now, the only parent element of box5 with relative or absolute positioning is the outermost container1, so box5 is now positioned relative to the outermost container1. (Clearly, box5 has moved)
Now, let's only comment out the position of the third container
The principle is the same; now, it is positioned relative to the second container (top: 50px, the nearest parent element with relative or absolute positioning):
Now, let's change the second and third containers to absolute positioning:
CSS Code:
(CSS code not provided)
The principle is the same as setting the second and third containers to relative, except that after setting them to absolute, the third container, along with its content, is removed from the document flow, so it does not expand the width of the outer two containers.
The effect now:
Adding another outer container to verify the effects of the first and second containers not being expanded:
The width is limited by the width of the parent container above. Now, if we widen the width of the first container, the second and third containers will widen accordingly, as shown below:
However, if there is a long div inside the container, the container can still be expanded, as shown below:
Supplement:
After setting box2 to absolute positioning, it is removed from the document flow, and box3 moves up, being covered by box2 on the left.
On the basis above, setting box3 to absolute, box3 is removed from the document flow (as if box3 floats above box2), box4 moves up, and box3 covers box2 and part of box4.
Similarly, if box4 is set to absolute positioning, it will float above box3 and box2.
Summary
Relative: Positioning is relative to its own position (when setting an offset, it offsets relative to its own position). An element with relative positioning remains in the document flow, and its width and height remain unchanged; setting an offset does not affect the position of other elements. If the outermost container is set to relative positioning without a defined width, its width is the width of the entire browser.
Absolute: Positioning is determined relative to the nearest parent element that has absolute or relative positioning. If no parent element has absolute or relative positioning, the element is positioned relative to the root element, which is the html element. An element with absolute positioning is removed from the document flow; if the width is not set, the width is determined by the content inside the element. After being removed, the original position is effectively empty, and the elements below will occupy that position.
Original Link: http://www.cnblogs.com/yy95/p/5672440.html