11

One more time today i stumbled upon a problem i always have with css layouts. I'd like to have 5 divs in a horizonzontal row. Let's say for example their widths should be:

  • 1 : 60 px,
  • 2 : 30 %,
  • 3 : 40px,
  • 4 : *
  • 5 : 100px

where * stands for "fill up the remaining space". Back in the old days that's been the way we layouted width tables. Nowadays due to accesibility reasons html tables are banned for layouts. This is just an example. I'm searching for a general solution.

Does someone know a generator, a lightweight javascript solution (can be a jQuery plugin), a tutorial, a book, or a magician which can help me to solve this problem for now and forevermore?

Allthough a javascript based solution is possible a non-script solution would be preferred.

2
  • what's wrong with floating all divs left? Commented Oct 17, 2012 at 13:11
  • @bobek floating all divs left will not get div n.4 to expand to fill the remaining space, the way <td>s would do Commented Oct 17, 2012 at 13:16

2 Answers 2

23

You can use display:table to create this effect, I made a quick fiddle

This makes the individual div's act like table cells, and the section is the table, I used a section just to have cleaner code, a div would work too.

You will notice the table cells get smaller than you specified if the window size is too small, this is because of the table's default behaviour. To combat this just add a min-width (with the same value as the width)

Sign up to request clarification or add additional context in comments.

5 Comments

since i don't have to support browsers < ie8 this is a veeery good solution! What if i need to have this vertically (i tried to modify the example to have the divs stacked vertically but that don't seem to work) is there any other magic spell for this? Thanks so far!
Here you go. jsfiddle.net/fa9FJ/4 I got myself confused half way through so this may not be the optimum technique for it, and may be overly messy, but it works! (to test, change the height of #container, resizing the window doesn't work for height)
oky i got this same thing to work vertically here: jsfiddle.net/UXhbJ/2 thanks a lot. That solves 99% of my css problems.
Glad you got it sorted :) Feel free to comment here if you get any related problems
Thanks I needed that :). And also to support older browser you could replace section with <div class="section"> and section {...} in css with div.section {...}. Thanks again @Andy and +1 of course.
8

http://jsfiddle.net/lnplnp/bFrmD/

#div1 {
    width: 60px;
}
#div2 {
    width: 30%;
}
#div3 {
    width: 40px;
}
#div4 {
}
#div5 {
    width: 100px;
}
.layout {
    display:table;
}
.cell {
    display: table-cell;
}​
<html>
    <head>
        <title>DIV LIKE TABLE</title>
    </head>
    <body>
        <div class="layout">
            <div id="div1" class="cell">1</div>
            <div id="div2" class="cell">2</div>
            <div id="div3" class="cell">3</div>
            <div id="div4" class="cell">4</div>
            <div id="div5" class="cell">5</div>
        </div>
    </body>
</html>

​Cross your finger ! With recent broswers you can do it now !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.