Class Name Selectors
suggest changeThe class selector selects all elements with the targeted class name. For example, the class name .warning would select the following <div> element:
<div class="warning">
<p>This would be some warning copy.</p>
</div>
You can also combine class names to target elements more specifically. Let’s build on the example above to showcase a more complicated class selection.
CSS
.important {
color: orange;
}
.warning {
color: blue;
}
.warning.important {
color: red;
}
HTML
<div class="warning">
<p>This would be some warning copy.</p>
</div>
<div class="important warning">
<p class="important">This is some really important warning copy.</p>
</div>
In this example, elements with the .warning class have blue text, elements with the .important class have orange text, and elements that have both class names have red text. The inner paragraph only has important, so its text stays orange even though its parent is red.
Notice that within the CSS, the .warning.important declaration did not have any spaces between the two class names. This means it will only find elements which contain both class names warning and important in their class attribute. Those class names could be in any order on the element.
If a space were included (.warning .important), the selector would match .important elements that are descendants of a .warning element, not elements that have both classes.