Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Stephen O'Connor
Stephen O'Connor
22,291 Points

Why does external css override inline svg styling?

I'm just working through the SVG course and Nick uses external css to override the inline styles of an SVG when the SVG code is in the HTML.

Why do the external css styles override the inline SVG styles? This usually wouldn't happen with normal inline css.

Thanks.

2 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

Because it is not an inline style it is an attribute and external styles will override inline attributes.

For example:

This is an attribute and not an inline style:

fill="red"

This is an inline style:

style="fill:red"

example:

<svg width="500" height="500">
  <text x="100" y="100" fill="red" style="fill:green">This will be green</text>
</svg>

The SVG text will be filled green because the inline style will override the fill="red" attribute. http://codepen.io/anon/pen/Ggwxmb

Laurie Williams
Laurie Williams
10,174 Points

I think it's because SVG is an attribute and CSS styles override attributes.