Darwyn Posted December 23, 2009 Share Posted December 23, 2009 Is there a difference between the following div tags...#menu {color:red}anddiv#menu {color:red}In other words, can "#" and "div#" be used interchangeably? Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2009 Share Posted December 23, 2009 They are identical. I like the longer version because it helps me remember what an id is referring to. And since the second is more specific than the first, if you ever got in a situation where one combination of selectors was overriding another, the more specific one would win. That doesn't come up unless you're using a lot of CSS. Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2009 Share Posted December 23, 2009 # declares a unique ID. By setting div#menu, it will only effect a div with that id, where as menu alone could be applied to any singular element with that ID.I think... Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2009 Share Posted December 23, 2009 But since an id is supposed to be unique . . . Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2009 Share Posted December 23, 2009 if it was just menu, then couldn't you use it on other elements? I was thinking that div#menu would force the style to a div. Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2009 Share Posted December 23, 2009 I guess you're right in that they are the same thing Link to comment Share on other sites More sharing options...
Darwyn Posted December 23, 2009 Author Share Posted December 23, 2009 I guess you're right in that they are the same thingThanks! Link to comment Share on other sites More sharing options...
Synook Posted December 24, 2009 Share Posted December 24, 2009 If, for some reason, the stylesheet would be applied to some pages with divisions identified with "menu", and other pages with other elements identified with "menu", then you could use the more specific selectors to define differing behavior. You can't have two elements with the same ID on a single page, however. div#menu { margin-left:0px;}ul#menu { margin-left:10px;} Also, the div#menu selector has a higher precedence, because it is more specific. div#menu { color:green;}#menu { color:red;}/* the color of <div id="menu"> is now green */ Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.