CSS Best Practices

Everyone has a slightly assorted move to CSS. That’s part of the think it crapper be a situation to modify someone else’s code.

But there are a some good practices you crapper use in your CSS to attain your stylesheets easier for you to read, and easier for anyone added that ends up modifying them in the future.

Single-line vs. Multi-line

The prototypal selection you’ll hit to attain in your CSS is whether you poverty to write it on multiple lines, same this:

.title { font-size:14px; line-height:20px; color:blue;}

Or on one line, same this:

.title {font-size:14px;line-height:20px;color:blue;}

When I started out, I began with the prototypal method. By broad the assorted CSS properties discover onto assorted lines, you attain them easier to read. That’s a enthusiastic support when you’re still getting utilised to CSS.

As I got more confident with CSS though, I swapped to the ordinal method.

Once you’re comfortable with CSS, the large instance waster becomes finding the surroundings (the .title part) you need. When you’ve split every property into its possess line, the enter becomes very long and unwieldy. I encounter it easier to scan down with the ordinal method.

The file size is quite dramatically revilement downbound (more on that later).

Of course, a aggregation of people module dissent with me there! This garner comes downbound mostly to personal preference. If you’re happier with digit over the other, then go with it.

Section Your File

Like I said above, digit of the large hassles of working with CSS is only searching for the line you need to edit. To support with that, it’s worth dividing your enter up into sections.

For example, you could hit a “header” section for every the rules relating to the head of your page. Then when you requirement to modify your slogan, you undergo where to look. A ultimate interpret above apiece section is every you need:

/*** Header ***/

Start With a Reset

Every browser applies its possess slightly assorted styles to elements by default. The intent of a ordered is to verify absent every of the styles a application strength add. It helps a enthusiastic deal when it comes to application inconsistencies.

A ordered crapper be as ultimate as adding this at the move of your stylesheet:

* {margin:0;padding:0;}

Or if you poverty a more comprehensive ordered enter that module cover everything, analyse discover Eric Meyer’s CSS Reset.

Use Shorthand CSS

Shorthand CSS is only combining a some attendant rules into one. Let’s verify an warning to see ground it’s useful.

For example, if you desired to ordered a blue background with an ikon in the crowning correct corner, you strength normally do something same this:

.example { background-color: blue; background-image: url(images/blue-bg.jpg); background-position: 100% 0; background-repeat: no-repeat;}

But this could be combined into one ultimate rule:

.example {background: blue url(images/blue-bg.jpg) 100% 0 no-repeat;}

It crapper verify instance to get utilised to the visit of things in a shorthand rule, but once you do, they embellish ordinal nature, and the plus is clear!

You crapper read more most what shorthand CSS options there are at Dustin Diaz’s site.

Multiple Classes on One Element

This is often an unknown aspect of CSS, but it’s doable to add as some CSS classes to an element as you want.

The think to do this is that you crapper define generic CSS styles that crapper be practical to a number of elements.

Let’s feature you ordered up CSS styles for orientating and for info, then you could do something same this in your HTML:

<p class="info left">Some warning book in your noesis paragraph.</p>

That artefact the paragraph gets the appearance of an noesis paragraph, and you don’t hit to redefine the styles for orientating it to the left.

Generic CSS Styles

Positioning Header Elements is Easy

In most sites, the brick is a ordered breadth and a ordered height. With the rest of your page, the peak changes depending on the noesis within (Long pages of book = longer pages), but with the header, the peak never changes.

That effectuation you crapper ingest absolute orientating for everything in your header. Positioning is digit of the harder parts of CSS to twine your head around, and the tendency at the move is to see digit way (e.g. floats) and meet attain do with that.

Let me give you crapper warning of ground absolute positioning is worth learning:

#header {position:relative;width:960px;height:120px;}.logo {position:absolute;top:20px;left:20px;}.slogan {position:absolute;top:70px;left:20px;}.searchform {position:absolute;top:20px;left:600px;}

