I don’t understand the position:absoulte attribute well, I hope someone can provide an answer, thank you,
Here I set position:absoulte for child p, and as a result, the space occupied by his parent p disappeared
<p style="width: 40%; background-color: #aa0000; float: left">
<p >
<p style="position: absolute">
111
</p>
<p style="position: absolute">
222
</p>
</p>
</p>
<p style="width: 20%; background-color: #00aa00; float: left">222</p>
<p style="width: 40%; background-color: #0000aa; float: left">333</p>
<p style="width: 40%;height: 100px; background-color: #aa0000; float: left">
<p >
<p style="position: absolute">
111
</p>
<p style="position: absolute">
222
</p>
</p>
</p>
<p style="width: 20%; background-color: #00aa00; float: left">222</p>
<p style="width: 40%; background-color: #0000aa; float: left">333</p>
Because when the element is set to absolute, it is already out of the document flow. It does not take up space inside the parent element
In your first example, the parent element has no height set, and the child elements also have no height, so they are not displayed.
It is absolute positioning, separated from the document flow, and you have not set top/right/bottom/left and other values, so the two p’s at the same level will overlap
Four positioning methods of CSS:
static
: Default positioning (i.e. no positioning, positioning depends on how the document flow is arranged)relative
: relative positioning, relative to the original position, the so-called original position, that is, the position ofstatic
positioningabsolute
: Absolute positioning, positioned relative to the first parent element other thanstatic
positioning. Starting from the current element, the positioned element is searched upwards until the root element. Regardless of whether the first parent element encountered isrelative
orabsolute
, or afixed
positioned element, the current element will be positioned relative to that element. And this parent element is not necessarily the first-level parent element of the current element. If no non-static
parent element is found, it is positioned relative to the root elementhtml
.fixed
: Fixed positioning, positioned relative to the browser windowBeginners just need to remember the above.
According to w3school:
In the quote, I think the exact statement should be "relative to the nearest positioned containing block or the initial containing block", because if the statement is "relative to the containing block", then why must it be at least an element
relative
. (Should check w3c...)What is a containment block?
containing block
css Containing Box
Containing block is a virtual rectangular area used by the browser to calculate the position of elements. The starting position for calculating element positioning is the upper left corner of the rectangular area, which is the origin, and the coordinate position is ( 0,0), the
top
andleft
of the positioned element are determined relative to the origin. The containing block is the frame of reference for element positioning.You can think of this rectangular area as the element that created it, but it is not this element, it is just a virtual thing.
The containing block is just used to calculate the position and size of the element.
Why does the space occupied by the parent element disappear?
Because the elements set to
position: absolute
are separated from the document flow (normal flow) and form an independent BFC.Each BFC in the page is an independent rendering area and does not affect each other.
But its position information is still determined by its containing block in normal flow.The so-called flow is that the browser layouts and renders elements one by one from top to bottom and from left to right in the browser viewport, forming a concept similar to water flow.
By default, a page has only one "flow", which is the document flow (normal flow). If the page has positioned elements and floating elements, a positioned flow and a floating flow will be formed, and the normal flow is formed by the root element of the document
html
.You can think of flows as pieces of paper stacked on the desktop. Each piece of paper is a "flow", but these papers are not necessarily the same size.
My understanding of BFC - wmsj100
1. The prime parent element does not have a fixed width and height.
2. When the child element floats, it jumps out of the document flow and cannot open the parent element, so the parent element disappears.
Here you mix absolute positioning and floating, which is not easy for beginners to understand. The two should be separated for easier understanding.
When using absolute positioning, please note:
For the relevant understanding of floating float, please refer to Baidu separately~
If you set a width and height for the parent, the space will still be there