img src SVG changing the styles with CSS

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

html

\"Logo\"

css

.logo-img path {
           


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

    I suggest to select your color , and go to this pen https://codepen.io/sosuke/pen/Pjoqqp it will convert HEX to css filter eg:#64D7D6

    equal

    filter: invert(88%) sepia(21%) saturate(935%) hue-rotate(123deg) brightness(85%) contrast(97%);
    

    the final snippet

    .filterit{
    width:270px;
    filter: invert(88%) sepia(21%) saturate(935%) hue-rotate(123deg) brightness(85%) contrast(97%);
    }
    <img src="https://www.flaticon.com/svg/static/icons/svg/1389/1389029.svg"
    class="filterit
    
    />

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

    For me, my svgs looked different when having them as img and svg. So my solution converts the img to csv, changes styles internally and back to img (although it requires a bit more work), I believe "blob" also has better compatibility than the upvoted answer using "mask".

      let img = yourimgs[0];
      if (img.src.includes(".svg")) {
        var ajax = new XMLHttpRequest();
        ajax.open("GET", img.src, true);
        ajax.send();
        ajax.onload = function (e) {
         
          svg = e.target.responseText;
    
         
          svgText = "";
          //change your svg-string as youd like, for example
          // replacing the hex color between "{fill:" and ";"
          idx = svg.indexOf("{fill:");
          substr = svg.substr(idx + 6);
          str1 = svg.substr(0, idx + 6);
          str2 = substr.substr(substr.indexOf(";"));
          svgText = str1 + "#ff0000" + str2;
          
    
          let blob = new Blob([svgText], { type: "image/svg+xml" });
          let url = URL.createObjectURL(blob);
          let image = document.createElement("img");
          image.src = url;
          image.addEventListener("load", () => URL.revokeObjectURL(url), {
            once: true,
          });
          img.replaceWith(image);
        };
      }
    
    0 讨论(0)
  • 2020-11-22 01:47

    Try pure CSS:

    .logo-img {
      // to black
      filter: invert(1);
      // or to blue
      // filter: invert(1) sepia(1) saturate(5) hue-rotate(175deg);
    }
    

    more info in this article https://blog.union.io/code/2017/08/10/img-svg-fill/

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

    You will first have to inject the SVG into the HTML DOM.

    There is an open source library called SVGInject that does this for you. It uses the onload attribute to trigger the injection.

    Here is a minimal example using SVGInject:

    <html>
      <head>
        <script src="svg-inject.min.js"></script>
      </head>
      <body>
        <img src="image.svg" onload="SVGInject(this)" />
      </body>
    </html>
    

    After the image is loaded the onload="SVGInject(this) will trigger the injection and the <img> element will be replaced by the contents of the SVG file provided in the src attribute.

    It solves several issues with SVG injection:

    1. SVGs can be hidden until injection has finished. This is important if a style is already applied during load time, which would otherwise cause a brief "unstyled content flash".

    2. The <img> elements inject themselves automatically. If you add SVGs dynamically, you don't have to worry about calling the injection function again.

    3. A random string is added to each ID in the SVG to avoid having the same ID multiple times in the document if an SVG is injected more than once.

    SVGInject is plain Javascript and works with all browsers that support SVG.

    Disclaimer: I am the co-author of SVGInject

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

    The main problem in your case is that you are importing the svg from an <img> tag which will hide the SVG structure.

    You need to use the <svg> tag in conjunction with the <use> to get the desired effect. To make it work, you need to give an id to the path you want to use in the SVG file <path id='myName'...> to then be able to retrieve them from the <use xlink:href="#myName"/> tag. Try the snipped below.

    .icon {
      display: inline-block;
      width: 2em;
      height: 2em;
      transition: .5s;
      fill: currentColor;
      stroke-width: 5;
      }
      .icon:hover {
        fill: rgba(255,255,255,0);
        stroke: black;
        stroke-width: 2;
        }
    
    .red {
      color: red;
      }
    
    .blue {
      color: blue;
      }
    <svg width="0" height="0">
      <defs>
        <path id="home" d="M100 59.375l-18.75-18.75v-28.125h-12.5v15.625l-18.75-18.75-50 50v3.125h12.5v31.25h31.25v-18.75h12.5v18.75h31.25v-31.25h12.5z"/>
    </svg>
    
      
      <span class="icon red">
              <svg viewbox="0 0 100 100">
                <use xlink:href="#home"/>
              </svg>
            </span>
      
        <span class="icon blue">
              <svg viewbox="0 0 100 100">
                <use xlink:href="#home"/>
              </svg>
            </span>

    Note that you can put any URL before the fragment # if you want to load the SVG from an external source (and not embed it into your HTML). Also, usually you do not specify the fill into the CSS. It's better to consider using fill:"currentColor" within the SVG itself. The corresponding element's CSS color value will then be used in place.

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

    Directly to svg fill css will not work you can use as below

    <style>
    svg path {
        fill: red;
    }
    </style>
    <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>
    

    This worked for me

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