HTML <svg> class Attribute

The class attribute assigns one or more classnames to the <svg> tag.

Classnames are defined in a stylesheet or in a local <style> element.

Classes, i.e. classnames, are used to style elements.



A class attribute styling a <svg> element.

  .svg-class { background:aliceblue;width:100px;height:100px; }

<svg class="svg-class">
  <circle cx="50" cy="50" r="40" stroke="steelblue" 
          stroke-width="2" fill="lightblue" />

Using class

Classes (i.e. classnames) are used for styling the svg element.

Multiple classnames are separated by a space.

JavaScript uses classes to access elements by classname.

Tip:  class is a global attribute that can be applied to any HTML element.


<svg class="classnames">



Value Description
classnames One or more space-separated class names.

More Examples

A class attribute styling a <svg> element.
Clicking the button toggles a classname that changes the background color.

  .bg-aliceblue { background: aliceblue; }
  .bg-steelblue { background: steelblue; }

<svg id="mysvg" class="bg-aliceblue" width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="steelblue" 
          stroke-width="2" fill="lightblue" />

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

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

Code explanation

Two CSS classes are defined in the <style> element.

The class attribute in <svg> assigns one classname.

Repeatedly clicking the button toggles another class, changing the background color of the <svg>.

Browser support

Here is when class support started for each browser:

4.0 Jan 2010
3.0 Jun 2008
9.0 Mar 2011
10.1 Jun 2010
3.2 Nov 2008

You may also like

 Back to <svg>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023

Quick question: what's your favorite/least favorite part of Dofactory?