In this example, we have two identical <circle>
and two identical <ellipse>
elements in an SVG; their cy
attribute values ar 50
and 150
, respectively.
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="30" />
<circle cx="50" cy="50" r="30" />
<ellipse cx="150" cy="50" rx="20" ry="40" />
<ellipse cx="150" cy="50" rx="20" ry="40" />
</svg>
With CSS, we style only the first circle and first ellipse, allowing their twin shapes to use default styles (with (fill
defaulting to black). We use the cy
property to override the value of the SVG cy
attribute and also give it a fill
and stroke
to differentiate the first shapes in each pair from their twin. The browser renders SVG images as 300px
wide and 150px
tall by default.
svg {
border: 1px solid;
}
circle:first-of-type {
cy: 30px;
fill: lightgreen;
stroke: black;
}
ellipse:first-of-type {
cy: 100px;
fill: pink;
stroke: black;
}
The styled circle's center is 30px
from the top edge of the SVG viewport and the styled ellipse is 100px
from that edge, as defined in the CSS cy
property values. The unstyled shapes centers are both 50px
from the top edge of the SVG viewport, as defined in their SVG cy
attribute values.