HTML <svg> style Attribute

The style attribute on a <svg> tag assigns a unique style to the svg image element. A style contains one or more CSS property/value pairs that define the appearance of the svg image.

Example

#

A style attribute on an <svg> image.

<svg style="background:aliceblue;width:100px;height:100px;">
  <circle cx="50" cy="50" r="40" stroke="steelblue" 
          stroke-width="2" fill="lightblue" />
</svg>

Using style

The style attribute specifies the style, i.e. look and feel, of the <svg> element.

A style contains any number of CSS property/value pairs, separated by semicolons (;).

The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.

This inline styling affects the current <svg> element only.


Syntax

<svg style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

A style attribute on an <svg> image.
Clicking the button toggles the background color.



<svg id="mysvg" style="background:aliceblue;width:100px;height:100px;">
  <circle cx="50" cy="50" r="40" stroke="steelblue" 
          stroke-width="2" fill="lightblue" />
</svg>

<br /><br />
<button onclick="toggle();">Toggle style</button>

<script>
  let toggle = () => {
     let element = document.getElementById("mysvg");

     if (element.style.backgroundColor === "aliceblue") {
       element.style.backgroundColor = "steelblue";
    } else {
       element.style.backgroundColor = "aliceblue";
    }
  }
</script>

Code explanation

The style attribute assigns a background color to the <svg> element.

Clicking the button calls JavaScript which toggles the background to another color.


Browser support

Here is when style support started for each browser:

Chrome
4.0 Jan 2010
Firefox
3.0 Jun 2008
IE/Edge
9.0 Mar 2011
Opera
10.1 Jun 2010
Safari
3.2 Nov 2008

You may also like

 Back to <svg>
Guides