Jump to content

The use of '#'


sepoto

Recommended Posts

I read a book on CSS not too long ago and I always thought that the use of '#' in CSS titles were meant as a reference to an ID. This case of the use of '#' looks so different to me I feel like I must ask a question to clarify the code.

div#menu {background:#EEE;height:1.5em;margin:1em 0;width:100%;}

Thank you!

Link to comment
Share on other sites

div#menu targets a div with id reference of #menu, but because it should be unique within a page, you could have easily used #menu on its own. I suppose it maybe used to identfy which element it is used on, or give a location as to where it will be used, as it could be used for UL which is mainly used to create dropdown menus.

Link to comment
Share on other sites

I sometimes like to use that format so that I can look at the CSS alone and know what kind of element is being described. The redundancy inefficient, but trivially so, and the advantage to the developer can be large.

Link to comment
Share on other sites

Great answers. Hopefully some time coming up I'll be able to answer some questions on this forum instead of asking so many of them. Thank you all.

Link to comment
Share on other sites

There's also a technical reason for this syntax's existance, though present mainly for completeness' sake.A single CSS file can be applied on multiple (X)HTML files. An ID is only unique per single (X)HTML file. In theory, one page may have an ID on one kind of element (say, "div"), while another page could have the same ID on another kind of element (say, "h1"). In practice, that's rarely, if ever, the case, but because the theoretical possibility exists, the syntax is here to accomodate cases where you need to target only one kind of an element with this ID.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...