Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

OK so this is actually a little complicated.

I have a navigation list where the list items are set to inline-block. 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.

http://jsfiddle.net/4K4cU/2/

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

like image 260
harryg Avatar asked Jun 12 '13 15:06

harryg


People also ask

Do inline elements take up the entire width of their container?

Note: An inline element does not start on a new line and only takes up as much width as necessary.

How do inline block elements add up to 100% width?

To do this, you simply need to add display: flex to the parent container and flex-grow: 1 to the divs (as opposed to defining a width). With the flexbox styles applied, we can have three inline divs of equal width – just as we can with the inline-block solutions.

Can a span be inline block?

A span is by default an inline element, you cannot set the width, height, and other properties associated with blocks.

Can inline element change width?

The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.


2 Answers

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 {
    margin:0;
    padding: 0;
    list-style-type: none;
    display: table;
    table-layout: fixed;
    width:100%;
}
.list>li {
    display: table-cell;
    border:1px green solid;
    padding:5px;
    text-align: center;
}
.container {
    border: 1px #777 solid;
}
<div class="container">
    <ul class="list">
        <li>text</li>
        <li>text</li>
        <li>some longer text</li>
        <li>text</li>
    </ul>
</div>

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/

like image 151
harryg Avatar answered Sep 18 '22 17:09

harryg


It's now 2016 and I wanted to update this question with an answer using flexbox. Consult with CanIUse for browser-compatiblity.

/* Important styles */
ul {
  display: flex;
}
li {
  flex: 1 1 100%;
  text-align: center;
}

/* Optional demo styles */
* {
  margin: 0;
  padding: 0;
}
ul {
  margin-top: 2em;
  justify-content: space-around;
  list-style: none;
  font-family: Verdana, sans-serif;
}
li {
  padding: 1em 0;
  align-items: center;
  background-color: cornflowerblue;
  color: #fff;
}
li:nth-child(even) {
  background-color: #9980FA;
}
<ul>
  <li>text</li>
  <li>text</li>
  <li>text</li>
  <li>text</li>
</ul>

Pre-edit fiddle (now inlined in above snippet)

like image 31
kano Avatar answered Sep 22 '22 17:09

kano