Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute divs next to eachother

Tags:

html

css

In my html page i have two divs inside a container div. The two inner divs have a 'position: aboslute'. Becuase they have to be placed at the bottom left corner of the container div.

This works great when the container div only has one inner div. But when i add a 2nd div, then the 2nd div is placed ontop of the first inner div. Which makes sense ofcourse. But now i'm trying to find a way to have them next to eachother instead of them overlapping eachother.

The inner divs are generated. So i can't manually add an ID to them. All they have is one class name.

Example:

<div id="container">
    <div class="icon">ICON1</div>
    <div class="icon">ICON2</div>
</div>
#container {
    position: relative;
    width: 200px;
    height: 200px;
    border: 1px solid red;
}

.icon {
    position: absolute;
    bottom: 0;
    left: 0;
    border: 1px solid green;
}

Anyone have any idea how to solve this?

like image 466
w00 Avatar asked Jul 13 '12 18:07

w00


People also ask

How do I align divs next to each other?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.

How do I put 4 divs next to each other?

you can use flex property for, Display:flex apply flex layout to the flex items or children of the container only. So, the container itself stays a block level element and thus takes up the entire width of the screen. ...


1 Answers

Use the absolute positioning on a wrapper element instead of each individual icon, then you can float or position the icons how you like within that container:

<div id="container">
    <div class="icon-wrapper">
      <div class="icon">ICON1</div>
      <div class="icon">ICON2</div>
    </div>
</div>
#container {
    position: relative;
    width: 200px;
    height: 200px;
    border: 1px solid red;
}
.icon {
    border: 1px solid green;
    float:left;
}
.icon-wrapper {
    position: absolute;
    bottom: 0;
    left: 0;
}

Demo: http://jsfiddle.net/sYGfq/3/

like image 79
Wesley Murch Avatar answered Sep 21 '22 13:09

Wesley Murch