CSS Lists

CSS Styling Lists

In HTML, there are two main types of lists:

CSS has the following properties for styling HTML lists:


CSS Style List-item Markers

The CSS list-style-type property specifies the type of list-item marker in a list.

The following example shows some of the available list-item markers:

Example:

Result:

Note: Some of the values are for unordered lists, and some for ordered lists.

CSS Replace List-item Marker with an Image

The CSS list-style-image property is used to replace the list-item marker with an image.

Note: Always specify a list-style-type property in addition. This property is used if the image for some reason is unavailable.

Example:

Result:

CSS Position the List-item Markers

The CSS 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. This is default:

list-style-position: inside; means that the bullet points will be inside the list item. As it is part of the list item, it will be part of the text and push the text at the start:

Example:

Result:

CSS Remove List-Item Markers

The list-style-type:none; property is used to remove the list-item markers.

Note: A list has a default margin and padding. To remove this, add margin:0 and padding:0 to the <ul> or <ol> element:

Example:

Result:

CSS list-style Shorthand Property

The list-style property is a shorthand property. It is used to set all the list properties in one declaration.

When using the shorthand property, the order of the property values are:

If one of the property values above is missing, the default value for the missing property will be inserted.

Example:

Result:

CSS Styling List With Colors

We can also style lists with colors, margins and padding, to make them look a little more interesting.

Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to the <li> tag will affect the individual list items:

Example:

Result:

All CSS List Properties

Property Description
list-style Sets all the properties for a list in one declaration
list-style-image Specifies an image as the list-item marker
list-style-position Specifies the position of the list-item markers (bullet points)
list-style-type Specifies the type of list-item marker

 

CSS Tables

HTML tables can be greatly improved with CSS:

Example:

Result:

CSS Table Borders

The CSS border property is used to specify the width, style, and color of table borders.

The border property is a shorthand property for:

The example below specifies a 1px solid border for <table>, <th>, and <td> elements:

Firstname Lastname
Peter Griffin
Lois Griffin

Example:

Result:

CSS Table Border Color

The example below specifies a 1px solid green border for <table>, <th>, and <td> elements:

Firstname Lastname
Peter Griffin
Lois Griffin

Example:

Result:

CSS Collapse Table Borders

The CSS border-collapse property sets whether table borders should collapse into a single border or be separated as in standard HTML.

This property can have one of the following values:

The following table has collapsed borders:

Firstname Lastname
Peter Griffin
Lois Griffin

Example:

Result:

CSS Table Padding

To control the space between the border and the content in a table, use the padding property on <td> and <th> elements:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Border Spacing

The CSS border-spacing property sets the distance between the borders of adjacent cells.

Note: This property works only when border-collapse is set to “separate”.

The following table has a border-spacing of 15px:

Firstname Lastname
Peter Griffin
Lois Griffin

Example:

Result:

CSS Outside Table Borders

If you just want a border around the table (not inside), you specify the border property only for the <table> element:

Firstname Lastname
Peter Griffin
Lois Griffin
Example:

Result:

CSS Table Size (Width and Height)

CSS Table Width

The CSS width property is used to set the width of a table.

The width can be set:

  • in percent (%)
  • as a fixed length (px)
  • by the auto keyword

CSS Table Width in Percent

To create a table that spans the entire screen (full-width), use width: 100%:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Result:

CSS Table Width in a Fixed Width

To create a table with a fixed width, use width: 500px:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Table Height

The CSS height property is used to set the height of a table.

The height can be set:

The example below sets the height of the table headers (<th>) to 70px:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Table Alignment

Horizontal Alignment

The CSS text-align property is used to set the horizontal alignment of the content in <th> or <td>.

This property can have one of the following values:

Note: By default, the content of <th> elements are center-aligned and the content of <td> elements are left-aligned!


CSS text-align: center

To center-align the content of <td> elements, use text-align: center:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS text-align: left

To force the content of <th> elements to be left-aligned, use text-align: left on <th> elements:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

Vertical Alignment

The CSS vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>.

Note: By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).

The following example sets the vertical text alignment to bottom for <td> elements:

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Table Styling

CSS Table Padding

To add some more space between the inner borders and the content in a table, use the padding property on <td> and <th> elements:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Horizontal Dividers

To create horizontal dividers for a table, add the border-bottom property to <th> and <td> elements:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Hoverable Table

Use the CSS :hover selector on <tr> to highlight table rows on mouse over:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Zebra-striped Table

For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Table Color

The example below specifies a background color and a text color for the <th> elements:

First Name Last Name Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300

Example:

Result:

CSS Table Properties

Property Description
border Sets all the border properties in one declaration
border-collapse Specifies whether or not table borders should be collapsed
border-spacing Specifies the distance between the borders of adjacent cells
caption-side Specifies the placement of a table caption
empty-cells Specifies whether or not to display borders and background on empty cells in a table
table-layout Sets the layout algorithm to be used for a table