A style attribute on a <li> tag assigns a unique style to the list item.
Its value is CSS that defines the appearance of the list item.
A style attribute on 3 <li> elements.
<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>
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.
<li style="CSS-styles">
Value | Description |
---|---|
CSS-styles | One or more CSS property/value pairs separated by semicolons (;). |
A style attribute on 3 <li> elements.
Clicking the button toggles the background color.
<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>
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.
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 |