5 quick tips to write better CSS
By following a set of rules, you can improve your CSS skills. Here are five of my quick tips on how you can write better CSS.
Use the cascade
When writing your CSS, make sure you make the best use of the cascade. A lot of classes and id attributes aren’t necessary for HTML elements in order to style them with CSS.
Look at this HTML code example:
<div id="header">
<h1 id="topheading">An awesome CSS example</h1>
<p id="headerintro">Lorem ipsum dolor...</p>
</div>
CSS for the HTML above could look like this:
#header { background: #f1f1f1 }
#topheading { font-size: 2em; font-weight: normal }
#headerintro { font-size: 0.9em; font-weight: bold }
Not only is the HTML code bloated with unnecessary id attributes, but the cascade in CSS isn’t particularly well used.
How you can use the cascade
#header { background: #f1f1f1 }
#header h1 { font-size: 2em; font-weight: normal }
#header p { font-size: 0.9em; font-weight: bold }
By using this CSS code, all id attributes and classes can be removed from the HTML elements inside the header div.
Using the cascade in CSS will save you time and good code will make the job for you.
Sort your properties
You should sort your CSS properties alphabetically. It’s easier to read, easier to maintain and easier to extend CSS rules when the properties are sorted alphabetically.
Try to find a property fast in this rule:
#content {
float: left;
width: 700px;
margin: 10px;
background: #f1f1f1;
padding: 5px
}
It isn't very easy. It's much easier when the properties are sorted.
Alphabetically sorted CSS properties
#content {
background: #f1f1f1;
float: left;
margin: 10px;
padding: 5px;
width: 700px
}
Sorted properties will make it easier for you to find what you are looking for, giving you more time to produce even better code.
Structure properly
You should structure your CSS code properly. Once you’ve come up with a good method to structure your style sheets, stick with this method.
Here’s a simple list on how you can structure your style sheets better:
- Decide how to write your properties – should you put them all on the same line or put each property on a new line?
- Write global styles first. Overall rules for headings, links, forms and similar.
- Structure sections in the order they appear in the HTML code. Example: write CSS rules for the header before the footer and left menu before the content.
- Tab nested rules. It’s always a good idea to use tabs when coding, even style sheets. This is great for readability and works better the larger a style sheet grows.
How you can structure some basic CSS code
body { }
h1,h2,h3 { }
h1 { }
h2 { }
#wrap { }
#header { }
#header #logo { }
#content { }
#leftmenu { }
#maincontent { }
#related { }
#footer { }
Use CSS shorthand
You can minimize your CSS rules by using CSS shorthand. Instead of writing rules like this:
#leftmenu {
background-color: #f1f1f1;
background-image: url(/gfx/bg.png);
background-repeat: no-repeat
background-position: 0 50%;
background-attachment: fixed
}
You can write rules like this:
#leftmenu { background: #f1f1f1 url(/gfx/bg.png) no-repeat 0 50% fixed }
This second shorthand rule is exactly the same as the extended one above.
CSS shorthand can make you more efficient and will make your CSS look better and more organized.
You can use CSS shorthand for background, border and font, amongst other CSS properties.
Dustin Diaz has compiled a great reference on CSS shorthand properties.
Use comments
Comments have several benefits, such as improving the structure. Comments also
- Make it easier to divide your style sheet into specific sections, making the sections easier to find.
- Improve the readability and make the style sheet easier to maintain.
How you can comment your CSS code
/* HEADER */
#header { }
/* CONTENT */
#content { }
/* NAVIGATION */
#leftmenu { }
Choose a coding style and stick to it
It’s important you choose a coding style that suits you and that you stick to it. Maybe you also have more tips to share?