Why use an attribute selector to match classes?

ducin picture ducin · Mar 24, 2013 · Viewed 46.8k times · Source

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?

Answer

Eric picture Eric · Mar 24, 2013

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.