Connecting Tech Pros Worldwide Forums | Help | Site Map

CSS Compliance and Convention...

Newbie
 
Join Date: Oct 2008
Posts: 4
#1: Oct 1 '08
In a stylesheet, what is the difference in writing:

div.class
{
styles:styles;
}

VS.

.class
{
styles:styles;
}


Is it just compliant to use the div.class, or span.class, or div#id, etc. when marking up your CSS? I want to a good argument for my teammates as to what convention we should use in our CSS.

Thanks,
kabrenica
sashi's Avatar
Expert
 
Join Date: Jun 2006
Location: Seremban, Malaysia
Posts: 1,630
#2: Oct 1 '08

re: CSS Compliance and Convention...


Hi Kabrenica,

In a very simple way let me put it as "HTML is used to structure content. CSS is used for formatting structured content".

Kindly refer to links below to get a better understanding of what is CSS all about and how CSS can be married to HTML. Happy coding.

http://www.w3schools.com/css/
http://www.html.net/tutorials/css/
Newbie
 
Join Date: Oct 2008
Posts: 4
#3: Oct 1 '08

re: CSS Compliance and Convention...


Quote:

Originally Posted by sashi

Hi Kabrenica,

In a very simple way let me put it as "HTML is used to structure content. CSS is used for formatting structured content".

Kindly refer to links below to get a better understanding of what is CSS all about and how CSS can be married to HTML. Happy coding.

http://www.w3schools.com/css/
http://www.html.net/tutorials/css/


Thanks for these great links!

But I was wondering more about the necessity of stating the selector that you plan to use the class with.

For instance, let's say the html is:

<div class="header">Header Text</div>

AND the CSS is:

.header
{
font-size:12px;
color:#333;
}

OR I see it written as:

div.header
{
font-size:12px;
color:#333;
}

Both css declarations work, but which one is more compliant? What's the difference between explicitly stating the selector that is going to use the class declared and not stating it? This is of course assuming that the header class will also be reference with a div tag.

Does that make more sense?
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,562
#4: Oct 1 '08

re: CSS Compliance and Convention...


There is no difference and it's a matter of personal preference.
Newbie
 
Join Date: Oct 2008
Posts: 4
#5: Oct 1 '08

re: CSS Compliance and Convention...


Quote:

Originally Posted by drhowarddrfine

There is no difference and it's a matter of personal preference.

Ah, ok, thanks a bunch!
Expert
 
Join Date: Aug 2008
Posts: 397
#6: Oct 1 '08

re: CSS Compliance and Convention...


Quote:

Originally Posted by kabrenica

Both css declarations work, but which one is more compliant? What's the difference between explicitly stating the selector that is going to use the class declared and not stating it?


Both are valid and both are compliant.
There is a difference between them.

The difference is:
div.whatever {...}
has higher specificity than
.whatever {...}

The same is true if it were an id:
div#whatever {...}
has higher specificity than
#whatever {...}
Newbie
 
Join Date: Oct 2008
Posts: 4
#7: Oct 1 '08

re: CSS Compliance and Convention...


Quote:

Originally Posted by David Laakso

Both are valid and both are compliant.
There is a difference between them.

The difference is:
div.whatever {...}
has higher specificity than
.whatever {...}

The same is true if it were an id:
div#whatever {...}
has higher specificity than
#whatever {...}


YES, that exactly the answer I was looking for!! Thanks for your great knowledge transfer..
-K
Reply


Similar HTML / CSS bytes