Forum Moderators: not2easy

Message Too Old, No Replies

Naming convention

whats a good one then?

         

bosshog

8:43 am on Jun 8, 2005 (gmt 0)

10+ Year Member



Hi,
I'm redoing my website with CSS , so lots to learn and experiment with.

However I noticing that I need to come up with a good naming convention for all the CSS elements.
What do you think is the best way?

Moby_Dim

11:01 am on Jun 8, 2005 (gmt 0)

10+ Year Member



w3 recommends to be descriptive. css usually not too voluminous to use hungarian convention or smth like this.

katana_one

12:23 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Group your page content with DIV tags with descriptive, unique IDs (such as "nav" or "header") and then use those IDs in your CSS to target elements within those DIVs.

Such as:
#nav { some style }
#nav a:link {some style}
#nav a:hover {some different style}

instead of:
a.nav:link {somestyle}
a.nav:hover {some different style}

In the first example, the ID "nav" needs only be assigned to the DIV tag containing your navigation links, where in the second example, the class "nav" needs to be assigned to each and every navigation link.

Not only will this cut down on the number of selector names you need to come up with, but it will also facilitate cleaner semantic code.

Hope that makes sense - I'm still on my first cup of coffee.

rocknbil

3:48 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I picked up somewhere that you should name your elements descriptive of their FUNCTION instead of their appearance, as both of these posts imply. So instead of "darkReverse," "ListHeading" or "TableHeading" tells you exactly what you're looking for when you want to know what it's affecting (often months later.)

katana_one

4:13 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



I picked up somewhere that you should name your elements descriptive of their FUNCTION instead of their appearance ...

Good call. Had I more caffiene when I posted earlier, I might have remembered to include that. ;)

zackattack

4:34 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Good Question

I usually try and make it make sense for both. I also try always to keep it as simple as possible, so its easy to edit 6 months later too....

If it is not too off the thread - rocknbil's post above caught my eye - I have ALWAYS used lowercase for selectors - is there a 'best way' with this - I have seen people use numbers in the selectors too...

ZA

thatwebguy

4:59 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



The first thing you want to do is pick a name format style and stick to it. My preference is to use:

  • CamelCase for IDs
  • lowercase for class names

I usually extend that formating convention into JavaScript as well (UPPERCASE for constants, initCase for method names, prefixed_initCase for variables, etc).

As it was mentioned recently, you also want to make sure that you have meaningful class names.

My preference is to name things according to the role the element will play on the page. Instead of:

[pre]
<div class="halfwidthright">
<h1>Title</h1>
<img src="..." class="floatleft" />
<p>...</p>
<img src="..." class="floatright" />
<p>...</p>
<p>...</p>
</div>
[/pre]

I would be more inclined to write the following:

[pre]
<div class="calltoaction">
<h1>Title</h1>
<img src="..." class="primary" />
<p>...</p>
<img src="..." class="secondary" />
<p>...</p>
<p>...</p>
</div>
[/pre]

There are a number of reasons for doing it this way. The most imporant is content reuse. If you are ever going to be working with a content management system of any quality, then you may find yourself in a situation where something like this (lets say its a marketing blurb that sits among some "real" content) would be managed separately from any given page and included in a number of different places, or that a number of different instances of effectively the same type of markup might be used in very different places. So lets say that when on of these calls to action are used as part of a regular prose page (say the "About Us" page) you want to format this sort of thing floating to the right, 175px wide, with a green background, but if its in a product listing page, you want it to span the whole width have a red background and get a blue border. Or what ever.

If you take my approach, you will have one set of markup to deal with but apply a number of different CSS treatments depending on content.

That leads me to my second suggestion:

Use the Cascade and selector rules aggressively

Often you'll find people who do things like:

[pre]
<div class="homepagecalltoaction">
<h1 class="homepagecalltoactiontitle">Title</h1>
<img src="..." class="homepagecalltoactionfloatleft" />
<p>...</p>
<img src="..." class="homepagecalltoactionfloatright" />
<p>...</p>
<p>...</p>
</div>
[/pre]

this is REALLY stupid, but very common. a better approach is to do:

