Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML How to Make a Website CSS: Cascading Style Sheets Use ID Selectors

Vladislav Kobyakov
Vladislav Kobyakov
9,439 Points

Does the semicolon have to be added after each declaration in a rule or is it optional?

I forgot to write a semicolon after the changing background declaration but still managed to change the background color though. Thus, the question if it has to be there or nor?

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Vladislav Kobyakov

In CSS, semicolons are needed to separate each statement ... However, the final semicolon in a set of statements is optional (but most developers recommend using it).
In your case, there was only one statement, so it is technically the last and thus, the semicolon is technically not needed.

So, you could have either of the following, and both are valid:

h1 {
  font-size: 24px;
  color: #56cd34;
  text-align: center;            /*with the semicolon*/
}

h1 {
  font-size: 24px;
  color: #56cd34;
  text-align: center             /*without the semicolon*/
}

Like I mentioned above, most developers strongly suggest that you do use the final semicolon, because this will help to eliminate potential errors if you were to add or modify the CSS. If you don't have the final semicolon, and then you add another statement without adding the semicolon, it will error. So, while it is not needed, it is recommended.

Hope that helps to clear it up for you. :)

Keep coding! :dizzy:

Steven Ang
Steven Ang
41,751 Points

You did a fantastic job at explaining my thought process! I couldn't fully explain everything out of my head and put it on paper... Though unintentional you certainly helped both of us here! Though I agree you could just omitted the final semi colon, but I think we should follow the web standard to write actual validate HTML and CSS and personally I think it's plain laziness to just not close it properly. Not here to argue, it's just my opinion. With that out of the way, you deserve the best answer for giving a great detailed answer.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Thank you Steven Ang!

And I do agree with you, the final semicolon should always be used. :)

Steven Ang
Steven Ang
41,751 Points

The browser is very forgiving, it will let you pass many common mistakes you would make i.e forget to put a semi-colon at the end and type image instead of the proper image tag - img. In conclusion, you must put a semi-colon at end of each declaration even if it let you pass, write validate HTML and CSS.