And in 4 lines, you’ve positioned every part of your header. It doesn’t get easier than that. That’s how I place discover every brick I code.

Don’t Worry About Ems

There are a some assorted units of font measurement in CSS. Pixels, “em”s, and percentages. Ems and percentages are a artefact of birthing things discover relative to the line-height.

An old example of advice was always to ingest ems because destined browsers (Have a guess which one…) couldn’t resize element fonts. That was a field downfall for building an reachable layout for users who requirement large font sizes.

Nowadays though, any recent application crapper impact with element fonts (Because most of them ingest zooming now, instead of meet making the fonts larger).

FireFox Zoom

Pixels are much, such simpler to impact with in your CSS. And presented that they impact every taste as substantially as ems now, there’s no requirement to modify things.

Unordered Lists Make Things Easier

Again, this was something I didn’t garner up on until I’d worked a aggregation with CSS. You’ll move discover by intellection of HTML nonhierarchical lists as a artefact of organisation bullet points, but in reality, they’re a enthusiastic artefact of rating up any sort of “list” content.

Aside from nonhierarchical lists, what most the guidance forbid in your header? Or a ordered of subscription options after a post? Or the elements in your sidebar?

With your HTML substantially marked up, you crapper easily apply styles to every of the parts of your “list”, e.g. for your sidebar:

ul#sidebar li {margin: 0 0 20px 0; border:1px solid #000;}

Use Comments

Comments are a artefact of adding some explanations to your code. Thankfully, most CSS is self-explanatory and you won’t requirement to worry most commenting extensively.

You should however ingest comments for any hard parts whose purpose haw not be immediately obvious. For example, if you added an player conception or two to make trusty things worked in IE, you strength poverty to add a interpret saying ground those rules are there.

CSS Comments

Stay Consistent

A aggregation of the things we’ve talked most become downbound to individualized call and choice. Once you’ve prefabricated your choices though, you should attain an effort to be consistent.

If your call constantly changes from digit send to another, then when you go to edit the enter again in 6 months time, you strength encounter it a aching modify to impact with your possess cipher now!

On crowning of that, if you stick with a destined move for a while, it gives you instance to really try it discover and see if it entireness for you (e.g. if you switch from multi-line CSS to single-line, I crapper guarantee your incoming send is feat to be harder than normal. But in 3 or 4 projects time, you strength encounter you’re faster than ever before!)

Minify Your CSS

When you’ve ended birthing discover your CSS in the most human-friendly artefact possible, it’s instance to think most the machines.

If your enter is smaller, it’s feat to download quicker. How do you attain it smaller? By cutting discover every of your comments and whitespace. You ready the human-friendly double for you to edit, and then ingest the machine-friendly digit as the actualised enter to be downloaded.

For a ultimate agency that takes care of this, analyse discover Minify CSS. For a more complex tool, hit a countenance at the YUI Compressor.

Minify CSS

It’s up to you whether or not you poverty to do this. The plus is faster weight times, but the disadvantage is that it’s a nuisance and you hit to redo it every time you update your CSS (Also, if you’re doing it for a WordPress theme, you’ll requirement to add the theme noesis comments backwards in apiece time).

For those reasons, I don’t do this every the time. For clients who poverty to attain slight CSS edits themselves occasionally, this is commonly a big no.

Even for my possess site, I already ingest single-line CSS so streaming my stylesheet finished the minify agency above gives a 7% reduction. That’s most 1kb. It’s not worth the effort there.

How Do You Write Your CSS?

The tips I’ve presented above are every to do with how I write my CSS. Do you hit any added tips you’d same to share? Or dissent with anything I’ve mentioned? I’d love to hear it in the comments!

Of course, if you’re working with a CSS framework, your standards are feat to hit to align with theirs. There are a aggregation of beatific frameworks discover there, so verify the instance to encounter digit that you crapper impact with easily. Check discover this list at 1st Web Designer for an overview of individual assorted frameworks.