Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an attribute selector to match classes?

I have found an example of responsive email templates where there are such CSS selectors such as the following:

a[class="btn"] 

Why is this syntax used if it's totally the same as:

a.btn 

Does it have any impact on mobile browsers or anything else?

like image 395
ducin Avatar asked Mar 24 '13 21:03

ducin


People also ask

What is the use of attribute selector in CSS?

The CSS Attribute Selector is used to select an element with some specific attribute or attribute value. It is an excellent way to style the HTML elements by grouping them based on some specific attributes and the attribute selector will select those elements with similar attributes.

What is attribute selector and how it is used?

CSS [attribute|="value"] Selector 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 (-).

What is the purpose of the element attribute value selector?

Presence and value selectorsMatches elements with an attr attribute whose value is exactly value, or contains value in its (space separated) list of values. Matches elements with an attr attribute whose value is exactly value or begins with value immediately followed by a hyphen.

What is the advantage of class selector?

In CSS, class and ID selectors are used to identify various HTML elements. The main benefit of setting class or ID is that you can present the same HTML element differently, depending on its class or ID.


1 Answers

The [] syntax is an attribute selector.

a[class="btn"] 

This will select any <a> tag with class="btn". However, it will not select <a> which has class="btn btn_red", for example (whereas a.btn would). It only exactly matches that attribute.

You may want to read The 30 CSS Selectors you Must Memorize. It's invaluable to any up-and-coming web developer.

like image 186
Cat Avatar answered Sep 22 '22 14:09

Cat