What is a CSS Class?
A CSS class provides us with a tool to create custom
styles for elements on our web pages. Using classes
can save us from typing the same formatting code for
numerous elements. Classes enable us to make wide-
scale styling changes very quickly.
In our earlier examples of using CSS, we applied the
same formatting to all instances of a particular element.
A CSS class allows us to define different styles for the
same element type. For example, we can make one
paragraph appear in green text and another paragraph
show in red text.
Syntax and Use of CSS Classes:
Class definitions are placed along
with all the other CSS inside the
<head> section of the document.
<head>
<style type="text/css">
.center {
text-align:center;
}
</style>
</head>
<p>Paragraph 1</p>
<p class="center">Paragraph 2</p>
A CSS class is defined with a
period, followed by the name of the
class.
Paragraph 1
Paragraph 2
By adding the class="center" to this
paragraph element, we are telling
the browser to apply all formatting
defined for that class to this
particular paragraph.
Syntax and Use of CSS Classes:
As before, multiple styles can be
defined in a single statement.
Remember the semicolon at the
end of each line.
<head>
<style type="text/css">
.special {
text-align:center;
color:green;
font-style:italic;
}
</style>
</head>
<p class="special">Paragraph</p>
Paragraph
Syntax and Use of CSS Classes:
CSS classes can even be
combined. The element will reflect
the formatting definitions from all
the classes being applied.
<head>
<style type="text/css">
.center {
text-align:center;
}
.green {
color:green;
}
.italic {
font-style:italic;
}
</style>
</head>
<p class="center green
italic">Paragraph</p>
Paragraph
Syntax and Use of CSS Classes:
Generic classes like these can be
applied to any element we choose.
<head>
<style type="text/css">
.green {
color:green;
}
</style>
</head>
<h1 class="green">Heading</h1>
<p class="green">Paragraph</p>
<ul>
<li>List Item 1</li>
<li class="green">List Item 2</li>
</ul>
Heading
Paragraph
• List Item 1
• List Item 2
Syntax and Use of CSS Classes:
You can also specify that only
particular elements be affected by
a class.
<head>
<style type="text/css">
p.green {
color:green;
}
</style>
</head>
<h1 class="green">Heading</h1>
<p class="green">Paragraph</p>
<ul>
<li>List Item 1</li>
<li class="green">List Item 2</li>
</ul>
Heading
Paragraph
• List Item 1
• List Item 2
In this example, we have defined
the .green class to apply only to the
paragraph element.
If we try to apply the .green class to
other elements, it will have no
effect; the browser will simply
ignore the class definition.
The <span> element:
The <span> tag allows us to apply
class styling to a specific section
within an element.
<head>
<style type="text/css">
.green {
color:green;
}
</style>
</head>
<p>This is a <span class="green">
typical</span> paragraph</p>
This is a typical paragraph.
Note that the <span> tag does
nothing on its own. Only when we
associate it with a class does it
make a change to how the page
displays.
Naming CSS Classes:
Give some thought to how you name your CSS classes.
By using class names that describe how the element will
look (.green, .center), we can make things confusing in
the future. What if we later change our green text to be
red? We would have the .green class display in red!
A much better approach is to use class names that
describe the meaning of the content, not how it will look:
Problematic names
.green
.underline
.center
.bigletters
Better names
.slogan
.booktitle
.caption
.headline