How to Define and Apply a Style Class
By Stephen Bucaro
Defining a style class is similar to defining styles rules for any html element except that a style class
has a name that you use to apply it to only specific incidences of elements, for example to only some
paragraph elements, or to apply it to dissimilar elements, for example to paragraphs and divs.
The name for a style class cannot begin with a number or a hyphen followed by a number. The name
cannot contain spaces, and the only punctuation characters allowed are the hyphen (-) and the
underscore (_).
When defining a class in a style block or in a style sheet, put a dot (.) in front of the name. An
example of defining a style class named appbtn is shown below.
<style type="text/css">
.appbtn
{
width:130px;
height:60px;
padding:4px;
border-style:solid;
border-color:#009000;
background-color:#66ff66;
font-family:verdana;
font-size:14px;
box-shadow: 6px 6px 4px #a0a0a0;
}
</style>
When applying the class to an html element, add the class attribute in the element's opening
tag, and set the class attribute's value to the name of the class - without the dot. An example
of applying the class appbtn to an html element is shown below.
<div class="appbtn">This box was styled by a class named appbtn</div>
The result of the code in this example is shown below.
This box was styled by a class named appbtn
By the way, you may see style code that looks like a class definition except instead of a dot,
it puts a pound sign (#) in front of the name. An example of this is shown below.
<style type="text/css">
#appbtn
{
width:130px;
height:60px;
padding:4px;
border-style:solid;
border-color:#009000;
background-color:#66ff66;
font-family:verdana;
font-size:14px;
box-shadow: 6px 6px 4px #a0a0a0;
}
</style>
This is not a CSS class selector, but instead it's an id selector. Think of a class
like a class room of students of all ages, sexes, races, and creeds. But they are all in the same
class. However the id selector (#) selects only one individual (or one specific html
element), the one with that id. An example of applying the id selector appbtn
to an html element is shown below.
<div id="appbtn">This box was styled by the id selector #appbtn</div>
More CSS Quick Reference: • Set an Element's Clipping • Context selectors • Use word-wrap Property to allow Line Breaks in the Middle of Words • Style the First Line • Highlight Text • Set the Border Properties • Set List Properties • Specifying Color • Set a Fixed Background Image • Set a Background Image's Position
|