Remember that you can stack and inherit CSS classes, as follows:
.icon { background: left center no-repeat; padding-left: 20px; /* leave space for icon */
#SearchBar .icon { background-image: url(../images/icons/search.png); }
A nice technique I've used before is setting multiple CSS classes (in this case, for displaying an audit log):
/* the icon is displayed by each entry; if it has no other class,
it will show a "generic" icon */
.log .entry {
padding-left: 20px; /* leave space for icon */
background: url(../images/icons/log-generic.png) top left no-repeat;
}
/* slot a generic delete icon in */
.log .icon.delete {
color: red;
background-image: url(../images/icons/log-delete.png);
}
.log .icon.delete.person {
background-image: url(../images/icons/log-delete-person.png);
}
This is a great way to define a series of generic styles (for links with icons, toolbar buttons, avatars, etc), which you can then override in specific instances (think of it as class inheritance).
I'm a bit weird about naming in CSS, but I stick to the convention that UpperCase
is for IDs, and lowerCamel
for classes. It just helps me to differentiate.
The zen of CSS naming that I follow is:
- The fewer IDs, the better
- IDs should be reserved for main layout sections
- ...and to identify elements for DOM/AJAX operations
- Use generic class names (log, icon, person, button, etc)
- ...then combine them with IDs or parent classes to form specifics, e.g.
#Header a.icon.person
for a profile link in the header
Most importantly, keep it lean. The less CSS, the better - use generic, re-usable styles, and you will benefit from a) consistency in your UI, and b) less page bloat.
HTH