[pre]
<html>
...
<body class="homepage">
<div class="calltoaction">
<h1>Title</h1>
<img src="..." class="primary" />
<p>...</p>
<img src="..." class="secondary" />
<p>...</p>
<p>...</p>
</div>
</body>
</html>
[/pre]

This lets me do this:

[pre]

.homepage .calltoaction { ... }
.homepage .calltoaction h1 { ... }
.homepage .calltoaction .primary { ... }
.homepage .calltoaction .secondary { ... }

.contentpage .calltoaction { ... }
.contentpage .calltoaction h1 { ... }
.contentpage .calltoaction .primary { ... }
.contentpage .calltoaction .secondary { ... }

.contentpage .product .calltoaction { ... }
.contentpage .product .calltoaction h1 { ... }
.contentpage .product .calltoaction .primary { ... }
.contentpage .product .calltoaction .secondary { ... }
[/pre]

The last thing I would look at is multiple class names. Noone ever does this and its one of the most powerful tools in the CSS toolkit. You have to be careful since IE doesn't handle multiple class names correctly but there still a lot you can do.

Imagine the you have a field for a form marked up as follows. We want to indicate that the field is required (so we can put a * image in the right place) and that it was submitted with an error (so we can color the text red). The simple way is to do the following:

[pre]
<div class="field">
<label> ...</label>
<input />
</div>
<div class="requiredfield">
<label> ...</label>
<input />
</div>
<div class="field_error">
<label> ...</label>
<input />
</div>
<div class="requiredfield_error">
<label> ...</label>
<input />
</div>
[/pre]

The css would be:

[pre]
.field,
.requiredfield,
.field_error,
.requiredfield_error { /* Layout the field */ }

.requiredfield,
.requiredfield_error { /* position the background image */ }

.field_error,
.requiredfield_error { /* set the font color */ }

[/pre]

This leads to a lot of css that gets duplicated; most people wouldn't even put their CSS this way, they'd do each class separate, which can cause problems if you make a change and forget to update everyone.

A better solution is to do:

[pre]
<div class="field">
<label> ...</label>
<input />
</div>
<div class="field required">
<label> ...</label>
<input />
</div>
<div class="field error">
<label> ...</label>
<input />
</div>
<div class="field required error">
<label> ...</label>
<input />
</div>
[/pre]

The css would be:
[pre]
.field { /* Layout the field */ }

.required { /* position the background image */ }

.error { /* set the font color */ }

[/pre]

This scales better if you have more complex setup than I've shown.

createErrorMsg

8:44 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to be careful since IE doesn't handle multiple class names correctly

Some specifics might be in order here to curtail confusion.

Modern IE versions (as far back as IE5 on both platforms, at least) support multiple classes of the form:

html:
<p class="one two">...</p>

css:
.one{
color:red;
}
.two{
background:blue;
}

This is the most common and, IMO, most useful multiple class declaration.

The syntax that IE chokes on (IE5.x/Mac and IE6/Win) is when the css selector, itself, contains multiple classes. As in...

html:
<p class="one two">...</p>

css:
.one.two{
color:red;
background:blue;
}

or:
.two.one{
color:red;
background:blue;
}

IE applies these styles to any element containing the LAST of the two classes, even if they don't also contain the first.

Note that if you have a browser that you're not sure about, you can always use a multiple class test page, such as this one [meyerweb.com], to check it's support for yourself.

cEM

bosshog

8:12 am on Jun 9, 2005 (gmt 0)

10+ Year Member



Whoo - this is great people - some great tips here - I've already made some mistakes, so I'll e-do my CSS again.

Keep them coming.

PS - Maybe a bit a bit off topic, but when do you use the # notation i.e. #button a {some style}?

katana_one

1:36 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



"#" is used for ID styles, and not class styles (defined with a ".")
The difference is that elements with a #ID are supposed to be unique - only one per page. They are usually reserved for block-level elements, such as DIV tags. Class styles are intended to be used repeatedly on the same page, and can apply to anything.

jetboy

1:57 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



Worth pointing out the Netscape 4 doesn't support the <p class="one two"> syntax in HTML. I believe only the last class gets applied, but I'm not 100% on that.

Yeah, I know it's Netscape 4 and no one cares ... ;)