If there's not parent with position set to relative , and you set the position to absolute , that will affect your element to get an position from x(0) and y(0) (left top screen corner) , if you set for example 2 divs , the first one is with position:relative
, and his child is with position:absolute; ,, that child item will be affected from x(0), y(0) from its parent position.
<div style="position:absolute;">One</div> <-- this one will be on the left top of the screen ,
<div style="position:relative;">
<div style="position:absolute;">Here you go boy</div>
</div>
(this children element gets the position of its parent element , not of the top-left screen)
In your case, you have set to both elements ( div and span ) position:absolute
, if there's no parent element with position:relative;
they automatically goes on the top left corner and gets left(0) top(0). If you want your span element to inherit div's position , first you have set position:relative;
to your div and position:absolute
; , to your span , and after that you have to adjust your left and top pixels , in depends on where your element wants to be positioned , there's a simple code below
=================================================
FIRST CASE : NO INHERITANCE OF POSITIONING!
HTML
<div>
<span>Test child</span>
</div>
CSS
div{
height:100px;
width:100px;
border:2px solid black;
margin:0 auto;
}
span{
position:absolute;
left:0px;
top:0px;
}
In this code , I've positioned your div in the middle , by (margin:0 auto)
.I've set position:absolute
to your span and left:0 , top:0
, (there's no inheritance , because div doesn't contain position:relative;
).
=============================
SECOND CASE - INHERITANCE
CSS:
div{
height:100px;
width:100px;
border:2px solid black;
margin:0 auto;
position:relative; /* from since now , your span will be affected of div's position */
}
span{
position:absolute;
left:0px;
top:0px;
}
Now your span is affected by div's position and will be placed on the x(0) and y(0) corner in your div , not screen. I hope you understand this.