Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS2 Attribute Selectors with Regex

CSS Attribute selectors allow the selection of elements based on attribute values. Unfortunately, I've not used them in years (mainly because they're not supported by all modern browsers). However, I remember distinctly that I was able to use them to adorn all external links with an icon, by using a code similar to the following:

a[href=http] {     background: url(external-uri);     padding-left: 12px; } 

The above code doesn't work. My question is: How does it work? How do I select all <a> tags whose href attribute starts with "http"? The official CSS spec (linked above) doesn't even mention that this is possible. But I do remember doing this.

(Note: The obvious solution would be to use class attributes for distinction. I want to avoid this because I have little influence of the way the HTML code is built. All I can edit is the CSS code.)

like image 539
Konrad Rudolph Avatar asked Sep 08 '08 09:09

Konrad Rudolph


People also ask

Can you use regex in CSS selector?

You can use regular expressions (regex) and cascading style sheet (CSS) selectors as operators wherever trigger filters are used. When a regular expression or CSS selector is set as the operator for a trigger, you can specify that the trigger matches the rule.

What are the three types of selectors in CSS Select 3?

We will be looking at the following important CSS Selectors: CSS Universal Selector. CSS Element Selector. CSS Id Selector.

What is an attribute selector give an example?

[attribute] Selector: This type of attribute selector is used to select all the elements that have the specified attribute and applies the CSS property to that attribute. For example the selector [class] will select all the elements with the style attribute. Example: <! DOCTYPE html>


2 Answers

As for CSS 2.1, see http://www.w3.org/TR/CSS21/selector.html#attribute-selectors

Executive summary:

     Attribute selectors may match in four ways:      [att]     Match when the element sets the "att" attribute, whatever the value of the attribute.     [att=val]     Match when the element's "att" attribute value is exactly "val".     [att~=val]     Match when the element's "att" attribute value is a space-separated list of     "words", one of which is exactly "val". If this selector is used, the words in the      value must not contain spaces (since they are separated by spaces).     [att|=val]     Match when the element's "att" attribute value is a hyphen-separated list of     "words", beginning with "val". The match always starts at the beginning of the     attribute value. This is primarily intended to allow language subcode matches     (e.g., the "lang" attribute in HTML) as described in RFC 3066 ([RFC3066]). 

CSS3 also defines a list of selectors, but the compatibility varies hugely.

There's also a nifty test suite that that shows which selectors work in your browser.

As for your example,

a[href^=http] {     background: url(external-uri);     padding-left: 12px; } 

should do the trick. Unfortunately, it is not supported by IE.

like image 199
Antti Kissaniemi Avatar answered Sep 17 '22 12:09

Antti Kissaniemi


Antti's answer is sufficient for selecting anchor's whose href's begin with http and gives a perfect rundown on the available CSS2 regex-esque attribute selectors, like so:

Attribute selectors may match in four ways:  [att] Match when the element sets the "att" attribute, whatever the value of the attribute. [att=val] Match when the element's "att" attribute value is exactly "val". [att~=val] Match when the element's "att" attribute value is a space-separated list of "words", one of which is exactly "val". If this selector is used, the words in the  value must not contain spaces (since they are separated by spaces). [att|=val] Match when the element's "att" attribute value is a hyphen-separated list of "words", beginning with "val". The match always starts at the beginning of the attribute value. This is primarily intended to allow language subcode matches (e.g., the "lang" attribute in HTML) as described in RFC 3066 ([RFC3066]). 

However, here is the appropriate, UPDATED way to select all outgoing links using the new CSS3 :not pseudo class selector as well as the new *= substring syntax to make sure it disregards any internal links that may still begin with http:

a[href^=http]:not([href*="yourdomain.com"]) {     background: url(external-uri);     padding-left: 12px; } 

*Note that this is unsupported by IE, up to at least IE8. Thanks, IE, you're the best :P

like image 22
dcarps14 Avatar answered Sep 19 '22 12:09

dcarps14