-1

I have a web page with an equivalent of the following html, and the corresponding CSS:

<div class="father">
    <div class="divson"></div>
    <a class="ason"></a>
</div>

.father     {position:relative}
.father div {position:absolute}
.father a   {position:relative}

Because 'divson' has position:absolute, I'd expect it to be always placed relatively to 'father' (because this one is positioned as well). Although, 'divson' still moves elsewhere when I reorder the html as follows:

<div class="father">
    <a class="ason"></a>
    <div class="divson"></div>
</div>

How is it possible that a div with position:absolute still depends on the html order? What could be a cause that I wouldn't have thought of?

Thanks

Telergoel
  • 353
  • 1
  • 13

3 Answers3

3

You have not set the left, top, bottom, right property for the divson. You have to set these to fix your div position, if you are using absolute positioning.

.divson{
 position:absolute;
 left:0;
 top:0
}
Abhay Srivastav
  • 754
  • 6
  • 16
1

As w3schools says:

position: absolute;

The element is positioned relative to its first positioned (not static) ancestor element.

0

Display: flex and play with items order (order:1; order:2;)

Erick Boileau
  • 478
  • 4
  • 14