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:
- p{ }: for all p elements, specifies a style.
- .marked{ }: for all class="marked" elements, specifies a style.
- .marked p{ }: for all class="marked" elements inside p elements, specifies a style.
- p.marked{ }: for all class="marked" 's p elements, specifies a style.
Example
p
{
color:
blue
;
text-align:
center
;
}
.marked
{
background-color:
red
;
}
.marked
p
{
color:
white
;
}
p
.marked
{
text-decoration:
underline
;
}
Try it »