img src SVG changing the styles with CSS

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

html

\"Logo\"

css

.logo-img path {
           


        
相关标签:
22条回答
  • 2020-11-22 02:07

    Use filters to transform to any color.

    I recently found this solution, and hope somebody might be able to use it. Since the solution uses filters, it can be used with any type of image. Not just svg.

    If you have a single-color image that you just want to change the color of, you can do this with the help of some filters. It works on multicolor images as well of course, but you can't target a specific color. Only the whole image.

    The filters came from the script proposed in How to transform black into any given color using only CSS filters If you want to change white to any color, you can adjust the invert value in each filter.

    .startAsBlack{
      display: inline-block;
      width: 50px;
      height: 50px;
      background: black;
    }
    
    .black-green{
      filter: invert(43%) sepia(96%) saturate(1237%) hue-rotate(88deg) brightness(128%) contrast(119%);
    }
    
    .black-red{
      filter: invert(37%) sepia(93%) saturate(7471%) hue-rotate(356deg) brightness(91%) contrast(135%);
    }
    
    .black-blue{
      filter: invert(12%) sepia(83%) saturate(5841%) hue-rotate(244deg) brightness(87%) contrast(153%);
    }
    
    .black-purple{
      filter: invert(18%) sepia(98%) saturate(2657%) hue-rotate(289deg) brightness(121%) contrast(140%);
    }
    Black to any color: <br/>
    <div class="startAsBlack black-green"></div>
    <div class="startAsBlack black-red"></div>
    <div class="startAsBlack black-blue"></div>
    <div class="startAsBlack black-purple"></div>

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

    2018: If you want a dynamic color, do not want to use javascript and do not want an inline SVG, use a CSS variable. Works in Chrome, Firefox and Safari. edit: and Edge

    <svg>
        <use xlink:href="logo.svg" style="--color_fill: #000;"></use>
    </svg>
    

    In your SVG, replace any instances of style="fill: #000" with style="fill: var(--color_fill)".

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

    The answer from @Praveen is solid.

    I couldn't get it to respond in my work, so I made a jquery hover function for it.

    CSS

    .svg path {
       transition:0.3s all !important;
    }
    

    JS / JQuery

    // code from above wrapped into a function
    replaceSVG();
    
    // hover function
    // hover over an element, and find the SVG that you want to change
    $('.element').hover(function() {
        var el = $(this);
        var svg = el.find('svg path');
        svg.attr('fill', '#CCC');
    }, function() {
        var el = $(this);
        var svg = el.find('svg path');
        svg.attr('fill', '#3A3A3A');
    });
    
    0 讨论(0)
  • 2020-11-22 02:11

    open the svg icon in your code editor and add a class after the path tag:

    <path class'colorToChange' ...
    

    You can add class to svg and change the color like this:

    codepen

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