Classes

CSS classes. Many people get confused with these, this tutorial will help you understand them. Assigning classes to specific tags using CSS.

In category css.

Posted by Joseph Skidmore on 10.09.05.

Last edited on 10.09.05.

The best way to describe these to you is to just to show you examples, so with the best example possible I will begin, in your CSS file use this code:

div.center {
	text-align: center;
}

div.indent {
	margin: 20px;
	text-decoration: underline;
}

You will see that we have given attributes to 2 of the same tag, in this example I have given different attributes to the div tag. To call up a class you use the class="" attribute. In your HTML page when you put your text between the <div class="center"> TEXT </div> you will see that the text is aligned in the center. When you use the code <div class="indent"> TEXT </div> the text is padded 20 pixels in from the left and the text is underlined. E.G.

This text is centred
This text is 20px from the left and underlined

The codes

<div class="center">This text is centred</div>
<div class="indent">This text is 20px from the left and underlined</div>

You see, when the browser reads the page it sees that the DIV tag is there, it then reads that the div tag wants the class 'center' or 'indent', it then looks through your CSS for the corresponding styles and applies them to it.

You can apply the class to any tag, even the body tag.

Another example

Applying multiple classes to the <p> tag. First of all you must give each p tag a name, the name of the class is the name given after the dot. E.G.

p.centerunderline {
  text-align: center;
  text-decoration: underline;
}

All P tags with the class name 'centerunderline' will be aligned in the center of the page and will also be underlined. You see in the CSS code you have p.centerunderline and the class name is the word given after the p. You then call the class by using the code class="". Simple isn't it?

Of course, if you wanted to customize every paragraph on your page you would just use the normal p { style style }. And then for every class it would be <p class="classhere">.

Page generated in 0.0012 seconds.

Whatcounter Free Invisible Stats CounterFree invisible hit counter