Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the list style disappear when display: block is added to a list item in a list (<ul> or <ol>)?

This seems to valid for display: inline; and display: inline-block; too.

This is what I mean:

ul li {   display: block;   /* Or display: inline; */   /* Or display: inline-block; */ }  <ul>   <li>list item1</li>   <li>list item3</li>   <li>list item3</li> </ul> 

And with list style I mean the actual "bullets" or "numbers" (when <ol> is used)

like image 574
Fredrik Avatar asked Jun 06 '11 19:06

Fredrik


People also ask

What does style display block do?

A block element fills the entire line, and nothing can be displayed on its left or right side. The display property also allows the author to show or hide an element. It is similar to the visibility property.

What is list style position in CSS?

The list-style-position property specifies the position of the list-item markers (bullet points). list-style-position: outside; means that the bullet points will be outside the list item. The start of each line of a list item will be aligned vertically: Coffee - A brewed drink prepared from roasted coffee beans... Tea.

How do you display items in a list in CSS?

#display: list-item. The only HTML elements displayed as list-item are the (unsurprisingly) list items <li> but also the definition descriptions <dd> . A list item is rendered with a bullet point (if in an unordered list <ul> ) or with a incremental number (if within an ordered list <ol> ).

How do you display items in a list in HTML?

The <li> tag defines a list item. The <li> tag is used inside ordered lists(<ol>), unordered lists (<ul>), and in menu lists (<menu>). In <ul> and <menu>, the list items will usually be displayed with bullet points. In <ol>, the list items will usually be displayed with numbers or letters.


2 Answers

That's because normally, display is set to list-item for <li> elements. See the W3C CSS3 specification: http://www.w3.org/TR/css3-lists/#declaring-a-list-item.

To declare a list item, the ‘display’ property should be set to ‘list-item’.

Note that you can give arbitrary HTML elements the same behaviour; set display: list-item on a <div> for example.

like image 138
Martijn Pieters Avatar answered Oct 24 '22 11:10

Martijn Pieters


An updated solution is to make use of :before and content.

See this JSfiddle for a working example. http://jsfiddle.net/t72h3/

ul li {   display: inline-block; }  ul li:before {   content: "• "; }  <ul>   <li>list item1</li>   <li>list item3</li>   <li>list item3</li> </ul> 
like image 34
alex Avatar answered Oct 24 '22 13:10

alex