Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

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.

Example

#

A class attribute styling a <svg> element.

<style>
  .svg-class { background:aliceblue;width:100px;height:100px; }
</style>

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

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.


Syntax

<svg class="classnames">

Values

#

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.



<style>
  .bg-aliceblue { background: aliceblue; }
  .bg-steelblue { background: steelblue; }
</style>

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

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

<script>
  let toggle = () => {
     let element = document.getElementById("mysvg");
     element.classList.toggle("bg-steelblue");
  }
</script>

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:

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>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides