Your last CSS attribute doesn’t need a semicolon

I actually found something out recently that I thought I would share with everyone. Somewhat of a minor little detail about CSS but still interesting none the less. It could come up, or possibly change in later versions of CSS but right now both ways are going to validate just fine. It turns out that when you end your last CSS attribute for a selector you don’t have to have your semicolon on the end of it.It doesn’t really make much of a difference either way, I personally won’t change how I write my CSS just to ensure that I’m in the habit of always ending my CSS selectors to avoid erroneous errors. The idea is pretty straight forward and not something to spend too much time talking about. I have an example below to show you what I am talking about.

How it is all done with CSS


h1 {font-size: 22px; color: #000;border-bottom: 1px solid #ccc}

If you’ll notice I didn’t put the last semicolon on to end of the selector for the last attribute, pretty simple and straight forward but a definite interesting quark about CSS. I’m not sure this question will be on jeopardy anytime soon but a cool little did-you-know fact.

Whats Next?

  • Save to Delicious! Page saved 0 times

9 Responses to “Your last CSS attribute doesn’t need a semicolon”

  1. I think the operative word is “habit” in this case, and web developers certainly adopt more habits than most people… fortunately they don’t cost much and are unlikely to give you a nasty rash :)

  2. Chihn says:

    I did not know this but I think that I will not use it so like Mattias said I won’t get any errors.

  3. Leaving out the last semi-colon is actually a habit I actively try to break. I’ve found out the hard way that the errors caused by forgetting to add it in when adding a new instruction outweighs the small reduction in file size that you’ll achieve.

  4. I’ve been getting into the habit of doing this, it’s handy for a minor trimming of the file size. Just have to remember, if you are adding a new attribute, don’t forget to add the semi-colon to the old, last attribute!

  5. Francisco Blanchart says:

    I knew this from the start and at first (as the obsessive clean-coder I was) I even spent time cleaning the last declaration (better than “attribute”) in every rule from their semicolons.

    Presently I always make sure that every declaration that I add ends nicely with its semicolon, after hours of head-scratching before realising that the problem was in that one that I added below the semicolon-less one that was supposed to close the rule.

    My years of experience have taught me that CSS styling is often a trial-and-error job in which you have to add and remove declarations until you get what you want to achieve and the effort of being constantly aware of the semicolons just to save a bunch of bites in the code is not worth it.

  6. Kelly McCathran says:

    As a teacher, I highly encourage people to build good habits. Practice doesn’t make perfect, it can make bad habits permanent. It is far better to be safe and have a habit of the semicolon than to troubleshoot all the errors it may introduce.

    Grammar police: Sorry, I’ll sleep better if I add this. I think you mean quirk, not quark (the sub-atomic particle and page layout program)

    ;)

  7. Kelly McCathran says:

    Another side note, the semicolon is equivalent to pressing return or enter in UNIX and some coders like to have every value on a separate line (instead of one massive line). This helps separate each and verify that you didn’t miss anything.

  8. Joe says:

    It’s definitely a very bad habit to ignore the semicolon at the last attribute. Problem occurs when you added another attribute at the end and forget to add the semicolon before it.

  9. barry gorbes says:

    Great information thanks for getting this out there for people like me to read.

Leave a Reply


About Dustin Brewer

Dustin Brewer

Dustin Brewer is a freelance web designer based out of Oklahoma City, OK passionate about web standards, and beautiful web design. Dustin Brewer has been in the web design industry for over 8 years through freelance and professional experience. If you are interested in hiring Dustin Brewer please visit the web design services page to find out more information. You can also checkout the web design portfolio.

Contact Information

© 2009 Dustin Brewer Design. All Rights Reserved.
Located in Oklahoma City, OK 73170