Hommer Smith
Hommer Smith

Reputation: 27852

Why put type_of_element#id in CSS?

Why would you want to do:

h1#chapter1 { text-align: center }

Instead of

#chapter1 { text-align: center }

If an id is supposed to be unique in a document tree?

Upvotes: 0

Views: 51

Answers (3)

Chris Herbert
Chris Herbert

Reputation: 6285

Usually it is done to make it easy to tell exactly what sort of element has the ID without having to leave the stylesheet. I think would also have a higher specificity than just #element, so you could override a standard #element selector with div#element.

Upvotes: 1

cowls
cowls

Reputation: 24334

Strange question, perhaps you have the same id on different elements across different pages that share the same stylesheet?

E.g. on home.html you have <h1 id="title">

on about.html you have <h2 id="title">

But they both use same stylesheet and you want to apply a style for the homepage title only..

Also it wouldnt make sense to make the CSS syntax inconsistent across different kind of selectors. Would cause more confusion then it was worth.

Upvotes: 0

Christopher Marshall
Christopher Marshall

Reputation: 10736

I do that to let the next developer know what element the ID is on.

I do not do that on divs, sections, or any "containers".

Upvotes: 0

Related Questions