SVGStyleElement
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
TheSVGStyleElement interface corresponds to the SVG<style> element.
In this article
Instance properties
This interface also inherits properties from its parent interface,SVGElement.
SVGStyleElement.typeDeprecatedA string corresponding to the
typeattribute of the given element.SVGStyleElement.mediaA string corresponding to the
mediaattribute of the given element.SVGStyleElement.titleA string corresponding to the
titleattribute of the given element.SVGStyleElement.sheetRead onlyReturns the
CSSStyleSheetobject associated with the given element, ornullif there is none.SVGStyleElement.disabledA boolean value indicating whether or not the associated stylesheet is disabled.
Instance methods
This interface doesn't implement any specific methods, but inherits methods from its parent interface,SVGElement.
Examples
>Dynamically adding an SVG style element
To dynamically create an SVG style element (SVGStyleElement), you need to useDocument.createElementNS(), specifying astyle element in the SVG namespace.
Note:Document.createElement() can't be used to create SVG style elements (it returns anHTMLStyleElement).
Given the following SVG element:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <circle cx="50" cy="50" r="25" /></svg>You can create an SVG style element as shown:
// Get the SVG element object by tag nameconst svg = document.querySelector("svg");// Create the `style` element in the SVG namespaceconst style = document.createElementNS("http://www.w3.org/2000/svg", "style");const node = document.createTextNode("circle { fill: red; }");style.appendChild(node);// Append the style element to the SVG elementsvg.appendChild(style);Accessing an existing SVG style
You can access an SVG style element that was defined in HTML (or an SVG file) using the normal HTML methods for getting tags, ids, and so on.These include:Document.getElementsByTagName(),Document.getElementById(),Document.querySelector(),Document.querySelectorAll(), and so on.
For example, consider the HTML below that defines an SVG file with a style element.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <style> circle { fill: gold; stroke: green; stroke-width: 3px; } </style> <circle cx="50" cy="50" r="25" /></svg>To fetch the firststyle element in the firstsvg element, you might useDocument.querySelector() as shown below.
const svg = document.querySelector("svg");const style = svg.querySelector("style");Alternatively, you can could useDocument.getElementById(), specifying the tag id:
const svg = document.querySelector("svg");const style = svg.getElementById("circle_style_id");Or just get the element from document by id (in this case usingdocument.querySelector()):
const style = document.querySelector("#circle_style_id");Getting and setting properties
This example demonstrates how to get and set the properties of a style element, which in this case was specified in an SVG definition.
HTML
The HTML contains an SVG definition for a<circle> with a<style> element, along with an HTML<button> element that will be used to enable and disable the style, and an HTML<textarea> element for logging the property values.
<button>Disable</button><textarea rows="6" cols="90"></textarea><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <style media="(width >= 600px)"> circle { fill: gold; stroke: green; stroke-width: 3px; } </style> <circle cx="60" cy="60" r="50" /></svg>Note that above we have set themedia attribute on thestyle tag.We have not settype as it is deprecated, ordisabled because there is no such attribute (only the property on the element).
JavaScript
The code below gets thestyle element (anSVGStyleElement) using its id.
const svg = document.querySelector("svg");const style = svg.getElementById("circle_style_id");We then add a function to log the style properties.This is called after initialization, whenever the frame resizes, and if the button is pressed.
// Get logging text areaconst log = document.getElementById("log");function setLogText() { // Log current values of properties log.value = `style.media: ${style.media} (frame width: ${window.innerWidth})\n`; // 'all' by default log.value += `style.title: ${style.title}\n`; // no default value log.value += `style.disabled: ${style.disabled}\n`; // 'false' by default log.value += `style.type: ${style.type}\n`; // deprecated (do not use) log.value += `style.sheet.rules[0].cssText: ${style.sheet.rules[0].cssText}\n`;}// Log initial property valuessetLogText();// Log when the frame resizesaddEventListener("resize", () => { setLogText();});Last of all we set an event handler for the button.When the button is clicked thedisabled property is toggled.This also updates the log and the button text.
const button = document.querySelector("button");button.addEventListener("click", () => { style.disabled = !style.disabled; button.textContent = style.disabled ? "Enable" : "Disable"; // Log after button presses setLogText();});Result
The result is shown below.Toggle the button to enable and disable the SVG style element.If the SVG style is not disabled, you can also resize the window width to see the effect of themedia property on the style when the frame holding the live example is 600px wide.
Specifications
| Specification |
|---|
| Scalable Vector Graphics (SVG) 2> # InterfaceSVGStyleElement> |