Why aren’t my absolutely/fixed-positioned elements located where I expect?

To correctly understand this, you need to refer to the official specification where you find the equation the element must satisfy:

'top' + 'margin-top' + 'border-top-width' + 'padding-top' + 'height' + 'padding-bottom' + 'border-bottom-width' + 'margin-bottom' + 'bottom' = height of containing block

We don’t have any border and padding so in your case it will simply be:

'top' + 'margin-top' + 'height' + 'margin-bottom' + 'bottom' = height of containing block

And if you read under you will find this:

  1. ‘top’ and ‘bottom’ are ‘auto’ and ‘height’ is not ‘auto’, then set ‘top’ to the static position, set ‘auto’ values for ‘margin-top’ and ‘margin-bottom’ to 0, and solve for ‘bottom’.

So in your case, you have set a height and kept top/bottom to auto thus top will be set to static position

..More precisely, the static position for ‘top’ is the distance from the top edge of the containing block to the top margin edge of a hypothetical box that would have been the first box of the element if its specified ‘position’ value had been ‘static’..

To make it easy, it’s the position of the element if you didn’t set position:absolute.

Here is an easy illustration to better understand

.container {
  background: lightblue;
  position: relative;
  padding:40px 20px;
  display:inline-block;
  vertical-align:top;
  width: 250px;
}


.box-grey {
  background: grey;
  height: 100px;
  width: 100px;
  position: absolute;
}

.box-green {
  height:20px;
  background:green;
}
<div class="container">
  <div class="box-green"></div>
  <div class="box-grey" style="position:static;">I am static</div>
</div>

<div class="container">
  <div class="box-green"></div>
  <div class="box-grey">I am absolute</div>
</div>

Note the static position of the first element that is kept if we add position:absolute. We didn’t specify any top value so the browser will use a default one that is the one of the position:static (default position) of the element.

If you don’t want this, simply set the top value and you fall into this rule:

  1. ‘bottom’ is ‘auto’, ‘top’ and ‘height’ are not ‘auto’, then set ‘auto’ values for ‘margin-top’ and ‘margin-bottom’ to 0 and solve for ‘bottom’
.container {
  background: lightblue;
  position: relative;
  padding:40px 20px;
  display:inline-block;
  vertical-align:top;
  width: 250px;
}


.box-grey {
  background: grey;
  height: 100px;
  width: 100px;
  position: absolute;
  top:0;
}

.box-green {
  height:20px;
  background:green;
}
<div class="container">
  <div class="box-green"></div>
  <div class="box-grey" style="position:static;">I am static</div>
</div>

<div class="container">
  <div class="box-green"></div>
  <div class="box-grey"></div>
</div>

Same logic apply to the left property


You may also notice the use of the word containing block which very important here and explained in the same specification

The position and size of an element’s box(es) are sometimes calculated relative to a certain rectangle, called the containing block of the element. The containing block of an element is defined as follows:

  1. If the element has ‘position: absolute’, the containing block is established by the nearest ancestor with a ‘position’ of ‘absolute’, ‘relative’ or ‘fixed’, in the following way:

And it’s not enough as there is other properties (listed below) that also establish a containing block so you can have an element positionned relatively to an ancestor that is not positionned!

Related: Why does applying a CSS-Filter on the parent break the child positioning?

Leave a Comment