Styling SVG with CSS

<style>

The <style> SVG element allows style sheets to be embedded directly within SVG content.

The SVG's style element has the same attributes as the corresponding element in HTML.

Example:

<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  <style>
    circle {
      fill: gold;
      stroke: maroon;
      stroke-width: 2px;
    }
  </style>

  <circle cx="5" cy="5" r="4" />
</svg>

Attributes:

type
This attribute defines type of the style sheet language to use as a media type string. Value type: media-type; Default value: text/css; Animatable: no
media
This attribute defines to which media the style applies. Value type: media-query-list; Default value: all; Animatable: no
title
This attribute is the title of the style sheet which can be used to switch between alternate style sheets. Value type: string; Default value: none; Animatable: no

...

Properties shared between CSS and SVG

Font properties:

Text Properties:

Color properties:

SVG Color

You can specify the stroke color in a variety of ways:

Internal Stylesheets

You don't need to place your styles inside each SVG element; you can create an internal stylesheet to collect commonly-used styles that you can apply to all occurrences of a particular element, or use as named classes to apply to individual elements. The following example sets up an internal stylesheet that will draw all circles in a blue double-thick dashed line with a light yellow interior. We have placed the stylesheet within a <defs> element, which we will discuss later.

The example then draws several circles. The circles in the second row have inline styles that override the specification in the internal stylesheet.

<svg width="200px" height="200px" viewBox="0 0 200 200">
<defs>
<style type="text/css">
  circle {
    fill: #ffc;
    stroke: blue;
    stroke-width: 2;
    stroke-dasharray: 5 3
  }</style>
</defs>
<circle cx="20" cy="20" r="10"/>
<circle cx="60" cy="20" r="15"/>
<circle cx="20" cy="60" r="10" style="fill: #cfc"/>
<circle cx="60" cy="60" r="15" style="stroke-width: 1; stroke-dasharray: none;"/>
</svg>