CSS Reference ยท Properties for Special Contexts
SVG properties in CSS
Overview of the CSS properties that style SVG: fill, stroke, stroke-width, dashes, paint-order and how they interact with inherited and shared styles.
SVG shapes are drawn with a handful of presentation properties. They are normal CSS properties, so you can set them in a stylesheet, change them on :hover, animate them with transitions and control them with custom properties, while the same names also exist as attributes in the SVG markup (fill="tomato"). This page is a map of the family; each property has its own page.
A shape has two paintable parts. The fill colors its inside, and the stroke is the outline drawn along its edge. Almost everything else in this group decides how that stroke looks or in which order the parts are drawn.
Try it
Hover the rectangle: its fill changes through CSS alone.
The family at a glance
| Property | What it controls |
|---|---|
fill |
The paint inside the shape. |
stroke |
The paint of the outline. |
stroke-width |
The thickness of the outline. |
stroke-dasharray |
A dash and gap pattern for the outline. |
stroke-dashoffset |
Where the dash pattern starts. |
paint-order |
Whether fill, stroke or markers are painted first. |
Other members of the family include fill-opacity, stroke-opacity, stroke-linecap, stroke-linejoin, stroke-miterlimit, fill-rule, clip-rule and vector-effect. They follow the same rules described below.
How they behave
- They are inherited. Setting
fillon a<g>or on the<svg>element colors every shape inside it that does not set its own. That makes theming easy. - Presentation attributes lose to CSS. A value in a stylesheet always beats the matching attribute, because attributes count as the weakest author-level rule. Use that to restyle inline SVG from outside.
- They only apply to SVG content. Setting
fillon an HTML<div>does nothing, althoughcurrentColorlets SVG pick up the CSScolorof its surroundings. - Inline SVG only. CSS in your page styles SVG that is in the page's markup. An SVG loaded with
<img>or as a CSS background is a separate document and cannot be reached by your selectors. - Use
currentColor.fill: currentColormakes an icon follow the surrounding text color, including hover and dark-mode changes.
Usage notes
- The default
fillis black and the defaultstrokeisnone, which is why a bare shape appears as a black blob. - Strokes are centered on the edge, so half the stroke width lies outside the shape. Mind this when a shape touches the edge of the
viewBox. - Dash patterns and offsets are measured in the SVG's own user units, so they scale with the
viewBox.
Browser support
All of these properties have been supported in every major browser for a very long time. Check a compatibility table for paint-order on HTML text, which is a newer use.