SVG Presentation Attributes vs Inline CSS Specificity

SVG styling can often cause confusion because presentation attributes and inline CSS styles appear directly inside SVG markup but behave fundamentally differently under CSS cascade and specificity rules. This article explains how the CSS cascade prioritizes SVG presentation attributes, external stylesheets, and inline style attributes, outlining the exact hierarchy needed to predictably style scalable vector graphics.

Understanding SVG Presentation Attributes

SVG presentation attributes are XML attributes such as fill, stroke, stroke-width, rx, and opacity. Although they are written directly on the SVG element tag (for example, <rect fill="red" />), the CSS specification treats them as author-level CSS rules with a specificity of zero (0, 0, 0, 0).

Because they possess zero specificity, any valid CSS rule defined elsewhere—whether in an external stylesheet, an embedded <style> block, or an inline style attribute—will override an SVG presentation attribute.

The Hierarchy of Specificity in SVGs

When the browser parses styles for SVG elements, it evaluates them according to the standard CSS cascading order:

  1. SVG Presentation Attributes: Treated as author rules with 0,0,0,0 specificity. This makes them the lowest-priority author styles.
  2. External and Embedded CSS Rules: Standard selectors (rect, .class, #id) have higher specificity than presentation attributes. Even a basic element selector like rect { fill: blue; } (specificity 0, 0, 0, 1) will override fill="red".
  3. Inline CSS Styles (style="..."): Properties defined inside the style attribute (such as <rect style="fill: green;" />) have a specificity weight of 1, 0, 0, 0. These override all normal external and internal CSS rules as well as presentation attributes.
  4. Declarations with !important: Any CSS rule utilizing the !important flag overrides lower-level rules and inline styles that do not use the flag.

Presentation Attributes vs. Inline Styles

The primary point of confusion lies in the visual similarity between presentation attributes and inline styles.

Summary Comparison

Style Method Example Specificity Weight Overridden by External CSS?
Presentation Attribute fill="orange" 0, 0, 0, 0 Yes (by any matching selector)
Element Selector path { fill: blue; } 0, 0, 0, 1 Only by higher specificity rules
Class Selector .icon { fill: blue; } 0, 0, 1, 0 Only by higher specificity rules
ID Selector #logo { fill: blue; } 0, 1, 0, 0 Only by inline styles or !important
Inline CSS style="fill: orange;" 1, 0, 0, 0 Only by !important

Best Practices