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 ... ] ]" ... >

By default, the property is equal to the string assigned to the className property of the given object, or null if the attribute is not explicitly assigned.

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.

Multiple styles are supported as of Microsoft Internet Explorer 5.

In Windows Internet Explorer 8, class names, such as hslice and entry-title, are used to identify portions of a webpage that can be monitored for updates (Web Slices). For more information, see Subscribing to Content with Web Slices.

Examples

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

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/className.htm


<!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
acronym
address
applet
area
article
aside
b
bdo
big
blockQuote
body
br
button
caption
center
cite
code
col
colGroup
custom
dd
del
dfn
dir
div
dl
dt
em
embed
fieldSet
figcaption
figure
font
footer
form
frame
frameSet
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
isIndex
kbd
label
legend
li
listing
map
mark
marquee
menu
nav
nextID
noBR
object
ol
optGroup
option
p
plainText
pre
q
rt
ruby
s
samp
section
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp

 

 

Show:
© 2014 Microsoft