Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svg styles interfering with each other

Tags:

css

svg

I have a bunch of logo's, generated with illustrator, that I would like to embed in my website directly. The svgs all have a <style> element where the styles are defined inside the svg element, something like this:

<svg>
  <style>
    .st1 { fill:#ff00ff; }
    .st2 { fill:#ff3421; }
    /* ... and so on */
  </style>
    <!-- svg paths and shapes -->
</svg>

The problem is that these styles interfer with each other. So if the last images defines .st21 {fill:#555555} this style is applied to all path with class="st21", including paths from all previously loaded svg images.

In another thread somebody suggested to wrap my svg-xml with an <object> tag, that doesn't seem to work.

How can I make sure that inline SVG styles are not interfering with each other, without touching the actual SVG code?

here's a pen to illustrate the problem: https://codepen.io/pwkip/pen/RLPgpW

like image 943
Jules Colle Avatar asked Sep 15 '17 08:09

Jules Colle


People also ask

How do I stop SVG from scaling?

Browser set min-width and min-height for svg elements to "auto" by default. So when it render your svg and see that you have not set the min-width , then it will shrink it as much as it can to fit more text. So you have to set min-width attribute for svg element.

Can svgs have pseudo elements?

SVG content can be added using these pseudo-elements by following the methods described below: Method 1: Using the background-image property: The background-image property is used to set one or more background images to an element.

Do SVG IDS need to be unique?

The ID must be unique within the node tree, must not be an empty string, and must not contain any whitespace characters. Note: You should avoid the use of id values that would be parsed as an SVG view specification (e.g., MyDrawing.

Does CSS affect SVG?

There are many Scalable Vector Graphics (SVG), but only certain attributes can be applied as CSS to SVG. Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others are already shared in CSS, such as font-size or opacity .


1 Answers

I would suggest to export svg with appropriate CSS properties in the first place. During export from Illustrator choose :style attributes it would be something like this in svg:

<path style="fill: red"></path>

It could increase your file size but it definitely do the job. I found a nice explanation here

like image 145
Andreew4x4 Avatar answered Oct 15 '22 12:10

Andreew4x4