Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch list items <li> to fill the width of <ul>

Tags:

I have an unordered list (<ul>) with various number of items (<li>) in it. I want to use one CSS styling that allows the items to stretch to the width of the list.

This is what I have:

enter image description here

This is what I want:

enter image description here

HTML:

These 4 items should fill the width:<br/> <ul id="ul1">     <li>item</li>     <li>item</li>     <li>item</li>     <li>item</li> </ul> <br/> These 5 items should fill the width:<br/> <ul id="ul2">     <li>item</li>     <li>item</li>     <li>item</li>     <li>item</li>     <li>item</li> </ul> <br/> And so on and so forth... 

Here is the JSFiddle to get you started.

Note: I don't want to hard-code the width in CSS. Note: If you are wondering about the use-case, this is a navigation structure in a responsive design and I want the list items to always fill up the available width no matter what is the resolution.

like image 559
AlexStack Avatar asked Nov 15 '12 11:11

AlexStack


People also ask

How do you make ul full width?

Basically, you just have to apply display: block , float: left and width: 33.3% on <li> elements to make them stretch out the full width of the <ul> element, which is already at 100% of the containing <div> .

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 you fit a full width to a div?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

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.


1 Answers

Demo HI now you can do this

Used to display :table and display:table-cell

as like this

Css

ul {     border:1px dotted black;     padding: 0;     display:table;     width:100%;     box-sizing:border-box;     -moz-box-sizing:border-box;     -webkit-box-sizing:border-box; } li {     background-color: red;     display:table-cell; } li:nth-child(2n) {     background-color: #0F0; } 

Now define your parent display:table; or width:100%; and define your child display:table-cell;

Demo

like image 160
Rohit Azad Malik Avatar answered Nov 08 '22 22:11

Rohit Azad Malik