We have an adaptive layout where some list elements are displayed horizontally:
| Li1 | Li2 | Li 3 | Li4 |
Obviously I can just set
ul {width:100%}
ul li {width:25%}
To have the li's change size as the browser changes size. However, we want the left edge of the left-most li to align to the left edge with the right-most li right edge aligning to the right edge even as the browser expands.
Li1 Li2 Li3 Li4
| |
Li1 Li2 Li3 Li4
| |
Li1 Li2 Li3 Li4
| |
Is there a way to do this with pure css?
The quickest way to display a list on a single line is to give the <li> elements a display property value of inline or inline-block . Doing so places all the <li> elements within a single line, with a single space between each list item.
If your text doesn't span more than one line, justifying doesn't do anything. Your text has to wrap to the next line, and then the FIRST line will be justified, but not the second.
To get the left justified text you would use text-align: justify; in you css. In the latest versions of Chrome and IE you get justified text with the last line being left aligned using that css.
Use this solution (or this one). Translating that answer to a list results in:
Markup:
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>
Style sheet:
ul {text-align: justify}
ul::after {width: 100%; display: inline-block; content: "."; visibility: hidden}
li {display: inline-block}
This is an IE7+ solution. For IE7 you need an extra element adjacent to the list items.
IMHO, the best way is use CSS3 Flexboxes:
.menu {
display: flex;
justify-content: space-around;
}
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