- How to insert a CSS style into an SVG with JavaScript?
- How to insert a CSS style into an SVG with JavaScript?
- Using an SVG and Apply CSS from Stylesheet
- DEMO
- DEMO
- Add CSS styling to SVG
- Style SVG with CSS
- Style SVG with CSS
- Correct (but possibly slow) solution
- Temporary solution
- Embedding SVG in CSS via use reference
- My first SVG
- Manipulate SVG viewbox with JavaScript (no libraries)
- Loading javascript libraries included in an appended SVG file
How to insert a CSS style into an SVG with JavaScript?
If you need that, you could try something like the following instead: Update : IE work-around as per Daniel’s comment: (The problem seems to be that IE does not support the .innerHTML for SVG elements.) Use the of a and set it to and then append all nodes in the ‘s to the element via: where is the element. Solution 1: OPTION 1 — Using generic css styles Personally I create several fill colours and simply apply the class to the svg where required. HTML CSS OPTION 2 — SVG’s targeted by unique class If your svg has several paths you can target each using other classes.
How to insert a CSS style into an SVG with JavaScript?
I try to add a CSS style sheet to an SVG. So what I have is the following svg:
And I would like to add a style sheet to this like:
var defs = document.getElementById('mydefs'); var style = document.createElementNS("http://www.w3.org/2000/svg", 'style'); style.type = 'text/css'; style.insertRule('.myclass < fill: red; >', style.cssRules.length); defs.appendChild(style);
But the insertRule (and cssRules ) seems not to be supported. Here is the (not working) codepen:
var defs = document.getElementById('mydefs'); var style = document.createElementNS("http://www.w3.org/2000/svg", 'style'); defs.appendChild(style); style.type = 'text/css'; style.sheet.insertRule('.myclass < fill: red; >', style.sheet.cssRules.length);
(BTW, you can also just use the parent document’s style sheet:)
var style = document.createElement('style'); document.head.appendChild(style); style.type = 'text/css'; style.sheet.insertRule('.myclass < fill: red; >');
The above approaches don’t show any CSS in the inspector. If you need that, you could try something like the following instead:
var defs = document.getElementById('mydefs'); var style = document.createElementNS("http://www.w3.org/2000/svg", 'style'); defs.appendChild(style); style.innerHTML = '.myclass < fill: red; >';
Update : IE work-around as per Daniel’s comment:
(The problem seems to be that IE does not support the .innerHTML for SVG elements.)
Use the .innerHTML of a and set it to and then append all nodes in the div ‘s .childNodes[0].childNodes[0] to the style element via:
Array.prototype.slice.call(div.childNodes[0].childNodes[0].childNodes).forEach(function(el) < element.appendChild(el); >);
Css class styles aren’t applied to svg, You can add the class declaration in your .svg file, directly inserting a
Using an SVG and Apply CSS from Stylesheet
So I’ve browsed several articles and questions here, but no holy grail.
I have an external file icon.svg , and I want to use it in several HTML files and have a different fill and sizes for each use.
I can’t use from what I gather since it doesn’t allow for styles from CSS sheets unless you reference that sheet from the SVG file itself, which kind of defeats the entire purpose.
The only way I found how to do this is like this:
And the CSS applies. But I can only seem to do it if I add symbol nodes in the SVG file and give them an ID, which I’d rather not do.
Also, could it be that this might be not supported in all major browsers ?
Added tags for javascript/angularjs since the solution was JS based. (see answer)
OPTION 1 — Using generic css styles
Personally I create several fill colours and simply apply the class to the svg where required.
OPTION 2 — SVG’s targeted by unique class
If your svg has several paths you can target each using other classes.
.icon < fill:#000; height:20px; width:20px; >.symbol < height:40px; width:40px; >.symbol .path1 < fill:#b00; >.symbol .path2
OPTION 3 — SVG’s as background images
DEMO
To utilise svg’s like images they can be targeted with css and even used as background images in css but each svg symbol would need to be a unique file.
To use the svg as different height and widths simply edit in css using additional classes to overwrite defaults using specificity:
OPTION 4 — Inline Svgs (My recommendation)
DEMO
Place the svg file directly in your html to reduce http requests and improve control.
Place just after your body tag at the top of your html
Then you can call the svg’s from anywhere using the svg’s id.
I ended up using and manipulating it after the SVG was rendered, in a way that would help me change the styling.
To anyone interested in the more technical details, I’ve written the process here in my blog.
I’ve created a directive in Angular to handle it, but of course you don’t have to use Angular and could take the relevant parts and use it however you want.
Edit: Wrote a bower for it, you can get it here.
How to apply a style to an embedded SVG?, Yet another option is to use the first method, to insert a style element, and then add an @import rule, e.g styleElement.textContent = «@import url(my-style.css)
Add CSS styling to SVG
I have an SVG in which i want to add a particular styling to all circle having r=»5″
I have tried this but does not work
var allElements = document.getElementsByClassName("bubble-plot-chart-circle"); for(var i = 0; i < allElements.length; i++) < var element = allElements[i]; if(element.getAttribute("r") === "5") < // it takes the initial inline style which was applied at the time of crating the SVG element.setAttribute("opacity", "0 !important");// does not work for SVG element.addClass("test"); // does not work for SVG >>
can any one help me on this as i am new to SVG
I used querySelectorAll to get the filtered elements with provided class name and attribute.
var allElements = document.querySelectorAll(".bubble-plot-chart-circle[r='5']"); // filtering as required for(var i = 0; i < allElements.length; i++) < var element = allElements[i]; element.setAttribute("opacity", "0"); // !important is invalid in presentation attribute (check comment above) element.classList.add("test"); // JavaScript's method instead of jquery's addClass method >
and also addClass method is not available for JavaScript DOM object. It is available for jQuery object. If you want to use addClass method, convert the DOM object to jquery object as shown below:
var jqueryElement = $(element); jqueryElement.addClass('test');
And also, you can do the same selection using CSS as well:
Its working. See this. Use element.classList.add() instead:
Actually your code does work. What doesn’t work, is your attribute.
I think you should look at https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-opacity
var allElements = document.getElementsByClassName("bubble-plot-chart-circle"); for(var i = 0; i