Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaving out the last semicolon of a CSS block

Tags:

jquery

syntax

css

A couple of questions concerning this:

  • Is it good practice?
  • Will it, on a large scale, result in better load times?
  • Can it result in browsers 'breaking'?
  • Is the same true for the last function in JavaScript (/jQuery)?

What I mean is stuff like this:

#myElement {   position: absolute;   top: 0;   left: 0 } 
like image 713
Bram Vanroy Avatar asked Aug 13 '12 17:08

Bram Vanroy


People also ask

Is semicolon mandatory in CSS?

No, semicolons are only required to separate rules in CSS blocks. Semicolons are delimiters, not terminators.

What is the semi colon used for in CSS?

In CSS, the colon separates syntax, and the semicolon denotes that that particular styling is over.

Do you need semicolon after break?

Not every line break needs a semicolon. Instead, a linebreak that cannot be parsed without a semicolon requires a semicolon.

Why would someone use a semi colon when writing a JavaScript?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code.


2 Answers

Is it good practice?

It's not good practice to manually exclude semicolons. This is purely because it's easy to overlook when adding more styles, especially if you're working in a team:

Imagine you start with:

.foo {     background-color: #F00;     color: #000             <-- missing semi-colon } 

And then someone adds some styles:

.foo {     background-color: #F00;     color: #000             <-- missing semi-colon     width: 30px;     z-index: 100; } 

Suddenly the other developer is wasting time figuring out why their width declaration isn't working (or worse yet, doesn't notice that it's not working). It's safer to leave the semi-colons in.

Will it, on a large scale, result in better load times?

Most definitely, for every block, you'd save a couple of bytes. These add up, especially for large style sheets. Instead of worrying about these performance gains yourself, it's better to use a CSS compressor, such as the YUI Compressor to automatically remove the ending semi-colons for you.

Can it result in browsers 'breaking'?

No, it's safe, as browsers implement this part of the specification correctly. The CSS2 specification defines a declaration thusly:

A declaration is either empty or consists of a property name, followed by a colon (:), followed by a property value.

More importantly:

...multiple declarations for the same selector may be organized into semicolon (;) separated groups.

This means that ; is used to separate multiple declarations, but it is not needed to terminate them.

Is the same true for the last function in JavaScript?

JavaScript is a whole different beast with a completely different specification. This particular question has been answered in depth many times before on Stack Overflow.

like image 141
zzzzBov Avatar answered Sep 22 '22 21:09

zzzzBov


  • No, leaving out that semicolon will introduce a great amount of risk in your application, it will be all too easy to overlook adding it back if you add more styles to your element. At that point, you are relying on your manual processes and attention to detail to ensure you didn't accidentially misplace on of your non-semicolon lines. Worse yet, you would have to physically check your css file every time you were ready to go to production to make sure you didn't screw up any of the final style lines in each element.

  • Possibly, since the file would be smaller, but the difference should be negligible. If you are worried about load times, Gzipping your files before placing them on the server will serve you well.

  • Most browsers are smart enough to know what you mean, but you still have to worry about screwing up your CSS file by not being careful about the last style.

like image 31
Robert Greiner Avatar answered Sep 21 '22 21:09

Robert Greiner