Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท SVG

SVG presentation attributes

Reference for SVG presentation attributes: fill, the stroke family, opacity, transform, text and font attributes, their CSS equivalents and override rules.

Presentation attributes set how SVG content looks: fill, stroke, opacity, font-size, transform and more. Almost every one of them is also a CSS property with the same name, so the same look can be written as an attribute, in a style attribute or in a stylesheet. Most are inherited, which is why a fill on a <g> or on the <svg> itself colors every shape inside.

HTML
<!-- three ways to paint the same circle -->
<circle cx="20" cy="20" r="15" fill="teal"/>
<circle cx="60" cy="20" r="15" style="fill: teal"/>
<circle cx="100" cy="20" r="15" class="teal"/>  <!-- .teal { fill: teal; } -->

Try it

Attributes lose to CSS

A presentation attribute counts as a CSS declaration with zero specificity, placed before all author styles. So any stylesheet rule that matches the element overrides it, and the style attribute overrides both. That makes attributes good defaults: a design tool can export fill="..." and your stylesheet can still change it.

Fill and stroke

Attribute Values Default CSS property Description
fill Color, url(#id), none, context-fill black fill Paint for the inside of the shape (or the glyphs of text).
fill-opacity 0โ€“1 or % 1 fill-opacity Opacity of the fill only.
fill-rule nonzero, evenodd nonzero fill-rule Decides which areas of a self-crossing or multi-part path count as "inside".
stroke Color, url(#id), none none stroke Paint for the outline.
stroke-width Length or % 1 stroke-width Width of the outline, centered on the geometry.
stroke-opacity 0โ€“1 or % 1 stroke-opacity Opacity of the outline only.
stroke-linecap butt, round, square butt stroke-linecap Shape of the ends of open lines. round and square extend past the end point by half the width.
stroke-linejoin miter, round, bevel, miter-clip, arcs miter stroke-linejoin Shape of corners. miter-clip and arcs are SVG 2 and not widely supported.
stroke-miterlimit Number โ‰ฅ 1 4 stroke-miterlimit How long a sharp miter corner may get (as a multiple of the width) before it is beveled.
stroke-dasharray List of lengths, or none none stroke-dasharray Dash and gap lengths, repeated: "8 4", "2 6 10 6". An odd-length list is repeated to make it even.
stroke-dashoffset Length 0 stroke-dashoffset Where along the outline the dash pattern starts. Animating it makes "line drawing" effects.
paint-order normal, or an order of fill, stroke, markers normal paint-order paint-order="stroke" paints the outline first, so a thick stroke on text no longer eats into the letters.
vector-effect none, non-scaling-stroke none vector-effect non-scaling-stroke keeps the stroke width constant when the shape is scaled. Not inherited.
The stroke family

The red guide lines show where each line really starts and ends: round and square caps stick out past them by half the stroke width. Combining stroke-linecap="round" with very short dashes (2 8) turns a dashed line into a dotted one.

fill-rule and paint-order

The copy made by <use> inherits fill-rule="evenodd" from the <use> element, because the original star doesn't set its own fill-rule. With evenodd, the pentagon in the middle is crossed twice and counts as outside, so it is left empty.

Opacity, visibility and other painting

Attribute Values CSS property Description
opacity 0โ€“1 or % opacity Opacity of the whole element (fill and stroke together, as one layer). Not inherited, but applies to a group's content as a whole.
color Color color Sets currentColor for fill, stroke, stop-color and others.
visibility visible, hidden, collapse visibility Hides the element but keeps it in the layout of text; a child can be made visible again.
display none, inline, โ€ฆ display none removes the element and its children from rendering. Other values behave alike in SVG.
clip-path url(#id), a basic shape, none clip-path Clips to a <clipPath> or a CSS shape such as circle(40%).
mask url(#id), none mask Applies a <mask> (luminance-based).
filter url(#id), CSS filter functions, none filter Applies an SVG <filter> or CSS filters such as blur(2px).
marker-start, marker-mid, marker-end url(#id), none Same names (marker sets all three) Draws a <marker> (an arrowhead, a dot) at the vertices of a path, line or polyline.
shape-rendering auto, crispEdges, geometricPrecision, optimizeSpeed shape-rendering A hint: crispEdges turns off anti-aliasing for pixel-aligned shapes.
stop-color, stop-opacity Color, 0โ€“1 Same names Colors of gradient stops (see gradients and patterns).
pointer-events visiblePainted (default), none, all, fill, stroke, โ€ฆ pointer-events Which parts of a shape react to the mouse. none lets clicks pass through.

Text attributes

These apply to <text>, <tspan> and <textPath>; x, y, dx, dy and rotate are regular attributes, not CSS properties.

Attribute Values CSS property Description
x, y Length or list of lengths (none) Position of the start of the text (or of each character, when a list is given).
dx, dy Length or list (none) Shifts relative to the previous character.
font-family, font-size, font-weight, font-style As in CSS Same names The font. font-size without units is in user units.
text-anchor start, middle, end text-anchor Which part of the text sits at x. middle centers text on a point.
dominant-baseline auto, middle, central, hanging, alphabetic, โ€ฆ dominant-baseline Which baseline sits at y. central vertically centers text on a point.
letter-spacing, word-spacing Length Same names Extra space between letters or words.
text-decoration underline, line-through, โ€ฆ text-decoration Lines drawn with the text's fill and stroke.
writing-mode horizontal-tb, vertical-rl, vertical-lr writing-mode Text direction for vertical scripts.
textLength, lengthAdjust Length; spacing, spacingAndGlyphs (none) Force the text to an exact width by adjusting spacing or stretching the glyphs.
text-anchor and dominant-baseline

Transforms

The transform attribute moves, rotates, scales or skews an element and everything inside it. Several functions can be listed; they are applied from right to left (the rightmost one is applied to the shape first).

Function Example Description
translate(tx [ty]) translate(50 20) Moves by tx, ty (ty defaults to 0).
scale(sx [sy]) scale(2), scale(1 -1) Scales around the origin; a negative value mirrors.
rotate(a [cx cy]) rotate(45 100 100) Rotates by a degrees, clockwise, around (cx, cy) or the origin.
skewX(a), skewY(a) skewX(20) Slants along one axis by a degrees.
matrix(a b c d e f) matrix(1 0 0 1 50 20) Any 2D transform as a matrix (here, the same as translate(50 20)).

In SVG 2, transform is also the CSS transform property, but the two syntaxes differ in ways that catch people out:

transform attribute CSS transform
Units Plain numbers: rotate(45), translate(10 20) Units required: rotate(45deg), translate(10px, 20px)
Separators Spaces or commas Commas between arguments
Rotation center Optional arguments: rotate(45 50 50) transform-origin (with transform-box: fill-box to use the shape's own box)
Default origin The user-space origin (0, 0) 0 0 of the viewBox for SVG elements, unless you set transform-box
Animation SMIL <animateTransform> or JavaScript CSS transitions and @keyframes
transform as an attribute and as CSS

In the top row, rotate(30 205 45) turns the third square around its own center (205, 45) after translate has put it there. The orange and purple rectangles use the same two functions in a different order. In the orange one, the rotation happens first (around the rectangle's own corner at 0,0) and then the rotated shape moves to (120, 110). In the purple one, the shape is moved first and then the whole coordinate system rotates around (0, 0), which swings it to a different place.

Usage notes

  • Attribute or CSS? Use attributes for the shape's intrinsic look in exported or standalone files, and CSS for theming, hover states and anything that changes. Remember that CSS always wins over the attribute.
  • Not every attribute is a property. Geometry such as x1, points and d (partly), text positions (x, y, dx), and viewBox can only be set as attributes. rx, cx, r, width and height are CSS properties in SVG 2.
  • Units. Attribute values without units are user units. In a stylesheet, write lengths with px (r: 20px); one CSS pixel equals one user unit inside the SVG's coordinate system.
  • currentColor for themes. fill="currentColor" links SVG colors to the CSS color of the surrounding HTML, so icons follow text color and dark mode automatically.
  • opacity vs fill-opacity. opacity fades the element as one flattened layer; where a stroke overlaps the fill, you don't see the fill through the stroke. Setting fill-opacity and stroke-opacity separately does show the overlap.
  • Inheritance. fill, stroke, the stroke-* properties, font properties, color, visibility and fill-rule are inherited. opacity, transform, clip-path, mask, filter and display are not, but they still affect a group's children as a whole.

Browser support

The presentation attributes in the tables are supported in every browser, except where noted (miter-clip and arcs joins, context-fill). paint-order, vector-effect="non-scaling-stroke" and transform-box work in all current browsers.