<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
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.
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.
Using inline-block property: Use display: inline-block property to set a div size according to its content.
I think you have three options:
Use JavaScript to calculate the sizes
Use a table, as discussed in this question (this is actually not a bad option — it’s pure HTML and CSS
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With