What should the parent element do when the child element is absolutely positioned?

Source: Internet
Author: User

When I did the exercises yesterday, I encountered this problem:

if the child element is set to absolute positioning (absolute), the parent element will need to set relative positioning (relative), otherwise the child element will not know where to float.

But always think still did not understand, went to Baidu, got some views, the following:

First, the table

The following two conditions must be met to achieve absolute positioning of child elements in the parent element:

1, the parent element to have the relative positioning attribute (position:relative),

2, the child element sets the absolute positioning (Position:absolute), and simultaneously adds four directions (top,bottom,left,right) The attribute value in any direction

<!DOCTYPE HTML><HTML><Head><MetaCharSet=utf-8 "/><title></title><styletype= "Text/css">#main{width:500px;Height:600px;Border:1px solid Black;position:relative;background:Pink;}#one, #two{width:100px;Height:100px;}#one{background:Blue;position:Absolute;Top:100px; Left:100px;}#two{background:Yellow;position:Absolute;Top:200px; Left:200px;}</style></Head><Body><DivID= "Main"><DivID= "One">1</Div><DivID= "both">2</Div></Div></Body></HTML>

This understanding should be summarized from the writing form of the code, although it can meet the requirements of the page, but in fact there is a fallacy. See below for more detailed analysis.

Two, Li

The absolute positioning of the child elements is the knowledge that contains the block "containing blocks",

in Verse 10.1 (http://www. W3.org/tr/css2/visudet. html#containing-block-details) Mentioned:
the position and size of an element ' s box (es) is sometimescalculated relative to a certain rectangle, called the C Ontainingblock of the element. In the detailed definition below, it is explained in detail how elements with different position attributes determine containing block, where the 4th is the case of absolutely positioned elements:
If the element has ' Position:absolute ', the containing block isestablished by thenearest ancestor with a ' position ' of ' absolute ', ' relative ' or ' fixed ', in the following:
    1. In the case, the ancestor was an inline element, the containingblock is the bounding box around the padding boxes of th E first andthe last inline boxesgenerated for that element. In CSS 2.1, if the inlineelement are splitacross multiple lines, the containing block is undefined.
    2. Otherwise, the containing Blockis formed by the padding edge ofthe ancestor.
If There is no such ancestor, the containing block is theinitialcontaining block.

as you can see from here, the containing block of an element is determined by its closest position to the ancestral elements of absolute/relative/fixed.
If not found, initial containing block. It is important to note that initial containing block is not a so-called <body> or

The initial containing block specification is described in the previous article:
The containing block in which the root element lives is a rectangle called the initial containing block. For Continuousmedia,It has the dimensions of the viewport and was anchored at Thecanvas origin; ...

This means that the initial containing block is the entire canvas (the space for rendering content,http://www. w3.org/tr/css2/intro.html#canvas The coordinate origin (upper left) of the viewport is the size of the rectangle (that is, the space in the browser window that renders the HTML).
so the position:absolute element that is usually said to be relative to the nearest position is the ancestor element of Absolute/relative/fixed, or is located based on the root element when it is not found, and half of it is wrong.   In summary , the absolute positioning of the child element is relative to the nearest position as the absolute/relative/fixed ancestor element, and is not found to be relative to the initial block (initial containing block) location.

What should the parent element do when the child element is absolutely positioned?

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.