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

CSS Grouping and Nesting

CSS Grouping and Nesting Selectors


Group selector

There are many elements with the same style in a stylesheet.

h1

{

color:

green

;

}

h2

{

color:

green

;

}

p

{

color:

green

;

}

To minimize code, you can use group selectors.

Each selector is separated by a comma.

In the example below, we use a group selector on the code above:

Example

h1

,

h2

,

p

{

color:

green

;

}

Try it »


Nested selector

It can apply styles to selectors nested inside a selector.

The following example sets four styles:

Example

p

{

color:

blue

;

text-align:

center

;

}

.marked

{

background-color:

red

;

}

.marked

p

{

color:

white

;

}

p

.marked

{

text-decoration:

underline

;

}

Try it »