CSS position: absolute and relative

Source: Internet
Author: User

Explanation on css2.0 Handbook:

Setting this attribute value to absolute will drag the object out of the normal Document Stream and definitely locate it without considering the layout of its surrounding content. If other objects with different Z-index attributes occupy a given position, they will not affect each other, but will stack at the same position. At this time, the object does not have an external patch (margin), but it still has an internal patch (padding) and a border (Border ).
To activate the absolute (absolute) Positioning of an object, you must specify at least one of the Left, right, top, and bottom attributes and set this attribute value to absolute. Otherwise, the property will use their default value auto, which will cause the object to follow the normal HTML layout rules and be presented immediately after the previous object.

The trbl attribute (top, right, bottom, left) is valid only when the position attribute is set.
When position: absolute is set
If the position attribute is not set for the parent level (unlimited), the current absolute uses the trbl attribute to locate the original point in the upper left corner of the browser.
If the position attribute is set for the parent level (unlimited), the current absolute is located based on the trbl attribute in the upper left corner of the parent level (nearest) as the original vertex.

When position: relative is set
Then, the original vertex is located based on the trbl attribute in the upper left corner of the parent (recent) content area (or offset relative to the last element of the located element in the parent content area ), if there is no parent level, the original vertex is in the upper left corner of the body. Relative positioning cannot be stacked. When relative positioning is used, elements still occupy the original space regardless of whether the elements are moved or not. Therefore, moving an element overwrites other boxes.

In general, when the webpage is centered, absolute is prone to errors, because the webpage always adapts automatically with the resolution, while absolute uses the browser's upper left corner as the original point, the location will not change due to resolution changes. Sometimes you also need to use Z-index to set the upper and lower relations of the container. The larger the value is, the higher the upper it is. The value range is a natural number. Of course, it should be noted that the parent-child relationship cannot be set with Z-index. The parent-child relationship must be set at the parent level.

Setting this property value to relative will keep the object in a normal HTML stream, but its position can be offset based on its previous object. The text or objects after the relative (relative) positioning objects occupy their own space and do not overwrite the natural space of the objects to be located. In contrast, the text or object after an absolute (absolute) Positioning object occupies its natural space before it is dragged away from the normal document stream. Placing an absolute (absolute) Positioning object outside the visible area will cause the scroll bar to appear. The scroll bar does not appear when the relative (relative) Positioning object is placed outside the visible area. In fact, the main problem of positioning is to remember the significance of each positioning. Relative positioning is "relative to" the initial position of the element in the document stream, and absolute positioning is "relative to" the most recently located ancestor element.

The following is a supplement:

Although I know the absolute position (absolute) and relative positioning (relative) of CSS, I have never done it myself!
I have been busy for a long time, and I have finished it! I also figured out some of these two attributes!

Summary:

First look at the following Layer Structure

<Body>

<Div id = posi>

<Div id = rel> This layer only applies position: relative; style </div>
<Div id = ABS> This layer only applies position: absolute; style </div>
<Div id = SSS> do not apply a style <div>

</Div>

</Body>

1. Absolute: No placeholder, relative: placeholder!

For example, the upper layer structure:

When a layer with the ID of rel is displayed, a row is occupied! The ABS layer behind it will only be displayed in the next line!
When the layer with id abs is displayed, it overlaps with the layer with id sss!

2. By default (excluding top and so on), absolute (absolute positioning) locates at the parent layer.
The above ID is the ABS layer. If the top layer is not used together, the position displayed will be the same as the parent-level posi layer (posi is in the lower left corner of the document, and it will also be in the lower left corner of the document)

3. When combined with top, bottom, right, left and other attributes, absolute (absolute positioning) uses the window as the positioning, and relative uses its own placeholder as the baseline for offset! As follows:

<Body>

<Div id = posi>

<Div id = SSS> do not apply a style <div>
<Div id = rel> application position: relative; bottom: 30px; style </div>
<Div id = ABS> This layer only applies position: absolute; bottom: 30px; style </div>

</Div>

</Body>

AboveCode:
Layers whose ID is rel are moved up and overlapped with the layer whose ID is SSS
The layer with the ID of ABS will be based on the window and move to the position 30 pixels away from the window!

4. When combining top, bottom, right, left, and other attributes, If you want absolute (absolute positioning) to use the parent layer as the baseline for locating, you can apply the absolute or relativ attribute to the parent layer! As follows:

<Body>

<Div id = posi style = "position: relative">

<Div id = rel> application position: relative; bottom: 30px; style </div>
<Div id = ABS> This layer only applies position: absolute; bottom: 30px; style </div>

</Div>

</Body>

The above code: the layer whose ID is posi can also use the absolute attribute!
The layer with the ID of rel is not affected. The layer is offset based on its own placeholder!

The layer with ID as ABS uses the bottom edge of the posi layer as the positioning baseline. If the height of the posi layer is smaller than 30px, the ABS layer may not be able to see it!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.