Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify an SVG as a background image and ALSO style the SVG in CSS?

Tags:

css

svg

Is it possible to specify an SVG as a background image and ALSO style the SVG in the same CSS file?

I'm very comfortable placing, scaling and clipping SVG images as either single files or sprites, but I haven't been able to work out if it possible to style the SVG within the same CSS file as sets it as a background image.

In pseudo CSS i'd like to do the following to vary the colour of simple shape based on the class of the parent element:

element1 {  background-image(icon.svg); }  element1.black .svg-pathclass {  fill: #000000; }  element1.white .svg-pathclass {  fill: #ffffff; } 

obviously this assumes a path in the SVG having class .svg-pathclass

is this possible?

like image 813
JSDBroughton Avatar asked Sep 09 '11 11:09

JSDBroughton


People also ask

Can I use SVG as background image in CSS?

SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.

Can SVG be styled with CSS?

Not only does it mean that SVG properties can be styled using CSS as presentation attributes or in style sheets, but this also can be applied to CSS pseudo-classes such as :hover or :active . SVG 2 also introduces more presentation attributes that can be used as styling properties.

How do I make SVG background transparent in CSS?

The SVG way would be to set the stroke to none , or alternatively set the stroke-opacity to 0 . You also don't set any value for fill on the <rect> element and the default is black. For a transparent rect you want to add fill="none" .

Does an SVG file have a background?

A SVG always have a transparent background as long as you do not insert a rect or something that is filling the whole graphic or using CSS to set a background color for the root element.


1 Answers

No, this is not possible. The SVG has to be prepared in one document (which may be a data URI or an externally referenced file) and then used as a background in another file.

like image 111
Phrogz Avatar answered Sep 26 '22 22:09

Phrogz