Latest web development tutorials

HTML class attribute

HTML Global Attributes HTML Global Attributes

Examples

Use the class attribute in an HTML document:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.com) </ title>
<Style>
h1.intro {color: blue;}
p.important {color: green;}
</ Style>
</ Head>
<Body>

<H1 class = "intro"> heading 1 </ h1>
<P> paragraphs. </ P>
<P class = "important"> Note: This is a very important paragraph. :) </ P>

</ Body>
</ Html>

try it"

There are more examples later in this chapter.

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support class attribute


Definition and Usage

class attribute defines the class name of the element.

class property is typically used to point to a stylesheet class. However, it can also be used in JavaScript (via the HTML DOM), to modify the class name of HTML elements.


Differences between HTML 4.01 and HTML5

In HTML5, class attributes can be used for any HTML element (it will verify that any HTML element, but not necessarily useful).

In HTML 4.01, class attributes can not be used: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.


grammar

<Elementclass = "classname">

Property Value

value description
classname It specifies the name of the class element. For an element to define a plurality of classes, class names separated by spaces. <Span class = "left important">. HTML element allows the use of multiple classes.

Name rules:

  • Must begin with a letter AZ or az
  • The following characters can be: (A-Za-z), numbers (0-9), the bar ( "-"), and the underscore ( "_")
  • In HTML, the class name is case sensitive


Examples

More examples

Add multiple classes of elements
HTML elements how to add multiple classes.


HTML Global Attributes HTML Global Attributes