Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this CSS ID selector include the element type?

in some CSS code I found out this type of selector

div#someid
  1. Is this formally correct?

  2. If the answer to (1) is YES, what's the need for the div selector before the #someid, shouldn't the id be unique in a valid web page?

Thanks!

like image 552
Marco Demaio Avatar asked Mar 11 '10 12:03

Marco Demaio


2 Answers

  1. Yes it's correct.
  2. It might be because it makes the selector more specific. The more specific a selector it is the higher priority it is.
like image 129
Colin Newell Avatar answered Oct 21 '22 08:10

Colin Newell


It is fine.

  • The stylesheet might be reused between pages which have the id on different elements
  • The explicit type provides information for the maintainer about the element
  • It makes the selector more specific, e.g. to override #other div.
like image 31
Quentin Avatar answered Oct 21 '22 07:10

Quentin