Color scheme

Styling SVG with CSS

SVG can be styled three different ways, and they do not all have the same priority. A presentation attribute on an element, an inline style, and a class in an external stylesheet all compete for the same property, and the cascade resolves them in a specific order that surprises developers who expect CSS class selectors to always win. Understanding the hierarchy matters whenever you are modifying someone else's SVG, overriding SVG sprite defaults, or trying to figure out why fill: currentColor is not taking effect.

The Three Styling Channels

Every SVG property can be set through three distinct channels, from lowest to highest priority:

1. Presentation Attributes

Presentation attributes are SVG-specific attributes written directly on elements: fill="red", stroke="black", stroke-width="2". They behave like a zero-specificity CSS declaration. Any CSS rule, even a bare element selector, overrides them.


    <circle cx="12" cy="12" r="10" fill="red" stroke="black" stroke-width="2"></circle>

2. CSS Rules (Embedded or External)

CSS class selectors, element selectors, and ID selectors applied to SVG elements follow normal CSS specificity rules. They override presentation attributes because any authored CSS declaration sits above presentation attributes in the cascade, but they lose to inline styles.


    /* Overrides fill="red" presentation attribute */
    circle {
        fill: blue;
    }

    .icon-accent {
        fill: var(--color-accent);
    }

3. Inline Style Attribute

An inline style attribute on an SVG element overrides both presentation attributes and external CSS rules, just as it does on HTML elements. It loses only to !important declarations in stylesheets.


    <!-- Inline style beats both the presentation attribute and any CSS rule -->
    <circle cx="12" cy="12" r="10" fill="red" style="fill: green;"></circle>

The Priority Order

flowchart LR
    D["!important CSS rule\nfill: purple !important"] -->|"wins over"| C["Inline style\nstyle='fill: green'"]
    C -->|"wins over"| B["CSS rule\n.icon { fill: blue }"]
    B -->|"wins over"| A["Presentation attribute\nfill='red'"]
The Specificity Surprise

Developers often expect a class selector to override everything on an element. With SVG, a bare element selector like circle { fill: blue; } overrides a presentation attribute even though an element selector has lower specificity than a class. This is because presentation attributes sit below all authored CSS in the cascade, not because of specificity. A class selector also overrides a presentation attribute, but so does an element selector. The surprise is that specificity does not determine whether CSS wins over presentation attributes: all CSS wins, regardless of specificity, unless an inline style is involved.

CSS Inheritance into SVG

SVG elements inside an HTML document participate in the CSS cascade and inherit certain properties from their HTML ancestors. This is why currentColor works: when you set color: red on a parent <button>, and the SVG inside uses fill: currentColor or stroke: currentColor, the SVG resolves currentColor to red via inheritance.

Properties that inherit into SVG from CSS include:

  • color (the source of currentColor)
  • font-family, font-size, font-weight (for SVG text elements)
  • visibility
  • cursor
  • SVG-specific inheritable properties: fill, stroke, stroke-width, stroke-linecap, stroke-linejoin, opacity

Properties that do NOT inherit into SVG include most box-model properties: width, height, margin, padding, border, background. Setting background: blue on a parent does not affect SVG shapes inside it.


    /* Setting fill on a parent SVG propagates to all children that don't override it */
    svg {
        fill: none;
        stroke: currentColor;
        stroke-width: 2;
    }

    /* A child path can still override */
    svg .filled-dot {
        fill: currentColor;
        stroke: none;
    }

Three-Channel Demo

The demo below applies styles to the same SVG circle using all three channels simultaneously. Toggle each channel on and off to see which one wins when multiple sources conflict. Notice that disabling the CSS rule reveals the presentation attribute underneath, and that the inline style always wins over both unless you force it out with !important.

Toggle Channel:
Active Winner: Presentation attribute (fill="gold")

currentColor and the Inheritance Chain

currentColor is the bridge between CSS color and SVG paint. It resolves to whatever value the color property holds on the element itself or its nearest ancestor that sets it. Because color inherits through the HTML tree, any ancestor's color value flows into SVG fill or stroke when those are set to currentColor.


    /* Ancestor sets color */
    .btn-primary {
        color: white;
    }

    /* SVG inside the button inherits it */
    .icon {
        stroke: currentColor; /* resolves to white */
        fill: none;
    }

This chain means you never need to set a separate icon color in your CSS. Changing the button's color on hover automatically updates the icon. Switching to dark mode via a token change updates every icon that uses currentColor with no additional rules.


    /* Hover: both text and icon update with one rule */
    .btn-primary:hover {
        color: var(--color-accent-light);
        /* icon stroke resolves to --color-accent-light automatically */
    }
currentColor Works on fill and stroke

You can use currentColor on both fill and stroke independently, or on both at the same time. A two-tone icon that uses fill: currentColor on the body and stroke: currentColor on an outline will keep both tones in sync with the inherited color value.

Build Your Demo

Create a new directory at unit-6/styling-svg/ in your Practice Site with index.html and styling-svg.css. Your demo must show all three styling channels on the same SVG element and demonstrate currentColor inheritance in at least one real use case. Include:

  • An SVG with a presentation attribute setting a fill or stroke color, overridden by a CSS class rule.
  • A second example showing currentColor in action: an SVG icon inside a button or link where changing the parent's color on :hover updates the icon automatically with no additional CSS.
  • A short comment in your CSS explaining which channel wins and why for each styled element.

Add a link to your demo on your Practice Site homepage and in your README.md.

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • The three styling channels in priority order, with a live demonstration of overriding a presentation attribute with a CSS class and then an inline style.
  • The specificity surprise: why a bare element selector overrides a presentation attribute even though element selectors have lower specificity than classes. Clarify that the cascade position of authored CSS vs. presentation attributes is what determines the winner, not specificity between them.
  • The currentColor inheritance chain: show the color property set on a parent HTML element, and prove that changing it updates an SVG icon's stroke or fill without touching the SVG CSS at all.
  • Your Practice Site demo: walk through each styled element, point to the winning channel in DevTools, and show the computed value resolving in the Styles panel.

The most effective way to teach the cascade surprise is to write a CSS rule with zero specificity (bare element selector) and show it beating a presentation attribute. Then ask teammates to predict what happens before you show them. The prediction gap is what makes the concept stick.

Resources