Obsessive CSS Code Formatting: Organization, Comments, and Signatures

Published Saturday, July 5, 2008 @ 12:41 pm • 11 Responses

One of my favorite aspects of producing clean, well-formatted CSS code is “meta-organizing” the document using comments. In CSS, comments are included in the stylesheet like so:

/* i am the walrus */

When used constructively, CSS comments serve to break down documents into distinct regions, provide key information about specific declarations, and bring order to even the most complex stylesheets. In my experience, a well-commented stylesheet improves efficiency and optimizes comprehension.

Working with CSS, you can add comments any way you want. There are many different ways to use CSS comments, and endless ways to get there. Let’s check out some practical and interesting ways to use comments, along with some creative ways of doing so.

Information about specific code

Surely the most common reason to use comments involves explaining various CSS rules. Especially helpful for unusual or unexpected code, explanatory comments generally are kept brief, look similar, and vary in location:

/* example border */
div#example_01 {
	border: thin solid red;
	}
div#example_02 {
	font-weight: bold;
	margin: 3em 0;
	_margin: 1em; /* IE6 hack */
	color: red;
	}

The obsessive-compulsive part of this type of commenting involves stripping down the explanation to a bare minimum. Instead of writing “some browsers get this, but not internet explorer”, I write, “non-IE only”. Additionally, I avoid using capital letters except when referring to acronyms. This helps maintain the flow of an otherwise entirely lowercase document, however I have also seen effective use of the crazy ALL CAPITAL LETTERS approach. And finally, only the opening (/*) and closing (*/) comment characters are included with the actual comment, along with a single space on either side. Taken together, these three techniques help keep these types of explanatory comments light, clean, and easy.

Organizing regions of code

An excellent way to stay organized when writing CSS is to group associated code blocks together in a way that is consistent and logical. For example, I generally break things down along the lines of:

  • global settings (resets, scrollbars, etc.)
  • primary layout structure (body, primary divs)
  • secondary layout structure (header, footer, sidebar)
  • tertiary layout structure (page regions, floats)
  • text-related treatments (headings, paragraphs, lists)
  • images, links, and other salient features (pre, code)
  • general styles (forms, tables, buttons, acronyms et al)
  • general classes (.clear, .center, .right, .left, etc.)
  • miscellaneous stuff and template code

Then, to organize such a stylesheet, each region would be clearly indicated with some sort of distinguishing comment. Although I tend to keep it simple with something along these lines:

/* === GLOBAL SETTINGS === */

..I have seen (and used) a variety of creative sectional comments:

/* GLOBAL SETTINGS
   ----------------------------- */


/* GLOBAL SETTINGS
   ============================= */


/* ==[ Global Settings ]== */


/* [[[ Global Settings ]]] */


/*** GLOBAL SETTINGS ***/


/* ===============
   GLOBAL SETTINGS 
   =============== */

..and the list goes on and on. Really, the only requirements for any CSS comment are the opening and closing characters — everything else is entirely up to you. In my stylesheets, I try to keep the sectional comments as obvious and recognizable as possible without interrupting the overall flow of the document. Generally, I like to include a few equality signs (“===”) and capitalize the entire comment to distinguish regional comments from the more specific variety targeting selectors, declarations, hacks, etc.

Navigating extensive stylesheets

For extensive or highly complex stylesheets, CSS comments can also be used to implement a “quick-jump” mechanism to facilitate easy navigation. By including a specific character within sectional comments, you provide a way to “jump” through the document by using your software’s inevitable “Find” feature. For example, with the following sectional comments dispersed throughout my stylesheet, it would be very easy to jump from one section to the next with a simple Find("=") command:

/* ===GLOBAL=== */
.
.
.
/* ===PRIMARY=== */
.
.
.
/* ===HEADER=== */
.
.
.
/* ===SIDEBAR=== */
.
.
.
/* ===FOOTER=== */
.
.
.

When doing this, it is important to choose a navigational character that is not used elsewhere in your CSS document. You may also want to use a CSS comment to include an organizational directory near the top of the document, thereby facilitating quick and easy stylesheet navigation:

/* CSS DIRECTORY
	1. =GLOBAL
	2. =PRIMARY
	3. =HEADER
	4. =SIDEBAR
	5. =FOOTER
*/

You could even include a key character within specific comments and hacks. Although I have never seen anyone use this technique, it is easy to imagine how something like this would be useful:

/* HACK DIRECTORY
	% box model hack
	$ targets safari
	& clearfix hack
	? targets IE 7
*/

..with the associated anchors also present within the comments themselves. The same line of thinking could be applied to nearly any important aspect of a stylesheet. Something like this would certainly help collaborators and clients understand and navigate the document.

Other remarkable uses for the humble comment

