The CSS class selector matches elements based on the contents of their class
attribute.
.spacious {
margin: 2em;
}
li.spacious {
margin: 2em;
}
li.spacious.elegant {
margin: 2em;
}
Syntax
.class_name { style properties }
Note that this is equivalent to the following attribute selector
:
[class~=class_name] { style properties }
CSS
.red {
color: #f33;
}
.yellow-bg {
background: #ffa;
}
.fancy {
font-weight: bold;
text-shadow: 4px 4px 3px #77f;
}
HTML
<p class="red">This paragraph has red text.</p>
<p class="red yellow-bg">
This paragraph has red text and a yellow background.
</p>
<p class="red fancy">This paragraph has red text and "fancy" styling.</p>
<p>This is just a regular paragraph.</p>
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
Class_selectors |
1 |
12 |
1 |
3 |
3.5 |
1 |
≤37 |
18 |
4 |
10.1 |
1 |
1.0 |