absolute positioning is a positioning model that occurs when a box has the absolute value as position property.
When this is the case, the absolute box is positioned:
Absolutely positioned elements are not in (removed from) the normal flow, and thus their dimensions does not alter the dimensions of their parents.
When a box has position: absolute its containing box is the parent's padding box. (the box around its padding).
It means that the width of the absolute positioned element is at maximum: the width of the containing box minus the padding of the containing box.
This example shows a slideshow where we position the next and previous button absolutely over a slide
<div id="component">
<div id="main">
<div>A slide</div>
</div>
<div id="controls">
<button class="left">Left</button>
<button class="right">right</button>
</div>
</div>
#component {
position: relative;
}
#main { /* The main box is the driver that calculates the dimension */
width:100%;
height:10rem;
display: grid;
place-items: center;
background-color: skyblue;
}
#controls {
box-sizing: inherit; /* same width and height than the parent box */
}
.left {
position: absolute;
left: 2rem;
top: 50%;
}
.right {
position: absolute;
right: 2rem;
top: 50%;
}
An absolute box will be positioned in two cases:
When a box has the value absolute, its position is calculated with the offset properties (left, right, bottom, top) from the first ascendant container box with a position value of relative, absolute or fixed (This box is called the containing block)
Example: Bottom Right of a relative container
.box_ancestor_relative {
position:relative; /* from static to relative */
width: 600px; height:300px; /* a size */
border: 2px solid steelblue; /* a border to see the box */
padding: 1rem /* padding to have the text not touching the border */
}
.box_descendant_absolute {
position:absolute;
border: 2px solid red; /* A border to se the box */
margin: 0px; /* margin to 0 to have the border touching the ascendant border */
padding: 1rem; /* padding to have the text not touching the border */
bottom:0; right:0; /* bottom right position of the first relative ascendant box */
width: 200px /* we restrict the width to see the bottom right (otherwise the box takes the whole width due to the content ) */
}
<div class="box_ancestor_relative">
Container Box with a Relative position
<div style="border:2px dotted steelblue;padding:1rem">
<p>This div has no effect on its descendant absolute box because it has a position value of static (the default)<p>
<p class="box_descendant_absolute">Descendant Box with a absolute position from the first relative ancestor box - Bottom Right</p>
</div>
</div>
When the box with a absolute position does not have any ascendant box with a a relative / absolute or fixed position (ie they are only static), the containing block is the viewport (ie from where the offset properties (left, right, bottom, top) applies)
Example: Bottom Right of the viewport
body {
/* position: static; default and therefore not needed */
border: 2px solid steelblue; /* To visualize the box */
padding: 1rem; /* ti have the text not touching the border */
height: 300px
}
.absolute_box {
position:absolute;
border: 2px solid red;
margin: 0px;
bottom:0;
right:0;
}
<p>A text in a body bigger than the viewport (this iframe has a height of 150px and the body is 300). <p>
<p>It helps to see the effect on the absolute box if you scroll down. If you scroll down, the absolute box will also move, if you don't want this behavior, use the ''fixed'' position.</p>
<p class="absolute_box">Absolute Box - Bottom Right of the viewport</p>
</div>