Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an unordered list span 100% the width of a div

Tags:

css

<div>
  <ul>
    <li>First</l1>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
  </ul>
</div>


div {
  width: 100%;
}
li {
  list-style: none;
  float: left;
}

With CSS, is there a way to make the LI tags automatically fill the whole width of the parent div, evenly? So they'd each take up 25%.

Specifying 'width' as 25% would obviously work but it's not solution I'm after. The menu in question is dynamically created and new items are created and deleted at times.

Cheers

like image 845
Allan Thomas Avatar asked Oct 13 '11 18:10

Allan Thomas


People also ask

How do you make a div 100 width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

Is a div 100% width by default?

auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.

How do I force div width fit content?

Using inline-block property: Use display: inline-block property to set a div size according to its content.


1 Answers

I think you have three options:

  1. Use JavaScript to calculate the sizes

  2. Use a table, as discussed in this question (this is actually not a bad option — it’s pure HTML and CSS

  3. If you’re only targeting new browsers, you can use the new flexible box component of CSS (shown here with a couple of vendor prefixes):

    ul{
        padding: 0;
        display: -webkit-box;
        display: -moz-box;
        display: box;
    }
    li{
        list-style: none;
        list-style:none;
        text-align: center;
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        box-flex: 1;
    }
    
like image 144
s4y Avatar answered Sep 28 '22 01:09

s4y