Last Updated: February 25, 2016
·
806
· krzochalski

Be a clean CSS coder (part two)

If you have not read the first part, you can find it here ( https://coderwall.com/p/nx-uxw )

.class {
  property: value;
}

Readability

When you want for example to set a background, it is much more readable to use properties like backgroung-image, background-color or background-size instead of just background. Properties set in multiple lines are much more easy to read.

Third person shooter

Try to read your code as a different person reading it for the first time, if it is understandable, it is ok. Else, change your code. Remember to always write your code as it was written for somebody else.

Blocks & Comments

Sort classes into blocks, if some particular classes are used to style footer put them into one block and make two lines of space before and after. For example:

(...)
/* End Header classes */


/* Article classes */
#footer a{
  property: value;
}
#footer p{
  property: value;
}
/* End Article classes */


/* Footer classes */
(...)

Logic

It is stupid to put width value at the beginning of a class and height at the end. Keep similar values or those that work together as close as possible.

Part two is the last :). Please, leave comments or pm me if I made some mistakes.

2 Responses
Add your response

Sorry if I'm wrong but writing "background" instead of all its variations is better for perfomance, right?

over 1 year ago ·

It does not affect the performance in a visible way, I prefer to have two versions of css. One of them is minimized (all in one line, no spaces) so it weighs less and it it read by the site (main site). The second one is normal to read and make changes on the svn server.

I prefer to have it written in this way. When you write an e-mail newsletter, you have to use those so as the e-mail client will read it properly.

However, when it comes to border, the faster and better way is to write

border: 2px dashed #222;

over 1 year ago ·