Okay, the article is getting rather long, so I will try to wrap it up. Here are some other great ways to use comments to improve the informational value of your stylesheets..

Timestamp and document information

An excellent way to stay organized on the team or network level, especially useful during periods of rapid collaboration or development. In addition to this example, check out the top several lines of just about any WordPress theme for an excellent “real-world” implementation.

/* DOCUMENT INFORMATION
	- Document: Jukebox Theme
	- Version:  1.2.3
	- Client:   Fonzi Winkler
	- Author:   M. Gibbonz   
*/

Color and font information

In an effort to streamline production and subsequent editing, I have adapted this excellent strategy for summarizing all of the colors and fonts used in the stylesheet. This practice has served me well on countless occasions.

/* COLORS
	- default body text  #ffffcc
	- subtitle h2 text   #ff9900
	- form input borders #cc9933
	- default p text     #f0f0f0
	- pre borders        #ffff99
*/

/* FONTS
	- default body text  Arial, Helvetica Neue, Helvetica, sans-serif
	- subtitle h2 text   Garamond, Hoefler Text, Times New Roman, Times, serif
	- form input text    Cambria, Georgia, Times, Times New Roman, Times, serif
	- default p text     Copperplate Light, Copperplate Gothic Light, serif
	- pre and code       Consolas, Lucida Console, Monaco, monospace
*/

Trademark signatures, quotes, slogans

Gotta have ‘em! Well, maybe not, but I certainly enjoy the opportunity to express myself creatively. For the past four or five years, my personal CSS signature thing has been this:

/* all your code are belong to poetry */

..which is placed at the end of every stylesheet. Although I cannot remember them now, over the years I have seen a good number of interesting signatures, including everything from song and movie quotes to personal thoughts and nonsense. If any of you have your own personal signature thing, I would love to hear it!

Time for some comments!

CSS comments are versatile and flexible enough to handle even the longest of fangs. Embellishing your stylesheets with informational and organizational comments is an excellent way to improve the overall quality of your CSS documents while enhancing usability and efficiency. Hopefully, this article will inspire some new ways for you to use comments in your next CSS adventure. Until then, tell us about your different CSS commenting techniques! New ideas, variations, improvements, styles — anything!


Dialogue

11 Responses Jump to comment form

1Louis

July 5, 2008 at 1:53 pm

I use the built-in comments functionnality of TextMate, as shown on their screencast. It’s not as elegant as some of the variations you showcase 1, but it’s quite handy.

Also, the idea of a hack directory, and the way you designed it is beautiful. The same goes for the document/color/fonts meta-information blocks; they are very clean, lovely.

Note that I really like the following shape.

/* ===============
       GLOBAL SETTINGS
       =============== */

Thanks for making me discover it!

2Erika

July 5, 2008 at 4:19 pm

You know, I’m ashamed to admit it, but as well as I know CSS, my stylesheet organizational skills just… suck. LOL. I mean, I literally code as I go, and just leave it there.

Not only that, but there are so many developers who are so concerned with download times… they make it damned difficult to justify adding so much extra markup (as some of your examples are heavy on the characters.) I guess it’s just personal preference… so long as your CSS isn’t as sloppy as mine! LOL

3Perishable

July 5, 2008 at 4:44 pm

@Louis: Textmate is an awesome program, I’ll have to agree. Watching that screencast, I see that it can do much more than I ever imagined. Time for a fresh look into it! Also, thanks for the kind remarks about the comment styles in the article. I agree that the hack directory has a certain “hook” to it that just feels right. Once again, thanks for the great feedback! :)

4Perishable

July 5, 2008 at 4:50 pm

@Erika: download times are definitely important, and you caught me on something that I should have mentioned in the article: this sort of elaborate commenting and organization is intended primarily for development purposes. Once a clean, well-commented stylesheet is ready for public consumption, I highly recommend running it through some sort of CSS code optimizer. The optimized version is then served to visitors, saving them bandwidth and download times, while the development document serves as future reference for updates, maintenance, editing, and so on.

5Louis

July 5, 2008 at 5:53 pm

Hey Jeff, you seem to be impressed by the screencast I linked, but it’s by far one of simplest task you would want to do with Textmate.

I strongly recommend that you watch some of the other screencasts. You can also have a look at this Symfony framework vidéo.

It has nothing to do with CSS formatting, but you’ll see how fast and efficient a Textmate user can be. In fact, I find it very hard to follow what the guy’s doing (this tutorial is bad pedagogy).

6Nikhil G.

July 8, 2008 at 5:17 pm

This may be out of context, but I am not able to post comments on your posts, e.g. APATHY. Huh?

7Nikhil G.

July 8, 2008 at 5:18 pm

Oh sorry, got it… OLD POSTS - NO COMMENTS.
well, I had a small problem using apathy:

