img src SVG changing the styles with CSS

后端 未结 22 1960
予麋鹿
予麋鹿 2020-11-22 01:16

html

\"Logo\"

css

.logo-img path {
           


        
相关标签:
22条回答
  • 2020-11-22 01:58

    Know this is an old question but recently we came across the same issue, and we solved it from the server side. This is a php specific answer but I am positive that other envs have something similar. instead of using the img tag you render the svg as svg from the get-go.

    public static function print_svg($file){
        $iconfile = new \DOMDocument();
        $iconfile->load($file);
        $tag = $iconfile->saveHTML($iconfile->getElementsByTagName('svg')[0]);
        return $tag;
    }
    

    now when you render the file you will get complete inline svg

    0 讨论(0)
  • 2020-11-22 01:59

    This answer is based on answer https://stackoverflow.com/a/24933495/3890888 but with a plain JavaScript version of the script used there.

    You need to make the SVG to be an inline SVG. You can make use of this script, by adding a class svg to the image:

    /*
     * Replace all SVG images with inline SVG
     */
    document.querySelectorAll('img.svg').forEach(function(img){
        var imgID = img.id;
        var imgClass = img.className;
        var imgURL = img.src;
    
        fetch(imgURL).then(function(response) {
            return response.text();
        }).then(function(text){
    
            var parser = new DOMParser();
            var xmlDoc = parser.parseFromString(text, "text/xml");
    
            // Get the SVG tag, ignore the rest
            var svg = xmlDoc.getElementsByTagName('svg')[0];
    
            // Add replaced image's ID to the new SVG
            if(typeof imgID !== 'undefined') {
                svg.setAttribute('id', imgID);
            }
            // Add replaced image's classes to the new SVG
            if(typeof imgClass !== 'undefined') {
                svg.setAttribute('class', imgClass+' replaced-svg');
            }
    
            // Remove any invalid XML tags as per http://validator.w3.org
            svg.removeAttribute('xmlns:a');
    
            // Check if the viewport is set, if the viewport is not set the SVG wont't scale.
            if(!svg.getAttribute('viewBox') && svg.getAttribute('height') && svg.getAttribute('width')) {
                svg.setAttribute('viewBox', '0 0 ' + svg.getAttribute('height') + ' ' + svg.getAttribute('width'))
            }
    
            // Replace image with new SVG
            img.parentNode.replaceChild(svg, img);
    
        });
    
    });
    

    And then, now if you do:

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

    Or may be:

    .logo-img path {
      background-color: #000;
    }
    

    JSFiddle: http://jsfiddle.net/erxu0dzz/1/

    0 讨论(0)
  • 2020-11-22 02:00

    Since SVG is basically code, you need just contents. I used PHP to obtain content, but you can use whatever you want.

    <?php
    $content    = file_get_contents($pathToSVG);
    ?>
    

    Then, I've printed content "as is" inside a div container

    <div class="fill-class"><?php echo $content;?></div>
    

    To finnaly set rule to container's SVG childs on CSS

    .fill-class > svg { 
        fill: orange;
    }
    

    I got this results with a material icon SVG:

    Mozilla Firefox 59.0.2 (64-bit) Linux

    Google Chrome66.0.3359.181 (Build oficial) (64 bits) Linux

    Opera 53.0.2907.37 Linux

    0 讨论(0)
  • 2020-11-22 02:01

    If you are just switching the image between the real color and the black-and-white, you can set one selector as:

    {filter:none;}

    and another as:

    {filter:grayscale(100%);}
    
    0 讨论(0)
  • 2020-11-22 02:02

    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

    0 讨论(0)
  • This might be helpful for people using PHP in combination with .svg images that they want to manipulate with CSS.

    You can't overwrite properties inside a img tag with CSS. But when the svg source code is embedded in the HTML you surely can. I like to resolve this issue with a require_once function where I include a .svg.php file. It's like importing an image but you can still overwrite styles with CSS!

    First include the svg file:

    <?php require_once( '/assets/images/my-icon.svg.php' ); ?>
    

    And it includes this icon for example:

    <svg xmlns="http://www.w3.org/2000/svg" width="20.666" height="59.084" viewBox="0 0 20.666 59.084"><g transform="translate(-639.749 -3139)"><path d="M648.536,3173.876c0-2.875-1.725-3.8-3.471-3.8-1.683,0-3.49.9-3.49,3.8,0,3,1.786,3.8,3.49,3.8C646.811,3177.676,648.536,3176.769,648.536,3173.876Zm-3.471,2.341c-.883,0-1.437-.513-1.437-2.341,0-1.971.615-2.381,1.437-2.381.862,0,1.438.349,1.438,2.381,0,1.907-.616,2.339-1.438,2.339Z" fill="#142312"/><path d="M653.471,3170.076a1.565,1.565,0,0,0-1.416.9l-6.558,13.888h1.2a1.565,1.565,0,0,0,1.416-.9l6.559-13.887Z" fill="#142312"/><path d="M655.107,3177.263c-1.684,0-3.471.9-3.471,3.8,0,3,1.766,3.8,3.471,3.8,1.745,0,3.49-.9,3.49-3.8C658.6,3178.186,656.851,3177.263,655.107,3177.263Zm0,6.139c-.884,0-1.438-.514-1.438-2.34,0-1.972.617-2.381,1.438-2.381.862,0,1.437.349,1.437,2.381,0,1.909-.616,2.34-1.437,2.34Z" fill="#142312"/><path d="M656.263,3159.023l-1.49-14.063a1.35,1.35,0,0,0,.329-.293,1.319,1.319,0,0,0,.268-1.123l-.753-3.49a1.328,1.328,0,0,0-1.306-1.054h-6.448a1.336,1.336,0,0,0-1.311,1.068l-.71,3.493a1.344,1.344,0,0,0,.276,1.112,1.532,1.532,0,0,0,.283.262l-1.489,14.087c-1.7,1.727-4.153,4.871-4.153,8.638v28.924a1.339,1.339,0,0,0,1.168,1.49,1.357,1.357,0,0,0,.17.01h17.981a1.366,1.366,0,0,0,1.337-1.366v-29.059C660.414,3163.893,657.963,3160.749,656.263,3159.023Zm-8.307-17.349h4.274l.176.815H647.79Zm9.785,43.634v10.1H642.434v-17.253a4.728,4.728,0,0,1-2.028-4.284,4.661,4.661,0,0,1,2.028-4.215v-2c0-3.162,2.581-5.986,3.687-7.059a1.356,1.356,0,0,0,.4-.819l1.542-14.614H652.1l1.545,14.618a1.362,1.362,0,0,0,.4.819c1.109,1.072,3.688,3.9,3.688,7.059v9.153a5.457,5.457,0,0,1,0,8.5Z" fill="#142312"/></g></svg>
    

    Now we can easily change the fill color like this with CSS:

    svg path {
      fill: blue;
    }
    

    I first tried to solve this problem with file_get_contents() but the solution above is much faster.

    0 讨论(0)
提交回复
热议问题