class (HTML attribute)
| Depr. | Version |
|---|---|
| No | HTML 4 |
| IE5.5+ | FF1+ | SA1.3+ | OP9.2+ | CH2+ |
|---|---|---|---|---|
| Full | Full | Full | Full | Full |
Syntax
Description
While the name of an
element specifies its type, the class attribute
lets you assign to it one or more subtypes. These subtypes may then be
used in CSS code for styling purposes, or by JavaScript code (via the
Document Object Model, or DOM) to make changes, or add behavior, to
elements belonging to a particular subtype.
Note that the
class attribute cannot be applied to the following
elements:
basebasefontheadhtmlmetaparamscriptstyletitle
Example
The div element in this example has two classes assigned to
it, which identify it both as an "aside" and as
"salescopy":
<div class="aside salescopy">This is a sidebar.</div>
Value
This attribute takes as
its value a space-delimited list of one or more
class names. Unlike the id
attribute, the class attribute’s value may begin
with a number and will still be valid HTML; for example, <div
class="3-col-wide"> … </div>. However, it’s best avoided
as the CSS 2.1 specification says that CSS identifiers, which would
include the class name that matches the class
attribute in the HTML, should not begin with a digit. By not starting
class names with a digit, you’ll be keeping both HTML and CSS validators
happy.
While there have been numerous calls from the developer
community to specify a standard set of class names and associated
meanings, at the moment you’re free to specify whatever
class names make sense to you. As with all HTML
markup, however, it’s recommended that you use names that are as
semantically meaningful as possible; the class name should indicate to a
human who reads it what that element’s purpose or meaning is, rather than
how it looks. Hence, purely presentational class
names like "large" or "shiny" are
bad practice, while class names such as
"executive-summary" or
"key-findings" provide some useful information to those
reading the markup.
While it’s good to make the markup
human-readable, the developer community has made great strides in
standardizing the usage of class names for certain
types of information on the Web, such as contact details or calendar
events, so that the markup can also be read, or parsed, by web-based
services to great effect. The result of this standardization effort is
known as Microformats.
Compatibility
| Internet Explorer | Firefox | Safari | Opera | Chrome | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 8.0 | 1.0 | 1.5 | 2.0 | 3.0 | 3.5 | 1.3 | 2.0 | 3.1 | 4.0 | 9.2 | 9.5 | 10.0 | 2.0 |
| Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full |
As with the id attribute,
compatibility issues with class generally depend on
the way it’s used or accessed by CSS and JavaScript; the presence or lack
of a class name on any attribute won’t adversely affect its display in
HTML when style sheets or JavaScript aren’t linked.
It
is perfectly valid—indeed, it’s extremely useful—to apply multiple values
in the class attribute; for example, applying both
class="friend colleague" or class="spouse
muse", and have CSS combine different styles depending on the
classes in use.
User-contributed notes
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.