A quick look at CSS attribute selectors

Filed under: CSS

comments (1) Views: 4,959

I'm working on an update to one of my projects and I had need of attribute selectors. If you've not used them before, they make CSS life so much easier. Attribute selectors allow developers to attach styles to an element based on the attributes (you got it) of the element. Let's look at some quick examples.

  • table[width='130'] matches tables with a width of 130
  • img[alt='ibm'] matches imgs with an alt of ibm
  • a[href='https'] (see below)

Some of those might makes sense to you without much work, but that last one might catch you up. It's sort of like regular expressions. That last one matches any anchor tag with an href attribute that begins with https. There's a few more that are tricky, but useful too:

  • [rel*='external'] matches items with external anywhere in the attribute
  • [rel$='external'] matches items with external at the end of the attribute
  • [rel~='external'] matches items with external in a space separated list

You can also use multiple attribute selectors together for even more sexiness:

  • img[width='18'][height='18'] matches images with width and height of 18

So get your head out of the clouds and start using attribute selectors. Your code will thank you for it.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s