SVG: Use Attributes or CSS to style?

前端 未结 4 1784
无人共我
无人共我 2021-02-04 02:09

In HTML it is recommended to seperate Content from Style, thus you should create external CSS-Files for your styles. As I am just getting started with SVG I now wonder: Does thi

4条回答
  •  没有蜡笔的小新
    2021-02-04 02:32

    I would generally prefer to because it's shorter and easily to manipulate with, for example, getAttributeNS(null, "fill").

    But over that I would prefer using a separate style element, just as with HTML, e.g:

          
    

    Which has all the same advantages of using CSS, such as making it easy to change the stlye of lots of elements at once.

    You can also put your CSS in an external file and add:

    
    

    Before the svg element.

提交回复
热议问题