Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position absolutely inside float:left?

Why adding of left rule changes behavior so drastically? Is it possible to position relative to default position?

http://jsfiddle.net/suzancioc/drDn3/6/

HTML:

    <div class='level0'>
   <div class='level1'>
       Hello

   </div>
   <div class='level1'>
       Hello
       <div id='inner2'>inner2</div>
   </div>
       <div class='level1'>
       Hello
       <div id='inner3'>inner3</div>
   </div>

</div>

CSS:

.level0 {
   height:40px;
   width: 500px;
   background:red;
}
.level1 {
   float:left;
   margin:2px;
   border-style: solid;
   background: cyan;

}
#inner1 {
   position: absolute;
   background: green;
}

#inner2 {
   position: absolute;
   background: green;
    left:0px;
}

#inner3 {
   position: absolute;
   background: green;
}
like image 628
Suzan Cioc Avatar asked Nov 23 '12 20:11

Suzan Cioc


People also ask

Does float work with position absolute?

Float right and position absolute doesn't work together.

How do you position an absolute element?

Absolute An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element. If it doesn't have any parent elements, then the initial document <html> will be its parent.

How do you move a position with an absolute element?

Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

How do you set the position absolute inside a div?

position : absolute Inside Other Positioned ElementsThe inner div element has position set to absolute and top to 0px and right to 0px . This places the inner div at the top right corner inside its parent element (because the parent element has position: relative set).


1 Answers

In order to position absolute something you need to assign that div(in your case) to a relative positioned parent

.level1 {
   float:left;
   margin:2px;
   border-style: solid;
   background: cyan;
   position:relative;

}

Adding position:relative makes .level1 a sort of coordinate system for all elements inside of it.

Take a look at this JSFIDDLE

like image 97
Ionel Lupu Avatar answered Oct 05 '22 10:10

Ionel Lupu