I’ve been trading e-mails with a colleague for the last few days and he asked if there are any CSS methodologies available. While I have practices that I consistently follow, I do not know of any that are wide-spread or commonly accepted as the best way to set up and maintain style sheets. So, I sent an e-mail to the CSS-D list to gather the feedback of other designers and developers. Below is the e-mail I sent sans-introduction which you just read.
So, does anyone know of a good methodology already in existence? If not, anyone interested in assisting me as I begin to document and revise one?
In case there aren’t any, here are a some of the practices I follow. Please feel free to let me know of poor decisions in my methods, and suggest new ones!
Style Sheet Organization
- Classes, IDs and elements are listed in alphabetical order unless otherwise noted. The properties of each class, element and ID are in alphabetical order as well. So the “acronym” tag comes before the “body” tag which comes before the “price” ID in the stylesheet. I find this makes it much easier for me and other developers to locate specific items within the stylesheet quickly.
- The link pseudo-classes are grouped together, but do not follow the alphabetical organization within that grouping as some browsers will use the last pseudo-class listed, even if the more accurate one is listed earlier. Rather annoying.
- When possible, elements should be grouped if they have a common attribute, or set of attributes. An example would be to assign the same font to the “body”, “p”, “input” and “td”:
body, input, p, td {
font: small Georgia, Garamond, "Times New Roman", serif;
}
- When possible descendant styles should be used to cut down on the code in the style sheets. Below is an example of applying a specific style to an input element within a form with an element that has been assigned the “Purchase” class. Example:
.Purchase form input {
border: 1px solid #654819;
height: 18px;
}
Style Naming Convention
- Classes and IDs use mixed case to make them easier to read. In the HTML 4.01 spec, classes and IDs are case sensitive; though some browsers do not recognize the fact. Elements are left in lower case. I know this one may end up being controversial as naming conventions often tend to be
- Do not start an ID or class with a digit or use underscores as they may not work in all browsers.
- Classes and IDS should never be an adjective or infer placement as a future redesign may not place the item in the same spot, or use the same background color. So, instead of using an ID of “TopBox”, use an ID specific to its contents, perhaps “GlobalNav”.
- Faux Borders and Pseudo Table Effect - Faux Borders and Pseudo Table Effect provides a handy tutorial explaining how to ensure that several floated divs all maintain the same height, even if...
- Style Evolution - Dynamic CSS Part 2 - Please note that this assumes you have read Part One of the series. Also, I have taken to calling this Evolved CSS, as the word...
- Invasion of the Body Switchers - Invasion of the Body Switchers from A List Apart: > Since it was first released in 2001, Paul Sowden's Style Sheet Switcher has been downloaded...
- Two Background Images - Jeff Croft pointed out a discovery by UrbanDude: how to use two background images on a Web page. The concept is so simple, that it...
- Floating Mac IE 5 - Just a quick note, when trying to float elements in IE 5 for the Mac, it is important to remember that the browser (incorrectly) applies...
Related posts brought to you by Yet Another Related Posts Plugin.
Hello, Alex!
At the office, I am currently working at a really big site and we have a big problem with organizing the CSS. The project has been started by a foreign team and now we have to continue it. But working on the front-end, I found many bad practices in CSS (I think they are bad).
So, I would really apreciate if you could tell what you found about organizing css, some best practices or some links.
Thank a lot,
Emilia.
Hi Emilia!
Your timing is excellent as I am preparing to post my first draft of my CSS methodology within the week. :) I hope it will be of some help.
Alex