CSS Organization Part 2
Do you write and manage large CSS files? Ever get tired of scrolling up and down in search of a specific rule or set of rules? The CSS files are often quite long, requiring constant scrolling up and down several screenâs worth of text to alter rules or add new ones.
Grouping your CSS rules
Every project typically have sections in the CSS broken out for the following:
- Header
- Structure
- Nav
- Search
- Headings
- Lists
- Forms
- Links
- Misc
One way how to separate CSS file sections, is using commented text and a series of dashes as headings to make these sections easily distinguishable:
/* Navigation
----------------------------------------------- */
This makes it fairly easy for me â or a client who takes over the files â to find certain rules, or to know which rules affect a relevant portion of the design. Even more importantly, this method of organization saves a lot of time when troubleshooting any style sheets Iâve written, especially for older projects where memory of how a project was built might be fading.
Finding those sections quickly
So how to find rules quickly when youâre working? Some apps allow you to set markers in documents, allowing quick key-command access to that section. Great if youâre into that kind of thing, but it can get tedious to do with every new CSS file.
You could also do what Iâve been doing for the last few years: use your text editorâs FIND command to do a quick search for a certain block of text. But if your style sheets are anything like mine, a generic search like “nav” or “h2″ might point to several places in the document, requiring several FIND NEXTs (or many) to actually find what youâre looking for.
A fictitious, but even better example of FIND not finding what you want immediately: Say you have a section of your site youâve abbreviated with the acronym âRDEâ. So you label a portion of your CSS with:
/* RDE
———————————————– */
Great. So you can easily see the rules that apply to the RDE section. Except that if you want to use FIND to zoom to that section, you quickly notice that your text editor finds every instance of the following property:
#nav {
float:left;
width:182px;
border-top:1px solid #911;
}
A simple fix: Flags
As I discovered, if I add a simple flag immediately before the commented header text â like the â=â character, one thatâs not used in CSS or class/id syntax â I have a simple text-based hook for finding (and zooming to) that section header immediately. So a quick FIND for â=rdeâ yields the following:
/* =RDE
----------------------------------------------- */
And Iâm right where I want to be with a few keystrokes. No more finding redundant instances of that string. The â=â flag doesnât even need to be placed before the first word. On a current project, I have several sections whose label begins with âMISC:â. So I place the flag next to a more unique word in the label:
/* MISC: =Lists
----------------------------------------------- */
â¦which allows me to quickly zoom to that section for generic/default list styles by doing a FIND for â=listâ.
Of course, this tip is only helpful if youâre diligent about keeping rules organized into discrete sections when working with large style sheets.
Next tip will center around how I choose to break out my sections, the order of those sections in the entire document, and how to decide which rules get placed in which sections when thereâs more than one choice.
Source: www.stopdesign.com
Tags: CSS, organization, rule, section, text, tips
Related:
Posted in CSS. 197 views
You can leave a response, or trackback from your own site.
[...] « The title and alt attributes CSS Organization Part 2 » [...]
[...] 3) The third tip is to clearly divide your stylesheet into specific sections. Also, by using a flag, you can get to the area you are looking for faster. Before you divide up your styles, it is important to define an outline you are comfortable with as will as a separator format you can notice easily. [...]