How to style one particular SVG path in CSS?

前端 未结 3 524
北荒
北荒 2021-01-07 00:22

Is it possible to style differently a same SVG symbol with 2 distinct CSS classes. Like in this jsfiddle

3条回答
  •  自闭症患者
    2021-01-07 00:54

    You can style the parent div's colors, and use fill: currentColor to use that color for the appropriate SVG element.

    So this CSS:

    div > svg {
      width: 55px;
      height: 55px;
    }
    
    svg g g > path:nth-of-type(1) {
      fill: currentColor;
    }
    
    div:nth-of-type(even) {
      color: red;
    }
    
    div:nth-of-type(odd) {
      color: blue;
    }
    

    … used with this HTML:

    … will look like this:

    enter image description here

    Fiddle

提交回复
热议问题