I have and SVg and adding tag inside svg and using nested css delcration syntax and wehn using &
it gives error of some XMl error ;
first see this html example
<style type="text/css">
:root {
--color-red: #FF5733;
}
div {
background-color: yellow;
.card {
border: 2px solid grey;
height:2rem;
width:2rem;
margin: 1rem;
background-color: var(--color-red);
&:nth-child(odd) {
background-color: blue;
}
}
}
</style>
<div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<section>
<p>Working SVG but not applying pseudo-class</p>
<svg width="600" height="90" viewBox="0 0 600 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect class="card" x="42" y="50" width="16" height="16" />
<rect class="card" x="120" y="20" width="16" height="16" />
<style type="text/css">
:root {
--color-red: #FF5733;
}
rect {
.card {
fill: var(--color-red);
:nth-child(odd) {
fill: blue;
}
}
}
</style>
</svg>
</section>
<section>
<p> works when nested under svg `&` does not works </p>
<svg width="600" height="90" viewBox="0 0 600 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect class="card" x="42" y="50" width="16" height="16" />
<rect class="card" x="120" y="20" width="16" height="16" />
<style type="text/css">
:root {
--color-red: #FF5733;
}
svg {
.card {
fill: var(--color-red);
}
:nth-child(odd) {
fill: blue;
}
}
</style>
</svg>
</section>
which works and if we remove &
ahead to :nth-child
then does not work as expected;
now same thing trying in svg ( and my requirement is every odd rectacle will be blue and otherwise red ( note: background-color
replace by fill
)
<svg width="600" height="90" viewBox="0 0 600 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect class="card" x="42" y="50" width="16" height="16" />
<rect class="card" x="120" y="20" width="16" height="16" />
<style type="text/css">
:root {
--color-red: #FF5733;
}
svg {
.card {
fill: var(--color-red);
:nth-child(odd) {
fill: blue;
}
}
}
</style>
</svg>
in above :nth-child
does not work nor &:nth-child
although changing the navigation, then it works
<svg width="600" height="90" viewBox="0 0 600 90" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect class="card" x="42" y="50" width="16" height="16" />
<rect class="card" x="120" y="20" width="16" height="16" />
<style type="text/css">
:root {
--color-red: #FF5733;
}
svg {
.card {
fill: var(--color-red);
}
:nth-child(odd) {
fill: blue;
}
}
</style>
</svg>
but then adding &
ahead to nth-child, it does not work and show nothing in browser
so my question is
- does css nesting does not work proper in svg style tag
- why does adding nth-child selector on parent
svg
works but not on.card
2
Answers
<style>
inside a SVG is not scoped.Cleaning up your long code.
The second style styles the whole page:
The
🙴
Ampersand is KeyThe
🙴
is a placeholder for the nested selector’s parent selector. I’ve seen comments about how the🙴
is optional for nested CSS, and while that’s true what’s not mentioned is the fact that when the CSS is parsed that🙴
and a space is added to all nested selectors. So if you neglect to add a🙴
, a🙴
and a space will be added. Review the code block below to see the difference this makes. Assume that there are 3.child
within.parent
.Example 1
In the following Demos are the HTML is identical:
Example 2
Demo 1
.shape
in<svg>
,:nth-child(odd)
in.shape
Demo 2
.shape
and:nth-child(odd)
are direct descendants of<svg>
Demo 1
Demo 2