all
alt
ch
dir
id
min
rel
top
url
URL
urn
Expand Minimize

class attribute | className property

Sets or retrieves the class of the object.

Syntax

HTML<element class="p" ... >
JavaScript

p = object.className

Property values

Type: String

the class or style rule.

Remarks

The class is typically used to associate a particular style rule in a style sheet with the element.

You can apply multiple styles to an element by specifying more than one style for the class attribute. To apply multiple styles to a single element, use the following syntax:


<element class = "sClass [ sClass2 [ sClass3 ... ] ]" ... >

When multiple styles are specified for an element, a conflict could develop if two or more styles define the same attribute differently. In this case, you can resolve the conflict by applying styles to the element in the following order, according to the Cascading Style Sheets (CSS) selector used to define the style:

  1. Element
  2. class
  3. id
  4. Inline styles

When two or more selectors pertain to an element, a style defined later takes precedence over a style defined earlier. For more information, see Introduction to Cascading Style Sheets.

Examples

This example uses the className attribute to apply one or more styles to an HTML element.


<!DOCTYPE html>

<html>
<head>
  <title>CSS</title>
  <style type="text/css">
    p {
      font-size: 24pt;
    }

    .redText {
      color: red;
    }

    .blueText {
      color: blue;
    }

    .italicText {
      font-style: italic;
    }
  </style>
</head>

<body>
  <p>
    Large text, no class specified, one implied.
  </p>
  <p class="redText">
    Large text, .redText class specified.
  </p>
  <p class="blueText italicText">
    Large text, .blueText and .italicText classes specified.
  </p>
</body>
</html>

See also

a
abbr
address
area
article
aside
b
bdo
blockQuote
body
br
button
caption
cite
code
col
colGroup
custom
dd
del
dfn
div
dl
dt
em
embed
fieldSet
figcaption
figure
footer
form
frame
head
header
hgroup
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=email
input type=file
input type=hidden
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
ins
kbd
label
legend
li
map
mark
menu
nav
object
ol
optGroup
option
p
pre
q
rt
ruby
s
samp
section
select
small
span
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
u
ul
var

 

 

Show:
© 2014 Microsoft