harryg harryg - 1 month ago 4x
CSS Question

css make inline-block elements span the whole width of container

OK so this is actually a little complicated.

I have a navigation list where the list items are set to

. The number of items is the list is dynamic so may vary.

My aim is to have the list items span the whole width of the container. (e.g. if there were 4 list items each one would take up 25% of the container width [ignoring margin/padding etc])

There is the added complication that browsers seem to add a 4px margin to inline-block elements where there is whitespace between them (linebreak/space etc).

I have made a fiddle as a starting point which has 2 examples: the first is just the list items in inline-block mode which the 2nd justifies them accross the width.

Neither achieves what I want which is for the whole width to be taken up by the elements without them breaking onto another line.


edit: slightly separate but why in my 2nd example is there a space beneath the
s, dispite the fact I have set line-height and font-size to 0?


OK, despite many decent answers and my inital thinking that js/jquery was the only way to go there is in fact a good css-only solution: using table cells. Original suggestion by @Pumbaa80

.list {
    padding: 0;
    list-style-type: none;
    display: table;
    table-layout: fixed;
.list>li {
    display: table-cell;
    border:1px green solid;
    text-align: center;
.container {
    border: 1px #777 solid;
<div class="container">
    <ul class="list">
        <li>some longer text</li>

This is superior to other solutions as:

  • css-only
  • no 4px margin problem as with inline-block
  • no clearfix need for floated elements
  • maintains equally distributed width independent of li content
  • concise css

Fiddle here: http://jsfiddle.net/rQhfC/