Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS .class selector

CSS .class Selector

Complete CSS SelectorComplete CSS Selector Reference Manual

Example

Select and set all elements with class="intro":

.intro

{

background-color:

yellow

;

}

Try it »


Definition and Usage

.class The selector is used to select HTML elements with a specified class attribute.

By using it in a CSS rule, .classyou can apply styles to multiple elements that share the same class.

**Note:** A class name begins with a . and can be used on multiple elements in an HTML document.

HTML code:

<p class="highlight">

This is a paragraph with class "highlight".

p

<

p

This is a normal paragraph.

p

<p class="highlight">

This is another paragraph with class "highlight".

p

CSS code

.highlight

{

 

color

:

red

;

 

font-weight

:

bold

;

}

Try it »

In the code above, the .highlight class is applied to two

elements. By using the .highlight selector, you can select all elements with that class and apply styles to them.

The CSS code in the example above sets the text color to red and makes it bold, with the effect shown below:


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support .classSelector


More examples

Example

Selects all

elements, and set the background color to yellow:

p

.hometown

{

background-color:

yellow

;

}

Try it »


Complete CSS SelectorComplete CSS Selector Reference Manual