XML technologies / CSS / CSS Cookbook / General topics / Structuring stylesheet content properly
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
You want to structure stylesheet content so that it is as easy as possible to maintain.
Solution
CSS rules can be structured by grouping a page’s visual elements. The following list suggests an order for grouping stylesheet elements:
HTML elements (
h1–h6,p,a, lists, links, images)Typography
Page layout (headers, content, page navigation, global navigation, subnavigation, sidebar, footers)
Form tags (
form,fieldset,label,legendContent (entry, event, news)
Here are comments from a stylesheet that group CSS code into different sections:
/* Typografie und Farben
------------------------------------ */
[CSS-Code]
/* Struktur
------------------------------------ */
[CSS-Code]
/* Kopfzeilen
------------------------------------ */
[CSS-Code]
/* Bilder/Grafiken
------------------------------------ */
[CSS-Code]
/* Listen
------------------------------------ */
[CSS-Code]
/* Formurlarelemente
------------------------------------ */
[CSS-Code]
/* Kommentare
------------------------------------ */
[CSS-Code]
/* Seitenleiste
------------------------------------ */
[CSS-Code]
/* Gemeinsame Elemente
------------------------------------ */
[CSS-Code]Discussion
What works for one person may not help another. This recommendation combines personal experience with best practices and should work well for small to medium-sized websites.
You probably have your own preferences for different projects, and another structure may work better in your situation. Visit your favorite websites and examine their stylesheets for other ways of organizing them.
See also
The article “CSS Organization Tip 1: Flags” by Doug Bowman for a method of locating particular rules in CSS files,
the recipe Organizing stylesheet files for organizing stylesheet files, and
the recipe A smart system for managing CSS hacks for creating a smart hacking system.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln