Jeff Starr of Perishable Press has written an interesting article outlining the patterns and trends of CSS code formatting. In it he gives some examples and shares his own peculiar way of formatting CSS code. In particular, he formats code in an inverted diagonal pattern, starting with the longest line at the top to the shortest one at the bottom, as in the following example:
div#example element {
margin: 5px 15px 5px 0;
border: 1px solid #444;
line-height: 1.5em;
text-align: center;
background: #222;
font-size: 10px;
display: block;
padding: 5px;
color: #888;
float: left;
}
div#another div.example element {
border: 1px solid #444;
margin: 7px 0 17px 0;
letter-spacing: 1px;
font-weight: bold;
background: #222;
font-size: 1.1em;
cursor: pointer;
display: block;
padding: 3px;
width: 308px;
color: #888;
clear: left;
float: left;
}
div#another div.example element {
text-indent: -9999px;
overflow: hidden;
position: fixed;
display: block;
z-index: 9999;
padding: 0px;
margin: 0px;
bottom: 0px;
right: 0px;
}
In his latest article, he focuses on CSS indentations and spacing trends. He provides even more examples, some of which are quite interesting - such as the one below, which he calls “Meta-spacing for columnar organization”:
* {
vertical-align: baseline;
font-family: inherit;
font-style: inherit;
font-size: 100%;
border: none;
padding: 0;
margin: 0;
}
My personal favorite is the single line code blocks method, where each declaration is written on a single line. This method benefits from not having to scroll all the way down thus saving space. Also, this method allows one to easily and quickly sort through and find any delcaration that needs to be worked on.
The only disadvantage to placing everything on one line is that some of then often end up becoming very very long, as in the example below:
#containerHead { padding-top: 57px; position: relative; }
#containerHead h1 a { display: block; width: 133px; height: 73px; position: absolute; right: 0px; top: 57px; background: url(../images/companyLogo.gif) no-repeat; }
.homeBanner { height: 134px; width: 730px; margin-top: 193px; position: relative; }
#homeBanner1 { background: url(../images/homeSplash1.gif) no-repeat; }
#homeBanner2 { background: url(../images/homeSplash2.gif) no-repeat; }
#homeBanner3 { background: url(../images/homeSplash3.gif) no-repeat; }
#homeNext a { width: 730px; height: 40px; background: url(../images/homeNext.gif) no-repeat; display: block; }
#homeNext a:hover { background: url(../images/homeNext.gif) bottom no-repeat; }
What is your favorite CSS coding convention? Do you use a special formatting method that is uniquely your own? I’d love to hear about them.
Share