Your last CSS attribute doesn’t need a semicolon
December 3rd, 2007 by Dustin Brewer
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%








December 3rd, 2007
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 :)
December 3rd, 2007
I did not know this but I think that I will not use it so like Mattias said I won’t get any errors.
December 3rd, 2007
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.
December 3rd, 2007
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!
December 26th, 2007
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.