4

I've been working with CSS for sometime and I see that in a lot of tutorials and exercises the last line of a CSS tag has no ";" added to it. For example:

ul li:hover ul { 
    display: block
}

ul li ul {
      list-style: none;
      position: absolute;
      display: none;
      width: 104px;
      height: 100px;
      background-image: url(../_img/nav/dropdownbg.1.png);
      background-repeat: no-repeat;
      background-position: center;
      background-position: top
}

As you can see there are two examples. One with only one line and another with different commands in one tag. I would like to know why and which reason is there to do this?!! At school they didn;t tell us anything about this. I just close the last or only line with and ";" and it works perfectly.

So, which is the reason to do this? Is it more safe?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Daniel Ramirez-Escudero
  • 3,877
  • 13
  • 43
  • 80

5 Answers5

6

Šime Vidas' response to a related question is also valid here. The semicolon is a delimiter so the final property does not need to be delimited.

However, I would not recommend it, because in the scenario that you need to add another property to the end of a CSS rule it is far more likely that you'll miss out the need for a semicolon and your CSS will break.

Community
  • 1
  • 1
Mike B
  • 12,768
  • 20
  • 83
  • 109
3

The specs say

ruleset     : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;

which, as you can see does not imply terminating ; in curly braces.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

The semi-colon is a delimiter: It separates things. The last item doesn't need to be separated from anything that follows.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
1

The semi-colons separate the different lines, so if you have reached the last line, there is no need to put in a semi-colon, because you will see a brace which not only closes the line, but also closes the block.

I personally would prefer to put in a semi-colon anyway, but you're free to just leave it out.

Also check out; http://www.pagetoscreen.net/journal/item/the_missing_semi-colon/

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

Since it is the last line in the rule there is no chance for ambiguity between it and the next rule because there is none. That's why it is ok to leave the last semi-colon off. Otherwise it offers no benefits or performance gains.

John Conde
  • 217,595
  • 99
  • 455
  • 496