Book your place only £325.00
-
Birmingham, UK
April 1st, 2010
Book now
Excludes VAT.
Photo credit: © John Morrison / Subism StudiosWriting this week about eating accessibility humble pie and using CSS attribute substring selectors, a comment by clever Craig Cook sent my imagination reeling.
I had written about using an attribute substring selector to bind a style to an element using its alt text.
<a href="#">
<img src="promo-main-css.png" alt="Designing With CSS Cover" />
<img src="promo-disc.png" alt="Designing With CSS Disc" />
<h3>Designing with CSS</h3>
</a>
div.promo-main img[alt*="Cover"] {
z-index : 2;
position: absolute;
top : 0;
left : 0; }
div.promo-main img[alt*="Disc"] {
z-index : 1;
position: absolute;
top : 70px;
left : 85px; }
This selector allows you to select an element where (in this case) the words “Cover” and “Disc” appear somewhere in the alt text string.
You could rename the cover images to end with “-cover.png” and use the same clever selector trick on the src attribute.
img[src*="cover"]
Wait. Did he just suggest using an attribute selector to target an image based on its file name? Why have I never done that? This approach can be extended to help me solve everyday problems.
The problem: My designs always include a mixture of JPGs and alpha-transparent PNG content images, inline rather than CSS background images. I often add padding, background-color and border to spruce up inline images. I want to apply these properties to JPGs but not to PNGs. I also want to avoid unnecessary class attributes in my HTML.
The answer: Use an attribute selector to target an image file type. In this example I can style images whose source ends with .jpg
<img src="promo-main-css.jpg" alt="" />
img[src$=".jpg"] {
/* Styles */ }
The problem: Sometimes you need to size images from external sites like Flickr, perhaps limiting them to a maximum width.
The answer: Select images where their source starts with http://farm3.static.flickr.com.
<img src="http://farm3.static.flickr.com/2547/4112232300_3215c9c5a0_b.jpg" alt="" />
img[src^="http://farm3.static.flickr.com"] {
/* Styles */ }
Or includes flickr.com somewhere in the source string.
img[src*="flickr.com"] {
/* Styles */ }
The problem: In practice, when images are served from a CMS or uploaded by a client, their remembering file naming conventions or adding class attributes to maintain styles can often be tricky. For example, let’s say that large content images should be styled with 10px padding but thumbnail images only 5px.
The answer: Upload images to different folders and use an attribute selector to target an image based on its directory. In this example I can style only images stored inside a folder named thumbnails.
<img src="thumbnails/promo-main-css.png" alt="" />
img[src*="thumbnails"] {
/* Styles */ }
Filing images into various directories not only makes good sense for long-term management, you can also use those directories for styling groups or collections of images using attribute selectors.
I slap my head. Craig’s comment may just have changed the way I name images, store images and style images forever.
Join world-renowned mobile designer and author Dan Rubin for a full day learning the key steps to transform your site for mobile users, from content strategy to CSS3 to device detection and optimisation.
April 1st, 2010
Book nowExcludes VAT.
Photo credit: © John Morrison / Subism Studios27th Nov 2009Excellent points very well made, I had my doubts as to the usefulness of CSS selectors but this has won me over - I have already implemented them (albeit very slightly) on my new project!
27th Nov 2009“Did he just suggest using an attribute selector to target an image based on its file name? Why have I never done that?”
Because you haven’t been paying attention to my technical talks over the past half-decade or so, Andy. That’ll teach ya.
Excellent post. I think you may opened my eyes to the fact that my advanced CSS skills are not where they need to be.
(This comment was left on For A Beautiful Web)
27th Nov 200927th Nov 2009What kind of browser support do you get for these selectors?
27th Nov 2009Cool selectors, but they’re a lot scarier to me than using a traditional dom selector. On a personal blog, sure it’s fine, because you control it end to end. On a large production environment with hundreds of developers, never. Naming anything off of a title attribute is just asking to get broken, as is the dependency on a 3rd party url - a substring of a url might be ok, but starts with “http://farm3.static.flickr.com”? That is just asking to be broken by flickr, a migration to a different farm node, or https.
All I’m trying to say is that the article is a great reminder of powerful selectors, but one should heed their use in the wild.
28th Nov 2009Yeah, it would be interesting to hear what kind of browser support there is for this? If its got a wide coverage then it would be stupid not to use this in future projects..
28th Nov 2009Interesting idea!!
28th Nov 2009Good to know but does this work in IE the same way as it does in FF or SF? If not, I stick to the routine to add the css class with JS to the prefered TAGs.
28th Nov 2009This sounds great and I also slapped my head for not thinking of something similar before.
To all those that asked: The attribute selector is supported by all browsers except MS IE 6 and earlier (see http://www.quirksmode.org/css/selector_attribute.html for details).
28th Nov 2009A quick question and it’ll probably be a silly one:
Why the $, *, or ^ symbols in the CSS, such as “img[src^=“http:...”?
What are they for? What are the differences between the their use?
Cheers!
28th Nov 2009I would also like I breakdown of browser support
28th Nov 2009^ starts with
$ ends with
* contains
28th Nov 2009@Stu
Aaaahhhhh… Thanks dude, cheers!
29th Nov 2009Eric Meyer: Because you haven’t been paying attention to my technical talks over the past half-decade or so, Andy. That’ll teach ya.
— Wait. You talk about CSS?
Jon Blower, chopeh, phenomenia: (paraphrasing) What kind of browser support do you get for these selectors? Does this work in IE the same way as it does in FF or SF?
— As Julian Schrader points out, all modern browsers support attribute substring selectors. Internet Explorer 6 does not, but that is rarely a problem for me as I serve Universal Internet Explorer 6 CSS to that browser for all my sites. If IE6 is still an issue for you, you can bootstrap support by using Dean Edwards’ IE8 Javascripts.
Peter Allen: A quick question and it’ll probably be a silly one: Why the $, *, or ^ symbols in the CSS, such as “img[src^=“http:...”? What are they for? What are the differences between the their use?
— As Stu rightly says, ^ selects the start of an attribute, $ selects the end of an attribute and * selects part of an attribute.
29th Nov 2009Awesome Tricks, I will try it
29th Nov 2009This is great. I never knew this technique existed.
The big problem is, that IE7 isn`t very handy at using attribute selectors. I don`t have a problem when there are selectors that IE6 doesn`t suppert and give a damn about it. But you can not ignore IE7 at the moment. Too many people are still using it and will for many years.
Don`t get me wrong: I am a big fan of CSS3 and advanced CSS stuff that older browsers (or IE8) don`t support (and I an also using it), as long as it doesn`t affect the design of a site too much. But when attribute selectors are used for essential page styling I am not a big fan of it.
(This comment was left on For A Beautiful Web)
30th Nov 20091st Dec 2009What about older brwosers (ie IE6) that don’t support these selectors?
1st Dec 2009Ionut Popa: What about older brwosers (ie IE6) that don’t support these selectors?
— As Julian Schrader points out, all modern browsers support attribute substring selectors. Internet Explorer 6 does not, but that is rarely a problem for me as I serve Universal Internet Explorer 6 CSS to that browser for all my sites. If IE6 is still an issue for you, you can bootstrap support by using Dean Edwards’ IE8 Javascripts.
Great tip. I am continually amazed at the power of CSS selectors.
(This comment was left on For A Beautiful Web)
4th Dec 2009There are currently no tweet replies. Add one?
An archive of blog entries since 2004 on subjects including CSS, web standards, accessibility, website design and development.