SVG Attributes vs CSS Properties Explained
While SVG attributes and SVG CSS properties often share identical
names—such as fill, stroke,
opacity, and geometric dimensions—they differ fundamentally
in cascade priority, syntax rules, dynamic capability, and DOM
manipulation. SVG presentation attributes exist as XML markup directly
on elements, while CSS properties exist within style sheets,
<style> blocks, or inline style
attributes to control rendering through the CSS cascade.
Cascade Priority and Specificity
The most critical distinction is how browsers resolve conflicting values. When an SVG presentation attribute and a CSS property of the same name target the same element, the CSS property always wins:
- Presentation Attributes: Evaluated as low-priority CSS rules with a specificity of zero. Any author-level CSS rule overrides them.
- External/Internal CSS: Element, class, and ID selectors override presentation attributes, regardless of selector complexity.
- Inline CSS (
style="..."): Overrides both presentation attributes and external CSS rules (unless!importantis used).
For example, on
<circle fill="red" class="blue-circle" />, where
.blue-circle { fill: blue; }, the circle renders blue
because the CSS class overrides the fill attribute.
Dynamic State and Responsive Styling
CSS properties allow elements to respond to user interaction and viewport changes without modifying the DOM:
- Pseudo-classes: States like
:hover,:focus, and:activecan dynamically modify CSS properties, whereas SVG attributes remain static unless changed via JavaScript. - Media Queries: CSS properties adapt based on dark
mode (
prefers-color-scheme), screen size, or print media. - Animations: CSS transitions and
@keyframeswork directly with CSS properties for smoother rendering and hardware acceleration compared to manipulating XML attributes.
Syntax and Unit Requirements
Syntax requirements differ between XML attributes and CSS rules:
- Unitless Values: Many SVG presentation attributes accept unitless numbers that automatically map to the SVG coordinate space (user units).
- CSS Units: Depending on the browser and property,
CSS equivalents often require explicit unit identifiers (such as
px,%, orrem), although SVG 2 has aligned many geometric properties to accept unitless values in CSS contexts.
Scope of Supported Features
Not every SVG attribute has a corresponding CSS property:
- Presentation Attributes: A specific subset of
attributes (like
fill,stroke,stroke-width,transform,opacity, and SVG 2 geometry likecx,cy,r) can be written as CSS properties. - Structural Attributes: Core structural and metadata
attributes (such as
viewBox,xmlns,id,patternUnits, andgradientUnits) exist strictly as XML attributes and cannot be controlled via CSS.
Best Practices
Use SVG presentation attributes to define baseline dimensions, fallbacks, and standalone graphic structures. Use SVG CSS properties when you need theming, design-system integration, media-query responsiveness, or interactive hover and focus states.