Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

img src SVG changing the styles with CSS

Tags:

css

image

svg

html

<img src="logo.svg" alt="Logo" class="logo-img"> 

css

.logo-img path {   fill: #000; } 

The above svg loads and is natively fill: #fff but when I use the above css to try change it to black it doesn't change, this is my first time playing with SVG and I am not sure why it's not working.

like image 228
ngplayground Avatar asked Jul 24 '14 12:07

ngplayground


People also ask

Can you change SVG with CSS?

You can't change the color of an image that way. If you load SVG as an image, you can't change how it is displayed using CSS or Javascript in the browser. If you want to change your SVG image, you have to load it using <object> , <iframe> or using <svg> inline.

Can I change SVG image color in CSS?

Edit your SVG file, add fill="currentColor" to svg tag and make sure to remove any other fill property from the file. Note that currentColor is a keyword (not a fixed color in use). After that, you can change the color using CSS, by setting the color property of the element or from it's parent.

Which property is used to change the color of an SVG using CSS?

The fill property is a presentation attribute used to set the color of a SVG shape.

Can we add style to SVG?

The SVG <style> element allows style sheets to be embedded directly within SVG content.


2 Answers

You could set your SVG as a mask. That way setting a background-color would act as your fill color.

HTML

<div class="logo"></div> 

CSS

.logo {   background-color: red;   -webkit-mask: url(logo.svg) no-repeat center;   mask: url(logo.svg) no-repeat center; } 

JSFiddle: https://jsfiddle.net/KuhlTime/2j8exgcb/
MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/mask

Please check whether your browser supports this feature: https://caniuse.com/#search=mask

like image 142
André Kuhlmann Avatar answered Nov 02 '22 06:11

André Kuhlmann


If your goal is just to change the color of the logo, and you don't necessarily NEED to use CSS, then don't use javascript or jquery as was suggested by some previous answers.

To precisely answer the original question, just:

  1. Open your logo.svg in a text editor.

  2. look for fill: #fff and replace it with fill: #000

For example, your logo.svg might look like this when opened in a text editor:

<svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">   <path d="M0 0h24v24H0z" fill="none"/>   <path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z" fill="#fff"/> </svg> 

... just change the fill and save.

like image 38
Rowe Morehouse Avatar answered Nov 02 '22 06:11

Rowe Morehouse