How to style SVG element?

前端 未结 4 798
半阙折子戏
半阙折子戏 2021-02-02 05:58

I have some SVG elements grouped together in a element. I just want to style that element to show grouping of elements. Like I want

4条回答
  •  攒了一身酷
    2021-02-02 06:31

    I know its long after this question was asked and answered - and I am sure that the accepted solution is right, but the purist in me would rather not add an extra element to the SVG when I can achieve the same or similar with straight CSS.

    Whilst it is true that you cannot style the g container element in most ways - you can definitely add an outline to it and style that - even changing it on hover of the g - as shown in the snippet.

    It not as good in one regard as the other way - you can put the outline box around the grouped elements - but not a background behind it. Sot its not perfect and won't solve the issue for everyone - but I would rather have the outline done with css than have to add extra elements to the code just to provide styling hooks.

    And this method definitely allows you to show grouping of related objects in your SVG's.

    Just a thought.

    g {
        outline: solid 3px blue;
        outline-offset: 5px;
      }
    
    g:hover {
     outline-color: red
    }
    
      
        
             
      
    

提交回复
热议问题