Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct behaviour of SVG "defs" tag in CSS?

When one write this SVG code, with embedded CSS:

<?xml version="1.0" encoding="utf-8"?>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 100 100">
    <style>
        defs rect
        {
            fill:           blue;
        }
    </style>
    <defs>
        <rect id="rectangle" x="5" y="10" width="3" height="3"/>
    </defs>

    <rect x="5" y="5" width="3" height="3"/>
    <use xlink:href="#rectangle"/>
</svg>

Then, Chrome does apply the "fill:blue" rule to the second rect through the use tag (so first rect is black, second is blue), whereas firefox does not apply the rule (both rects remain black).

Is that a firebug? Is there something I don't get? Or does the standard say "defs tag should block CSS selectors"?

like image 895
Xenos Avatar asked Apr 16 '14 18:04

Xenos


People also ask

What is DEFS in SVG?

The <defs> element is used to store graphical objects that will be used at a later time. Objects created inside a <defs> element are not rendered directly. To display them you have to reference them (with a <use> element for example).

What is an SVG viewBox?

The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. The value of the viewBox attribute is a list of four numbers: min-x , min-y , width and height .

Can SVG be stretched?

An SVG image with fixed dimensions will be treated just like a raster image of the same size. Note: If you are trying to stretch your SVG to a different aspect ratio with CSS—for example in order to stretch it over the page background—make sure your SVG includes preserveAspectRatio="none" .


1 Answers

It's actually a bug from Firefox.

Rules apply to the defs rect elements, but they don't apply when the use tag clones the defs rect. Replacing the defs with a g tag shows that the defs rect is filled; but the rules are not applied to the "-generated clone".

Correct behavior is Chrome's one, filling the use-generated clone; use-cloned version of the defs rect is wrongly not filled by firefox.

See https://bugzilla.mozilla.org/show_bug.cgi?id=997362#c4 for more explanations.

like image 167
Xenos Avatar answered Oct 29 '22 06:10

Xenos