“hey Perishable… I must say you made a fan by making this theme. :P

anyways, I had a simple problem.
can u check out how this theme works with Platinum SEO plugin, with rewrite title ON.

When I tried, it showed “Featured| (blogtitle)” where FEATURED was the category i used for right side column.

Thanks. I’m following up.
Nikhil G.”

8SneakyWho_am_i

September 26, 2008 at 2:28 pm

Never included a table of contents, but I LOVE the idea!!

Never included and never will include a font list, For the most part I am fine with the browser’s default decision. The main time I override it is for ascii art or code, where I need a monospace font.

Signatures? In my CSS generally no. No timestamps, nothing. Ascii art? Yes. Jokes? Yes. Thanks to xyz for the clever hack? Yes.

I am leaving more and more and more comments in PRODUCTION CSS now. I know it increases the download time by up to 1 second for dialup users on a bad day, and I know that I can theoretically lose views that way, but really I mostly code for the love of it and not for money, so I don’t care :D I’d rather that people could learn from what I’ve done… My stylesheets for profile pages on third party sites normally include verbose, foul-mouthed, long-winded complaints about layout tables and deprecated HTML attributes such as bgcolor.

Important things are normally with an inline ====== horizontal rule ====== or the under-and-over style horizontal rules. Equals sign of course because it’s not part of the syntax and uses a lot of screen ink.

/*
Long comments are normally like this
lorem ipsum etc
blah blah
*/
/* short comments have one interior space on either side. */

All comments have a new line or space outside each delimiter, also, except in common cases where I was lazy, hit CTRL+D, and typed something else in.

Back to this table of contents idea though.. I can’t use the “xyz hack” thing though.. I don’t use them for the most part now even when thy are valid CSS.But I can see how it could be useful in a big way for marking out elements with a specific purpose. Or for elements which are “high in red” or something.

9Jeff Starr

September 27, 2008 at 10:08 pm

I know it increases the download time by up to 1 second for dialup users on a bad day, and I know that I can theoretically lose views that way, but really I mostly code for the love of it and not for money, so I don’t care

— Hilarious! XD

Subscribe to comments on this post


Share your thoughts..

TopRead official comment policy

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

Laguna Sunrise, for those who know

Perishable on Tumblr

How to Close your Windows Live ID and Hotmail Account

Sunday, 9 November 2008, 6:06 pm

After deciding to stop using Hotmail, I went ahead and tried to close my newer Hotmail accounts. Unfortunately, the process of closing the accounts was a dreadfully convoluted experience, mostly due to the fact that these newer Hotmail accounts are acutally two accounts in one: one is associated with a Windows Live Hotmail account, and the other associated with a Windows Live ID account.

Whatever. After spending twenty minutes trying to figure out how to close the account(s), I decided I had better share the information for future reference. I mean, for crying out loud, Microsoft has convoluted the process to the point of absurdity. It’s like going on a damn treasure hunt! Don’t believe me? Check it out:

Login to your Hotmail account. In the upper right-hand corner, click on “Options” and then on the “More Options” link in in the drop-down panel. Then, under the “Manage your account” section, click on “View and edit your personal information”. From there, click on “Settings” in the left sidebar. Under “Additional options”, click on “Close account”. But wait, you’re not done yet!

On the “Are you sure you want to close your account” page, read everything and then enter your password for verification. If the Live ID account was not associated with a Hotmail account, you should see a success message. If your account was associated with a Hotmail account, you will see a message that says: “Cannot close your account”. Read the paragraph; in it, you will find a link to close your associated Microsoft Live Hotmail account. Click it.

The next screen gives a final warning that you are about to close your Windows Live Hotmail account (as if you didn’t know by now!). Read everything and then click “Close account” to finally close the damn account. And, believe it or not, that’s all there is to it! ;)

Goodbye, Hotmail

Sunday, 9 November 2008, 5:15 pm

Over the years, I have accumulated a large number of free Hotmail accounts. Several of these accounts were created over seven years ago, long before Microsoft made their first major overhaul to the Hotmail service. The overhaul happened quietly around five years ago (if I remember correctly), and directly affected the way in which all future Hotmail accounts operated.

One of the differences between the (very) old accounts and those established after Microsoft changed everything, is the way in which Thunderbird (and various other open-source email applications) is able to process emails from either type of account. For some reason, all of my old Hotmail accounts are processed perfectly well from within Thunderbird; however, those accounts created within the last several years do not play nice at all.

Unlike the older accounts, which are completely synchronized and integrated into Thunderbird, the newer accounts continually throw errors whenever they are checked for new email. These errors are usually associated with good ‘ol Hotmail not accepting any preset passwords, which must be re-entered manually every time email is checked. Needless to say, this behavior is very inconvenient. Meanwhile, the older accounts function flawlessly.

