Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CSS attribute selector for an SVG element with namespaced attribute href?

Why can't I select elements by their href-attribute?

CSS

/* Works */
svg image[type=overlay]{
    outline: 3px solid blue;
}

/* Doesn't work */
svg image[href*='temp']{
    outline: 5px solid red;
}

/* Doesn't work either */
svg image[href='wcs/WFL/position/temp2.png']{
    outline: 5px solid red;
}

SVG

<image display="inline" type="overlay" width="148" height="90" x="920" y="918" transform="" href="wcs/WFL/position/temp2.png"><title>B02003
Temp: 2</title></image>

I did notice the browsers turns the href attribute to xlink:href but image[xlink:href*='temp'] doesn't work either.

How can I make this work?

Edit: The SVG uses the following namespaces, I think this causes the problem but I don't know how to get around it.

xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svg="http://www.w3.org/2000/svg"

Fiddle

like image 280
Jonathan Avatar asked Dec 10 '14 10:12

Jonathan


People also ask

Can I use SVG use href?

use. For <use> , href defines a URL referring to an element or fragment within an SVG document to be cloned. The <use> element can reference an entire SVG document by specifying an href value without a fragment.

How can we select elements with a specified attribute in CSS?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

What is the CSS selector for an anchor tag containing the title attribute?

The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.


1 Answers

Demo Fiddle

Firstly, in order to use xlink slectors, you need to to declare the xlink namespace at the top of your stylesheet according to the spec:

@namespace xlink 'http://www.w3.org/1999/xlink';

Then, you can use the following attribute selector with a namespace prefix:

svg image[xlink|href*="temp"] {
    outline: 3px solid red;
}

The attribute name in an attribute selector is given as a CSS qualified name: a namespace prefix that has been previously declared may be prepended to the attribute name separated by the namespace separator "vertical bar" (|). In keeping with the Namespaces in the XML recommendation

like image 149
SW4 Avatar answered Sep 22 '22 02:09

SW4