CSS Lists
CSS list properties are used as follows:
- Set different list item markers for ordered lists
- Set different list item markers for unordered lists
- Set list item markers as images
List
In HTML, there are two types of lists:
- Unordered list ul - list items are marked with special shapes (such as small dots, small squares, etc.)
- Ordered list ol - list items are marked with numbers or letters
With CSS, lists can be styled further, and images can be used as list item markers.
An unordered list looks like this:
-
Coffee
-
Tea
-
Coca Cola
-
Coffee
-
Tea
-
Coca Cola
An ordered list looks like this:
-
Coffee
-
Tea
-
Coca Cola
-
Coffee
-
Tea
-
Coca Cola
Different list item markers
The list-style-type property specifies the type of list item marker:
Example
ul
.a
{
list-style-type:
circle
;
}
ul
.b
{
list-style-type:
square
;
}
ol
.c
{
list-style-type:
upper-roman
;
}
ol
.d
{
list-style-type:
lower-alpha
;
}
Try it »
Some values are for unordered lists, and some are for ordered lists.
Images as list item markers
To specify an image as the list item marker, use the list-style-image property:
Example
ul
{
list-style-image:
url
('
sqpurple
.
gif
')
;
}
Try it »
The above example does not display the same in all browsers; IE and Opera display the image marker slightly higher than Firefox, Chrome, and Safari.
If you want the same image marker in all browsers, you should use a browser compatibility solution, as follows
Browser compatibility solution
To display the image marker the same in all browsers, the following example:
Example
ul
{
list-style-type:
none
;
padding:
0
px
;
margin:
0
px
;
}
ul
li
{
background-image:
url
(
sqpurple
.
gif
)
;
background-repeat:
no-repeat
;
background-position:
0
px
5
px
;
padding-left:
14
px
;
}
Try it »
Example explanation:
- ul:
- Set the list type to no list item marker
- Set padding and margin to 0px (browser compatibility)
- All li in ul:
- Set the URL of the image and make it display only once (no repetition)
- You need to position the image (left 0px, top and bottom 5px)
- Use the padding-left property to place the text in the list
List - shorthand property
All list properties can be specified in a single property. This is called a shorthand property.
When using the shorthand property for lists, the list style properties are set as follows:
Example
ul
{
list-style:
square
url
("
sqpurple
.
gif
")
;
}
Try it »
The following properties can be set in order:
- list-style-type
- list-style-position (for explanation, see the CSS property table below)
- list-style-image
If one of the above values is missing, the rest still follow the specified order, and that is fine.
More examples
All the different list item markers
This example demonstrates all the different CSS list item markers.
Remove the default settings
The list-style-type:none property can be used to remove the small markers. By default, lists or
also have padding and margin set, which can be removed using
margin:0 and padding:0 to remove:
Example
ul
{
list-style-type:
none
;
margin:
0
;
padding:
0
;
}
Try it »
All CSS list properties
| property | Description |
|---|---|
| list-style | Shorthand property. Used to set all the properties for a list in a single declaration |
| list-style-image | Sets the image as the list item marker. |
| list-style-position | Sets the position of the list item marker in the list. |
| list-style-type | Sets the type of the list item marker. |