2

In a chain of list elements (<li>) with display: inline, is there a way to force a line break using a CSS property?

Using a <br> within a <li> feels dirty, and outside a <li> is probably forbidden.

to clarify:

I need them "display: inline" because I may need to center them within the UL

I need only some of the elements to have a line break.

asked Nov 25, 2009 at 16:24
1
  • I am not sure what you are doing but what is the reason to using display:inline instead of float? Commented Nov 25, 2009 at 16:31

6 Answers 6

3
  1. Why do you use display:inline?
  2. display: list-item; does exactly what you need (which is default for li)
Robert Harvey
182k48 gold badges349 silver badges516 bronze badges
answered Nov 25, 2009 at 16:28
Sign up to request clarification or add additional context in comments.

1 Comment

1. Because I need to center them in a wide <ul>. 2. Good idea, will try out. 3. Probably true. :)
2

You can have all <li> elements rendered with float:left and then set on one of them clear:left. This will cause it to "jump" to the next line.

Alternatively, float:right and clear:right will do a similar thing.

answered Nov 25, 2009 at 16:27

2 Comments

Won't both work, as the list elements are centered in a 100% wide UL.
Make UL fixed width and center it. float:left | right will make LIs to be aligned to the left or right, so it may not achieve the needed effect if your UL is 100% wide.
0

Do you want a "line break" after each <li> or just after a few certain ones?

For the former: If you set the width wide enough to fill the container, they will line wrap (actually, they only have to be wide enough to fill 51% of their container, since two could no longer fit on one line). -- but in this case, you probably don't need them to be inline at all.

For the latter: You would probably be better off using float: left with a clear: left on each one you want to start a new line with.

answered Nov 25, 2009 at 16:31

Comments

0

Try putting display:block; on the particular <li> that you want on the next line.

answered Nov 25, 2009 at 16:32

Comments

0

You could try and use the :after pseudo-element but I haven't played with it much.

answered Nov 25, 2009 at 16:34

Comments

0
li.class:after { content: "\a"; white-space: pre; }

works for me.

answered Nov 25, 2009 at 18:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.