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:
This is what I want:
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.
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> .
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.
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.
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.
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
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