HTML <li> style Attribute

The style attribute on a <li> tag assigns a unique style to the list item. A style contains one or more CSS property/value pairs that define the appearance of the list item.

Example

#

A style attribute on 3 <li> elements.

  1. Designers
  2. Developers
  3. Managers
<ol style="display:inline-block; width: 250px;">
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Designers</li>
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Developers</li>
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Managers</li>
</ol>

Using style

The style attribute specifies the style, i.e. look and feel, of the <li> element.

A style contains any number of CSS property/value pairs, separated by semicolons (;).

The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.

This inline styling affects the current <li> element only.


Syntax

<li style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

A style attribute on 3 <li> elements.
Clicking the button toggles the background color.

  1. Designers
  2. Developers
  3. Managers

<ol id="myol" style="display:inline-block; width: 250px;">
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Designers</li>
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Developers</li>
  <li style="background:papayawhip; padding: 5px; margin: 5px 0;">Managers</li>
</ol>

<br />
<button type="button" onclick="toggle();">Toggle style</button>

<script>
  let toggle = () => {
    let elements = document.getElementById("myol").getElementsByTagName("li");
    [].forEach.call(elements, element => {
        if (element.style.backgroundColor === "papayawhip") {
          element.style.backgroundColor = "lightblue";
        } else {
          element.style.backgroundColor = "papayawhip";
        }
    });
  }
</script>

Code explanation

The style attribute assigns a background color to the <li> elements.

Clicking the button calls JavaScript which locates all the <li> elements.

It then iterates over the elements and toggles the background color and back.


Browser support

Here is when style support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <li>
Guides