A style attribute on a <svg> tag assigns a unique style to the element.
Its value is CSS that defines the appearance of the svg image.
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>
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.
<svg style="CSS-styles">
Value | Description |
---|---|
CSS-styles | One or more CSS property/value pairs separated by semicolons (;). |
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>
The style attribute assigns a background color to the <svg> element.
Clicking the button calls JavaScript which toggles the background to another color.
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 |
Back to <svg>