Subscribe to RSS

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.

Popularity: 6%

Whats Next?

  • Save to Delicious! Save the page

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

  1. Gordon Mackay Says:

    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. Mattias Lundmark Says:

    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. Mike Robinson Says:

    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.

Leave a Reply


Recent comments

I’ve been on a vacation, normality will happen next week

On August 28, 2008, holiday travel wrote:

well this is useful… (at least for me) very thanks ————&# 8212;———R 12;—–...

Top 10 search engine optimization techniques

On August 26, 2008, ибп wrote:

А что, неплохо!

Styling the first post of your Wordpress blog

On August 21, 2008, cheap website design wrote:

good point you have in there! thanks for sharing with us

CSS Hack:Getting Safari to behave

On August 21, 2008, Glass wrote:

You can also target Safari with the following: body:first-of-type .class {} That is: just prepend css the rule with body:first-of-type. Simple.

Styling the first post of your Wordpress blog

On August 20, 2008, jeeremie wrote:

Hi, This solution didn’t work for me. Instead, I use this one »