What are the advantages of combining a type select

2019-06-06 13:57发布

问题:

This is extremely simple but I am wondering why people write this?

div#some_id

You can only have one ID of that name "some_id" on a webpage. So why be specific with it? Is it for readability purposes?

回答1:

There are a couple of possible reasons:

  • It is a more specific selector
  • It tells people reading the stylesheet that the selector is aiming at a div element
  • The same id could be applied to different element types on different pages that use the same stylesheet (this usually causes more confusion then benefits though, so I wouldn't advise that approach).


回答2:

You're right, but this would mean a div with an ID of some_id. (More correctly, an element with an ID of some_id, which is also a div).

This grants a higher specificity value, but you're right. It's usually worthless with IDs, more useful with class names.

For example:

div.large { width: 500px; /* 500px is a large div */ }
input.large { width: 100px; /* 100px is a large input */ }

Same class name, different results.



回答3:

The reason this is done is mainly for readability in the stylesheet.

As you can only have 1 id on a page it makes sense that you would not need to specify which type of element it is.