Forum Moderators: not2easy

Message Too Old, No Replies

The Best CSS Syntax

         

Tonearm

2:05 am on Nov 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can anyone suggest the best syntax in which to write CSS, all things considered? I'm thinking it should validate, use as few characters as possible, and work across browsers. I'm wondering about things such as commas between multiple declarations,

h1,h2,h3 {...}

spaces, semi-colons on the very end, specifying individual border widths vs. all-in-one, and shorthand techniques of all kinds.

Also, is it necessary to have both of these:

* {padding:0; margin:0; border:0;}
.* {padding:0; margin:0; border:0;

sonjay

4:15 am on Nov 26, 2004 (gmt 0)

10+ Year Member



commas between multiple declarations,
h1,h2,h3 {...}

Yes, these are necessary, because without the commas the above style declaration would apply to h3 tags inside an h2 tag which is inside an h1 tag.

semi-colons on the very end

I like to use a semi-colon after my last item so that if I add more declarations later, or move them around, or whatever, I don't have to remember to add the semi-colon. It's not necessary, though, simply a practice that I find helpful.

Using shorthand, individual border widths, etc. I vary depending on what properties I'm setting.

The * and .* I dunno if they're both needed. I'd hazard a guess that using both might be needed because of some failure on the part of Explorer. Much of the redundancy in css is needed for that reason.

Guilt_Puppy

4:31 am on Nov 29, 2004 (gmt 0)

10+ Year Member



"use as few characters as possible"... I'd agree with that, but place it after two other priorities which will tend to contradict it:

- First, use meaningful names. Something like "#nb" may be quicker to type throughout your code, but when you return to edit it after months or longer, you'd really rather be looking at something like "#navbar", at least. (Actually, I prefer "#navigation", since it may not take the shape of a bar in your next design.)

- Second, only use shorthand notations (ie border: 1px black solid) when you're pretty sure you won't be tooling around with the individual attributes, ah, individually at a later date... Remember, when you do change things, you want to change them one thing at a time (to figure out if and where it breaks) -- while shorthand doesn't prevent you from doing this in any way, it doesn't match the process syntactically. A minor point, but still helpful enough to make a habit of.

Generally, readability should come before space economy. Stuff like:

#show { text-indent: 40px; width: 500px; margin-left: auto; margin-right: auto; border-left: 1px black solid; border-right: 1px black solid; padding: 6px; padding-top: 0px; text-align: justify; }

...may validate, and it may take up less space (though if you're keeping your CSS in .css files, I don't know why you should worry about it taking up too much visual space), but it doesn't lend itself to future modification or reuse at all. A few extra new-lines aren't going to choke your bandwidth, but they will save you time and stress when you decide that hey, this element would look a bit better like this...

Tonearm

6:40 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Guilt_Puppy -

I'm not sure I agree that readability should come before space economy because of search engines calculating your content/code ratio and using that in your ranking. I actually keep my CSS declarations right in the <head> so there is no pop-in when a user comes to my site for the first time.

One other thing. I've noticed a lot of people use the # in front of a CSS declaration. Is that necessary?

Robin_reala

8:30 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



# means that the word following is an id (as in <div id="whatever">). If you don't put it in the the selector is a tag name.

Tonearm

9:20 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it, thanks!

MatthewHSE

1:24 pm on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course, you can also use classes instead of ID's:

#exampleID {}
.exampleClass {}

Classes and ID's work exactly the same as far as styling goes, but there is one major difference:

An ID can only be used once per page. They're useful for designating page elements such as "Content," "Navigation," etc.

Classes can be used as many times on a page as necessary. So, they're good for designating page elements such as "p.small" (for "small" type in paragraphs) or ".notice" (which might be useful to apply to any element that you wish to stand out).

You don't need to specify an element. You can specify ID's and classes by specifying the element or leaving the element out of the selector. Here's an example using ID's:

div#Content {}
Selects
<div id="Content">

#Content {}
Selects any element where
id="Content"

And likewise for classes:

p.small {}
Selects every instance of
<p class="small">

.small {}
Selects all elements where
class="small"

I tend to prefer leaving the elements off my ID and class selectors, so I can choose what fits best on each page. For instance, I often set up a rule that simply needs to be applied to a block-level element. Then, I know that I can use whatever is semantically best on each page.