Why Doesn’t My CSS Work? Five Quick Fixes.

large hammer

Imagine: You’ve been working on your brand new, beautifully cascading style sheet for most of the day. You save your work, load it into a browser, and… what the heck?! Nothing’s working right! You know you didn’t make any huge errors in your code, but something is obviously wrong.

We’ve all been there before. But instead of wasting endless hours debugging your code, here are a few very simple things you can check in your CSS before you start pulling out your hair.

Closing brackets are notoriously easy to miss, especially if you don’t put them on a line all by themselves (and I usually don’t). Missing a single closing bracket can throw off your entire website, because the browser quits loading your rules as soon as it encounters the malformed code.

1
2
3
4
5
6
7
.sidebar {
	width: 200px;
	background-color: #333;
.sidebar h3 {
	/* None of these rules are applied! */
	font-size: 1.4em;
	color: #fff; }

A missing semicolon can likewise cause your code to start behaving wonky. CSS specifications state that you don’t need a semicolon on your very last rule before your closing bracket, but in my experience that’s a great way to set yourself up for some heartbreak. As soon as you decide to add one more rule to that property, that missing semicolon is going to rear its ugly head.

1
2
3
4
5
6
.sidebar {
	width: 200px;
	background-color: #333
	/* The following rule won't render! */
	padding: 5px;
}

Is there any chance you have a misspelled class or ID? It may sound obvious, but I’ve spent far too much of my life trying to debug my CSS just because I misspelled something along the line. This, if anything, is a good reason to stick to whole words (or at least common abbreviations) when writing your CSS classes and IDs – it’s going to be easier to notice a misspelling in the “leftSidebar” class than it is the “ltSdbr” class.

1
2
3
4
5
.siedbar {
	/* Dunno what a siedbar is, but I'm
	   pretty sure it's not in my XHTML. */
	width: 200px;
}

Misspelled properties or values have also caused me more than their fair share of trouble. There are certain words I tend to misspell when I’m typing too quickly, and it just so happens that there are a lot of CSS properties with related names. I don’t care how much I’m “in the zone,” there’s still about a 50% chance I’m going to try to apply a “bakcground-color” somewhere along the line.

1
2
3
4
5
.sidebar {
	/* The following property does exactly 
	   bupkis. Dyslexics Untie! */
	bakcground-color: #333;
}

Bad CSS values can be a bear to hunt down, particularly if you’re convinced that the value you’re using really exists. Some invalid values are more obvious than others – a background-color of “1px” would be relatively easy to spot, but what about a vertical-align of “center,” like I mentioned last week? We tend to think of things as being either aligned to the top, center, or bottom of a space… but CSS classifies that second option as the “middle,” not the center. An alignment of center won’t create nearly the presentation you’re expecting.

1
2
3
4
.sidebar img {
	/* This image isn't really going anywhere. */
	vertical-align: center;
}

If you start your CSS debugging by looking at these five common problem areas first, there’s a good chance you’ll save yourself a whole lot of time – and a massive headache to boot. If you have any other ideas for common areas of CSS misstep, offer them up in the comments!

5 Comments

  1. On March 12, 2008
    9:28AM

    Justin said:

    How about missing units on non-zero values? padding: 3; isn’t going to fly in strict mode.

  2. On March 12, 2008
    9:43AM

    Scott Phillips said:

    Also, running your code through a CSS validator can help track down a problem quickly.

  3. On March 12, 2008
    10:14AM

    Rob Glazebrook said:

    Both great suggestions. :) Thanks, guys! If I get enough submitted tips, I’m hoping to put together an article in the future consisting of user-submitted tips (with attribution, of course!). Keep ‘em coming!

  4. On March 13, 2008
    2:01AM

    Custom Wordpress Designs said:

    Some great tips. I usually run my css file through a validator to check on things, but a lot of times I notice I forget to close out comments in the css file.

    /* COMMENT HERE */

    It’s supposed to look like above, but I always end up with:

    /* COMMENT HERE /*

    This will throw everything off as well.

  5. On June 24, 2009
    5:12PM

    BlackSheep said:

    Thanks, Checking your own mistakes sure helps :)

9 Responses Elsewhere

  1. Pages tagged "css" said:

    [...] tagged cssOwn a Wordpress blog? Make monetization easier with the WP Affiliate Pro plugin. Why Doesn’t My CSS Work? Five Quick Fixes. saved by 4 others     tmoney2388 bookmarked on 03/13/08 | [...]

  2. 10 errori comuni nello sviluppo dei CSS : cssblog.it said:

    [...] possono allungare di molto i tempi di sviluppo. Due ottimi articoli di Elementary Group Standards e CSSNewbie raccolgono 10 errori comuni che si possono fare nello sviluppo dei fogli di stile: conoscerli [...]

  3. On May 20, 2008
    7:59AM

    TechBlogy | BlogSphere of Technology said:

    [...] why doesn’t my css work? five quick fixes [...]

  4. Why doesn’t my CSS work? said:

    [...] It seems that I am always banging my head against the CSS wall screaming “What is wrong with my CSS code?” and “Why doesn’t it work?” Today I came across the blog CSSNewbie and they had a nice article on the top five reasons why CSS doesn’t work. [...]

  5. 15 Surefire Ways to Break Your CSS said:

    [...] Newbie reader Justin reminded me of this problem the last time I wrote about CSS faux pas. With only a few limited exceptions, all measurement values in CSS need a unit of measurement [...]

  6. On May 07, 2009
    7:34PM

    15 Surefire Ways to Break Your CSS « Internet Turnkey Websites said:

    [...] Newbie reader Justin reminded me of this problem the last time I wrote about CSS faux pas. With only a few limited exceptions, all measurement values in CSS need a unit of measurement [...]

  7. Colorrage Blog » Blog Archive » 15 Surefire Ways to Break Your CSS said:

    [...] Newbie reader Justin reminded me of this problem the last time I wrote about CSS faux pas. With only a few limited exceptions, all measurement values in CSS need a unit of measurement [...]

  8. 15 Surefire Ways to Break Your CSS « Why Limit Media said:

    [...] New­bie reader Justin reminded me of this prob­lem the last time I wrote about CSS faux pas. With only a few lim­ited excep­tions, all mea­sure­ment val­ues in CSS need a unit of [...]

  9. 15 Surefire Ways to Break Your CSS | CSS Heaven said:

    [...] Newbie reader Justin reminded me of this problem the last time I wrote about CSS faux pas. With only a few limited exceptions, all measurement values in CSS need a unit of measurement [...]

Leave a Comment