Forum Moderators: not2easy

Message Too Old, No Replies

CSS Coding Standards

Question 1

         

IanTurner

4:38 pm on May 22, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Okay I need to put in place some CSS coding standards as people who do work for us come up with some real wierd and wonderful stuff.

Question 1:

As a basic standard which way would you go.

given that we have elements
#mydiv
p

to provide a specific style for p tags in #mydiv would you

a)
set up a class p.mydiv

b
define #mydiv p

and why?

swa66

8:11 pm on May 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



classes and id-s both have their reason to exist, I doubt you make it better by forcing either way, in fact you'll loose a lot.

IanTurner

8:28 pm on May 22, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Yes I know they are both valid and have their reasons to exist. I'm trying to set out some guidelines for less experienced people, so use a unless you have a good reason for using b or the other way round - but please don't use both to do the same thing and make it difficult for the next person who comes along to maintain the code.

Receptional Andy

8:32 pm on May 22, 2008 (gmt 0)



#mydiv p

That covers every paragraph within #mydiv, without requiring an additional class to be added to them in the HTML - and adding paragraphs is likely a common occurrence. Assuming you want the CSS to affect every paragraph, I'd consider that reason enough. IMO the other way creates un-needed classes.

londrum

8:36 pm on May 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...and if you only want to affect the first paragraph in the div you can always do stuff like this

#mydiv p
#mydiv p+p

most people's idea is to try and use as little HTML as possible, and as few classes. it doesn't matter if your css grows a bit as a result because it gets cached

topr8

8:39 pm on May 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>b)
>>define #mydiv p

because, no extra code required, i personally like working with the 'cascade' so to speak, that is the whole point of css, it keeps the page code cleaner and more manageable.

Receptional Andy

8:43 pm on May 22, 2008 (gmt 0)



A situation where I might choose classes would be if within #mydiv there was the possibility of more than one paragraph that required different styling - let's say those paragraphs need to be highlighted in some way, and are not predictable in where they appear or the number of them.

topr8

9:51 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>there was the possibility of more than one paragraph that required different styling - let's say those paragraphs need to be highlighted in some way

yes, kinda, but in that case i would seriously consider using something like blockquote or another suitable tag rather than a paragraph class, it is highly likely that it would be symantically corrrect too.