Reputation: 343
Here is my scenario. I have a container div that has (n) child elements inside it. For this instance lets say there are 2 divs within the container div:
<div id="container">
<div id="col1">
Stuff in col1
</div>
<div id="col2">
Stuff in col2
</div>
</div>
The container div is going to be a percentage of the viewport, say 80%. Now, what I'm looking for is for these two inner divs (col1 & col2) to be inline with each other and take up the same amount of space. So the result should look something like this:
+-------------- container -------------+
| +---- col1 ----+ +---- col2 ----+ |
| | stuff in | | stuff in | |
| | col1 | | col2 | |
| +--------------+ +--------------+ |
+--------------------------------------+
Or if the container width is changed should result in something like this:
+------------------------------ container -----------------------------+
| +------------ col1 ------------+ +------------ col2 ------------+ |
| | stuff in col1 | | stuff in col2 | |
| | stuff in col1 | | stuff in col2 | |
| +------------------------------+ +------------------------------+ |
+----------------------------------------------------------------------+
The inner divs are always of equal width and have some separation from each other. This is similar to a table layout, but I would rather not use tables if possible. I have tried various techniques like floating and displaying the divs inline to no avail. They can never seem to align just right.
Upvotes: 0
Views: 1518
Reputation: 1663
Maybe the use of display: table;
would help? http://jsfiddle.net/g4dGz/119/
Upvotes: 0
Reputation: 9511
#container{
overflow: hidden
}
#col1, #col2 {
float: left;
width: 50%;
}
Upvotes: 0
Reputation: 18816
Use positioning relative to the outer container:
#container, #container > div
{
position: relative;
}
#col1
{
left: 2%; /* your margin */
}
#col2
{
right: 2%;
}
#container > div
{
width: 47%;
}
Note that you leave approximately the same 2% in the middle. The #col1
and #col2
should be aligned now.
With CSS3: use column-count: 2
and break column after first div
.
If you really feel like floating, do only #col1 { float: left; width: 50%; }
Upvotes: 0
Reputation: 1364
Table cells could stretch automatically. It's not exactly possible with div
, so you have to specify appropriate width for each column by hand. For example:
#col1, #col2 {
float: left;
width: 50%;
}
Upvotes: 4
Reputation: 57167
make col1 and col2 spans (not divs) with
vertical-align:top
display:inline-block
width:50%
obviously (adjust the width to account for your margins/padding. and recommended that you use percentages for margins/padding so they add up to just under 100% see:http://ejohn.org/blog/sub-pixel-problems-in-css/)
Upvotes: 0