Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<li> elements with adapted width

I'm wondering if it is possible to do this in CSS, without javascript:

A list of N <li> items, with display inline, equal width, and the width of the all equal to the width of the container

I can have 3 <li> items, in this case the <li> width will be 33%, or I can have 4 <li> items, then the li width will be 25%.

like image 733
tahir Avatar asked Nov 18 '11 22:11

tahir


People also ask

How do you get full width with Li?

This value prevents the contents of the list item element to take up the entire width. An easy way to fix this and create a full width li link is to convert the a element to a flexbox container. This is done by adding display flex to the nav_item selector.

How do I change the width of a Li in HTML?

You'd have to work on your code because you can't assign widths to inline elements. But this can be solved by setting the display to block and by floating it.

How to stretch content in CSS?

Define the available stretch-able space by using padding on the parent element. Use box-sizing: border-box on the parent element to subtract the padding and border defined when child element sets height 100%. This will cause child elements to use the actual free space of the parent when using height: 100%

How to make content fit in a div?

You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).


1 Answers

This is a perfect example for usage of display: table. Works in modern browsers and IE8+...
Support chart
JSFiddle

css:

ul {
    display: table;
    width: 100%;
    table-layout: fixed; /* optional, for equal spacing */
    border-collapse: collapse;
}
li {
    display: table-cell;
    text-align: center;
    vertical-align: middle; /* or similar, if needed */
}

html:

<ul>
   <li>foo</li>
   <li>bar</li>
   <li>baz</li>
</ul>
like image 149
Marc Diethelm Avatar answered Oct 13 '22 15:10

Marc Diethelm