Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full-justify for li elements [duplicate]

Tags:

html

css

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?

like image 224
Brad Herman Avatar asked Apr 04 '12 22:04

Brad Herman


People also ask

How do you keep Li in the same line?

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.

Why is text align justify not working?

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.

How do I justify text left in CSS?

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.


2 Answers

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.

like image 55
NGLN Avatar answered Oct 30 '22 13:10

NGLN


IMHO, the best way is use CSS3 Flexboxes:

.menu {
    display: flex;
    justify-content: space-around;
}
like image 30
VeroLom Avatar answered Oct 30 '22 11:10

VeroLom