Codex tools: Log in
Languages: English • 日本語 • (Add your language)
Just like with PHP Coding Standards, following CSS code standards ensures that your CSS code is valid, helps in debugging and maintenance, and aids in collaboration with other developers.
Contents |
selector {
property: value;
}
Correct:
#selector-1,
#selector-2,
#selector-3 {
background: #fff;
color: #000;
}
Incorrect:
#selector-1, #selector-2, #selector-3 {
background: #fff; color: #000;
}
Also incorrect:
#selector-1 { background: #fff; color: #000; }
Correct:
#comment-form {
margin: 0;
}
Incorrect:
#commentForm { /* Avoid camelcase. */
margin: 0;
}
#comment_form { /* Avoid underscores. */
margin: 0;
}
#c1-xr { /* What is a c1-xr?! Use a better name. */
margin: 0;
}
Prefixed vendor-specific properties pairs should appear directly before the generic property they refer to (why?).
#selector-1 {
-moz-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.2);
-webkit-box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.2);
box-shadow: 0px 3px 3px rgba(0, 0, 0, 0.2);
display: none;
float: left;
}
Dimensional position properties (top, right, bottom, left) should be grouped together outside of alphabetical ordering when paired with a specific type of positioning (relative, absolute, etc.).
#selector-1 {
font-size: 1em;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1;
}
Where width and height are both declared, height should always follow width.
#selector-1 {
width: auto;
height: auto;
}
/* Short comments: one line with no trailing space. */
#selector-1 {...
#selector-1 {
color: #000; /* A short comment can also follow one property/value pair. */
}
/*
For longer, multiple line comments that need more room,
add a newline before and after the comment.
Leave one line of space before the next block.
*/
#selector-1 {...
For comments denoting a new section, use the following notation (see CSS Flags). Sections are separated from the previous block by two lines, and should have one following line of space.
#previous-block {
color: #000;
}
/* =Name of section
-------------------------------------------------------------- */
#selector-1 {...
There are CSS authoring applications, like TextMate, CSSEdit and TopStyle that have a "format" command that allows you to preset the format you'd like, and you can reformat the entire document at once.
In TextMate, for example, you can use shortcuts to help format CSS quickly.
Here are several other tools for cleaning up your CSS code.