CSS3 and naming conventions
At this point my web design career I have almost completely removed directories from my images folder. This is the result of using content management systems like Joomla, and the fact that I rarely have more than a handful of images in my images folder when I initially develop a site. I stopped because I was noticing that I was taking time to create folders like “header” and “background” to organize images, but only placing one or two images in those directories.
Instead, I began using naming conventions. Images with semantic names like “logo.png” and “navigation.png” would get their use or location in the code appended in front of them. So “logo.png” would become “header-logo.png”, “navigation.png” would become “background-navigation.png”. If you look at the source of this site you can see this in action:
#wrapper {
background: transparent url(images/background-header.jpg) center top repeat-x;
}
What does all this have to do with CSS3?
Basically, I can now add new styles to images based on my naming conventions. This, of course, applies to more than just images, but this was the first use I’ve found for these that naturally fits my already established workflow.
A little history: attribute selectors
These selectors allow web designers to select items based on just a portion of an attribute. I imagine this was originally developed to allow CSS target the many different kinds of forms elements (you’ve got checkboxes, text, radio buttons, etc) without needing to give each one a separate class. While useful, they were only good at matching exact strings. For example, to add a red background to text inputs I just write a rule like this:
input[type="text"] {
background-color: #F00;
}
The future: substring matching attribute selectors
Substring matching attribute selectors are a huge leap up from attribute selectors. They free web designers from relying on matching exact strings, making just about any tag with a selector fair game. One might also find himself using less classes/ids as a result, and it increases the semantic value of attributes. Plus it makes use of naming conventions that many of us web designers have probably been using for years.
I can use these naming conventions to add styles to the images, whereas in the past I would have used an id or class. For example, in the past, if I wanted all the images in the main content area of my site to float right I would have a rule like this:
#content img {
float: right;
}
But then let’s say that I wanted certain images to float left to mix it up a little:
#content img.left {
float: left;
}
Now I have two rules for my content images that are difficult (well not that difficult) to override. But if I use these new CSS3 selectors, I can write two simple rules that are “easier” to override (should the need arise):
img[src*="right"] {
float: right;
}
img[src*="left"] {
float: left;
}
The awesome part is that these CSS rules will work for either of my methods described above:
Old method
<img src="images/right/lion.png" alt="Lion on his back" />
New method
<img src="images/right-lion.png" alt="Lion on his back" />
One last thing…
Notice that I am using the * in these, that means that the string “left” or “right” can be anywhere in the src attribute. You can alternatively us ^ or $ which will look at the end of the string only, but won’t be of much use in this instance. Unless I was to place “left” or “right” at the end of the image name, but I would need to make it look like this:
HTML
<img src="images/lion-right.png" alt="Lion on his back" /> <img src="images/lion-left.png" alt="Lion on his back" />
CSS
img[src$="right.png"] {
float: right;
}
img[src$="left.png"] {
float: left;
}
For more on CSS2 attribute selectors, check here or here.
For more on CSS3 substring matching attribute selectors.