Old 18-09-2007, 21:40   #1 (permalink)
Jeff-in-Denver
Registered User
 
Join Date: Sep 2007
Posts: 4
Stupid CSS Question

I have a question regarding CSS.

Quick background: I have no formal training. Self taught via trial and error. I consider myself at intermediate level.

Question: Can someone explain to me the difference between these two:

div#nav {
width: 25.5%;
padding: 10px;
margin-top: 1px;
height: 100%;
float: left;
}

#nav {
width: 25.5%;
padding: 10px;
margin-top: 1px;
height: 100%;
float: left;
}

The page works when I use the "DIV#" in the style, but not when I just use the "#" by itself. My page now works. I am curious as to know why...what changes by putting "DIV" in there?
  Reply With Quote
Old 18-09-2007, 21:46   #2 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,334
Specificity. It's the "cascading" part of "CSS". Assigning property values, Cascading, and Inheritance

div#nav has a slightly higher specificity than #nav.
  Reply With Quote
Old 18-09-2007, 22:43   #3 (permalink)
Jeff-in-Denver
Registered User
 
Join Date: Sep 2007
Posts: 4
Thanks. Not sure why it changed anything...the ID was being applied only to DIVs anyway.

Is there a reason you would specify the syle to DIVs as opposed to just leaving it generic?
  Reply With Quote
Old 18-09-2007, 23:23   #4 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,334
Well, say you want all elements that have #something to be green.

#something { color: green; }

But on some pages, your "#something" is a <p>. You want those to be red.

p#something { color: red; }

Now, all "#something" on any given page is going to be green - unless that "#something" is a <p>.

Granted, this isn't a terribly common occurance.
  Reply With Quote
Old 20-09-2007, 21:30   #5 (permalink)
Jeff-in-Denver
Registered User
 
Join Date: Sep 2007
Posts: 4
Ahhh...so it is an exception thing...there is no real reason to use "p#something" unless you already have a "#something" defined. Right?

That does make sense. It doesnt explain why my problem was fixed, but it is still good to know I can do that. I never knew that before.
  Reply With Quote
Old 20-09-2007, 22:20   #6 (permalink)
pgo
Senior Member
 
Join Date: Jan 2005
Posts: 12,334
Well, I guess it could also serve as a reminder as to what kind of element is getting the ID.
  Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive
vBulletin © 2000-2009 Jelsoft Enterprises Limited.
Search Engine Optimization by vBSEO 3.0.0 RC8