liveBook · Manning

Style attribute

As explained in CSS in Depth, Second Edition

The style attribute is an HTML attribute whose value contains CSS declarations applied directly to its element.

The style attribute holds CSS declarations directly on an HTML element. Because those declarations target the element without a selector, they override declarations from a stylesheet or a style element. section 1.1.2

Making one navigation link orange

Add the declaration to the Specials link: <a href="/specials" class="featured" style="background-color: orange;">Specials</a>. The orange background applies only to that link and takes precedence over matching selector-based rules. section 1.1.2

Use the style attribute with these cascade rules in mind:

  • Apply it when a declaration must affect one specific element directly. section 1.1.2
  • Expect it to outrank declarations applied through CSS selectors in a stylesheet or a style element. section 1.1 section 1.1.2
  • Override a normal inline declaration from a stylesheet only by using !important; avoid making inline declarations important, because nothing can override them. section 1.1.2
  • Prefer moving the rule into a stylesheet when a reusable or easier-to-maintain approach is possible. section 1.1.2

How CSS in Depth, Second Edition covers it

How deep it goes
Explains direct application, cascade precedence, and !important exceptions without developing broader selector or layer mechanics.
Examples
Uses an orange Specials navigation link to demonstrate element-specific inline styling overriding selector-based declarations.
What it assumes
Assumes basic HTML elements and CSS declarations while demonstrating the cascade through a navigation-link example.
What you can do after
Apply inline CSS and diagnose why it overrides ordinary stylesheet or style-element declarations.
How it connects
Connects the style attribute to the CSS cascade, CSS selectors, stylesheets, style elements, and !important.

Also explained in

Compare how 7 books explain style attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.