question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS not applied to SVGs

See original GitHub issue

See this demo for an illustrated example.

When inline SVGs are styled using CSS the properties are not rendered.

My example above shows how an SVG that includes a defined fill property as:

<path fill="red" ...

But is then overridden in the browser using the CSS:

path {
    fill: green;
}

will render in the browser as a green path but once converted to a canvas using html2canvas the property is lost and is instead rendered as red.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

6reactions
PawelBaranowskicommented, May 27, 2020

@Jerther you saved my day, thanks a lot! I confirm this works, here’s a Typescript version if anyone needs it:

function applyStyleToSvg(container: Element | Document) {
    for (let svg of Array.from(container.getElementsByTagName('svg'))) {
        for (let element of Array.from(svg.getElementsByTagName('*')) as Array<SVGElement>) {
            const computedStyle = getComputedStyle(element);
            for (let property of Array.from(computedStyle)) {
                element.style.setProperty(property, computedStyle.getPropertyValue(property));
            }
        }
    }
}
5reactions
Jerthercommented, Apr 29, 2020

Here’s a version of the fix that does not rely on external libraries:

function applyStyleToSvgTrees(container) {
  for (let svg of container.getElementsByTagName('svg')) {
    for (let el of svg.getElementsByTagName('*')) {
      s = getComputedStyle(el);
      for (let key of s) {
        let prop = key.replace(/\-([a-z])/g, v => v[1].toUpperCase());
        el.style[prop] = s[key];
      }
    }
  }
}


html2canvas(document.body, {
  onclone: (clonedDoc) => {
    applyStyleToSvgTrees(clonedDoc);
  },
}).then(canvas => {
  document.body.appendChild(canvas);
});

Try here with jsFiddle

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style SVG with external CSS? - Stack Overflow
Let's say you have 10 SVGs you want to style. Now you need to copy in a reference to the CSS into all...
Read more >
SVG Properties and CSS
There are many Scalable Vector Graphics (SVG), but only certain attributes can be applied as CSS to SVG. Presentation attributes are used to ......
Read more >
SVG and CSS - SVG: Scalable Vector Graphics | MDN
This page illustrates the application of CSS to the specialized language for creating graphics: SVG.
Read more >
Styling — SVG 2
Elements in an SVG document can be styled using CSS. Most visual characteristics and some aspects of element geometry are controlled using CSS...
Read more >
Styling And Animating SVGs With CSS - Smashing Magazine
Most CSS selectors can be used to select SVG elements. In addition to the general type, class and ID selectors, SVGs can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found