To get the Hotmail accounts to work at all with Thunderbird, two extensions are required, Webmail and its Hotmail-specific add-on. These two extensions enable users to integrate and process their Hotmail accounts along with all of their other email accounts. At first, the Webmail extensions worked perfectly with all Hotmail accounts; then, after a year or so, the newer Hotmail accounts stopped working. After upgrading the Webmail extensions, the newer Hotmail accounts would work, but then another year or so later, the same thing would happen all over again.

Occasionally, I have been able to get the newer Hotmail accounts to work by rolling back the Webmail extensions to previous versions, but this does not always work. Frankly, I suspect that the good ‘ol boys down at the Hotmail factory purposely tweak the system settings such that Hotmail is incompatible with free, open-source email applications like Thunderbird. I don’t know, but one thing is certain, the changes are happening on the Hotmail side of the fence, and they are changing their settings either deliberately or without concern for non-Outlook users.

So, as I find this cycle having come full-circle again, I am once again unable to check emails on newer Hotmail accounts. The bad news is that I am plumb out of previous Webmail extensions to try, and there aren’t any new versions to check out. So, as of today I am officially dumping all of my newer Hotmail accounts. I no longer have the patience or free time to spend fiddling things, trying to second-guess periodic changes on the Hotmail server. I will certainly keep the older accounts for as long as they continue to work, but the newer accounts are just not worth the effort. So, goodbye, Hotmail, I will never open another account with you again.

Transparent Division Recipe

Wednesday, 15 October 2008, 7:59 pm

I am getting so old that I need to make recipes of my design stunts so I don’t have to relearn them from scratch every time. Sad, but so are a lot of things..

In any case, here is the basic procedure for creating the transparent panels used in my current (Quintessential) design:

  1. Open main design template
  2. Verify proper widths via existing images
  3. Create guides according to desired width(s)
  4. Create a rounded-corner vector shape of 10-pixel radius
  5. Rasterize vector shape
  6. Set outer-glow layer style (normal, 33, 3, 7)
  7. Create new empty layer and merge with panel layer
  8. Magic-wand select panel area at 1%, no anti-aliasing
  9. Add layer mask to reveal all
  10. Ctrl+Gradient tool from panel top to desired fade
  11. Duplicate layer as new document
  12. Crop and match desired size

From there, it should be self-explanatory. If not, perhaps it’s time to pursue an alternate line of employment..

Tons of Firewalls

Tuesday, 7 October 2008, 1:45 am

Recently overheard on conservative talk radio (instructing listeners how to obtain a free promotional video from their new website):

“This website has tons and tons of firewalls, so you have to use your real email address to download the video..”

The Quiet Search Revolution

Monday, 6 October 2008, 12:15 pm

Just a thought.. As awesome as Google is these days, it would suck if they ended up owning the entire search-engine business. When they get to the point where all competition is impossible (due to their sheer size, financial resources, media influence, etc.), how many alternate search engines will have the resources for continuous improvement and top-quality search results? When this happens, we will have no choice but to do exactly what Google tells us to do.

As deeply ingrained as it is for everyone to instinctively and unthinkingly turn to Google for their search activity, it is time to leave a few alternate search tabs open for as much use as possible. Instead of using Google just because that’s what you always do, try your search on MSN, Yahoo, Ask, or any of the other independent search engines instead. Sharing traffic with other search engines is a nice, quiet way to keep the competitive spirit alive and well in the search-engine business.

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • Jeff Starr: @Chris: There are so many different things involved with any given layout that it is difficult to troubleshoot and solve problems wit...
  • Martin: Chris: Not sure exactly what the issue is (perhaps someone else can be more specific in their assistance) but the following article w...
  • Jeremy: This worked great with OS 10.5.5 and my Blackberry Pearl (8100). Thanks!...
  • Chris: It is amazing how many dofollow plug ins there are. It is a wonderful thing if people use it correctly. My blogs are on blogspot and ...
  • Chris: Martin: Both of the divs have different IDs, but the problem seems to be with how the 'content' div handles what the new div 'contain...
  • Jeff Starr: Hi David, I disagree entirely that the issue described in the article is anything at all abnormal. I have developed many sit...
  • balisugar: Hi, sorry to botter you, I need help. I think I have a few pages with strange url, that i can see from my wassUp stats. That xxx i...
  • David Grega: The behavior you describe of any RewriteRule in .htaccess automatically enabling Hotlink Protection is abnormal. I have a website my...
  • Martin: Chris> Sounds like you already know that you'll have to fix the container div conflict you've got. Can't have 2 divs with the same...
  • Chris: Hi, I just found your blog tonight and it's certainly bookmark worthy, so thanks! Compliments aside, I'm having problems with this...

Read more recent comments..

Attention: Do NOT follow this link!