Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 62
Comments: 18 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Ids and Classes
This tutorial will teach you how to create Ids and Classes in CSS, which can be used to implement a group of settings to an element.

Ids and Classes allow you to define multiple style settings to as many elements as you need, Without having to set those settings for every element on the page. For example, Let's say I wanted to make only certain <p> tags have larger font than all of the other <p> tags used in my HTML document.

Classes

This can easily be accomplished with a simple class, like the one below.

<style type="text/css">
p.bigger {
    font-size: 2em;
}
</style>

To define a class you simply put the element you want to be able to use that class, followed by a period, followed by the name you want the class to have. It isn't absolutely necessary to define the element you want to be able to use the class. You can leave that part out to allow multiple elements to use the same class.

To apply the above class to your <p> tag(s), all you have to do is add the attribute class to the tag. Example:

<p class="big">This text is bigger</p>

Pretty easy eh?

Let's look at some more things you can do with this...

For all, or most, of the code that is displayed in tutorials on this website I use <pre> tags. Not just ordinary <pre> tags, though. I give them a class - code. In this class I set the background color, borders and other properties that I want to have to make the code look better and to make it more readable.

Let's look at my code class.

pre.code {
    background-color: #E5E5E5;
    border: 1px solid #000000;
    width: 350px;
    padding: 5px;
    font-family: verdana, arial, serif;
    margin: 0 auto;
}

I start out by setting the background color to a light gray (#E5E5E5). Next, I set all of the borders(top, right, bottom and left) to be 1 pixel width, solid, black. Then I set the width to an absolute 350 pixels. After that, I set the padding to 5 pixels. Padding is the area of space on the inside of the <pre> tag that separates the text from the borders. Next, I use the font-family property and set it to verdana, to match with the rest of this site. I also set two other back-up fonts to use, incase the visitor doesn't have verdana installed on his/her computer. Finally, I set the margins of the <pre> tag to 0(top and bottom) and auto(left and right). This is what centers it in the middle of the page.

Ids

Ids are much like classes, exept to define Ids, you need to use a number sign(#) instead of a period. Example:

<style type="text/css">
 #box {
    background-color: #E5E5E5;
    border: 1px dotted #000000;
    font-size: 12px;
    padding: 3px;
    width: 500px;
 }
</style>

To use the Id above you need the id tag. Example:

<div id="box">Some text</div>

I hope you got some use out of this tutorial.

Discuss Tutorial: Ids and Classes 11 Comments
Comment by Ron on Mar 16, 2004, 4:31 am
Please post questions or comments about this tutorial below. Smile
Comment by haftrige on May 21, 2004, 5:15 am
thanks for the info but i still don't see the difference between id and class !when i should use one and not the other?
Comment by Slimeball on Oct 20, 2004, 1:23 am
Class can be used more than once, ID is only supposed to be used once. (according to standards)
Comment by Streuer on Nov 6, 2004, 8:04 pm
I was wondering if any of you guys know anything about Javascript style sheets. If so, which do you think is better, the JSSS or the CSS?
Comment by dmsuperman on Jan 8, 2005, 2:21 am
There is no JSSS, only CSS. JSSS makes no sense, JS is for scripting, not styling.
Comment by Get Bent on May 17, 2005, 9:54 pm
Even if jsss existed, css would be recomended...
Comment by Mysterious Man #6821 on Jul 13, 2005, 5:51 am
Wassat Angry Java Script does not suck. IF your using firefox go to tools and turn off your javascript. See how your web browsing is without it.
Comment by Ash on Aug 13, 2005, 4:48 pm
I've just read a tutorial on making menus in CSS in a magazine I have, and in it they use IDs like this:
#menu ul { /* CODE HERE */}
Why is the 'ul' after the '#menu'? Does it make any difference?
Comment by Blake Lysol on Jun 17, 2006, 4:18 pm
Ummm... Nobody said js sucked. JS is awesome, just not fit for this kind of stuff.

The tutorial is great. I knew how to write a stylesheet, but now I know how to get all the variables or whatever you wish to call them. Thanks, your site is amazing.
Comment by Krunkosaurus on Jun 18, 2006, 8:48 pm
Ash: That is an indispensable feature called contextual css. What that code basically states that all UL tags within the #Menu div get /*CODE HERE*/. Great for specifying things like "All links in the navigations div should be x color:

div#navigation a {color: white; }

« Previous [ 1 2 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.