
What are Selectors?
CSS selectors identify specific HTML elements as targets for CSS styles. This topic covers how CSS selectors target
HTML elements. Selectors use a wide range of over 50 selection methods offered by the CSS language, including
elements, classes, IDs, pseudo-elements and pseudo-classes, and patterns.
Basic selectors
Code: Select all
*
Code: Select all
div
Code: Select all
.blue
Code: Select all
.blue.red
Code: Select all
#headline
Code: Select all
:pseudo-class
Code: Select all
::pseudo-element
Code: Select all
:lang(en)
Code: Select all
div > p
Note: The value of an ID must be unique in a web page. It is a violation of the HTML standard to use the value of an ID more than once in the same document tree.
Attribute Selectors
Attribute selectors can be used with various types of operators that change the selection criteria accordingly. They select an element using the presence of a given attribute or attribute value.
[attribute]
Selects elements with the given attribute.
Code: Select all
div[data-color] {
color: red;
}
<div data-color="red">This will be red</div>
<div data-color="green">This will be red</div>
<div data-background="red">This will NOT be red</div>
Selects elements with the given attribute and value.
Code: Select all
div[data-color="red"] {
color: red;
}
<div data-color="red">This will be red</div>
<div data-color="green">This will NOT be red</div>
<div data-color="blue">This will NOT be red</div>
Selects elements with the given attribute and value where the given attribute contains the given value anywhere (as
a substring).
Code: Select all
[class*="foo"] {
color: red;
}
<div class="foo-123">This will be red</div>
<div class="foo123">This will be red</div>
<div class="bar123foo">This will be red</div>
<div class="barfooo123">This will be red</div>
<div class="barfo0">This will NOT be red</div>
Selects elements with the given attribute and value where the given value appears in a whitespace-separated list.
Code: Select all
[class~="color-red"] {
color: red;
}
<div class="color-red foo-bar the-div">This will be red</div>
<div class="color-blue foo-bar the-div">This will NOT be red</div>
Selects elements with the given attribute and value where the given attribute begins with the value.
Code: Select all
[class^="foo-"] {
color: red;
}
<div class="foo-123">This will be red</div>
<div class="foo-234">This will be red</div>
<div class="bar-123">This will NOT be red</div>
Selects elements with the given attribute and value where the given attribute ends with the given value.
Code: Select all
[class$="file"] {
color: red;
}
<div class="foobar-file">This will be red</div>
<div class="foobar-file">This will be red</div>
<div class="foobar-input">This will NOT be red</div>
Selects elements with a given attribute and value where the attribute's value is exactly the given value or is exactly
the given value followed by - (U+002D)
Code: Select all
[lang|="EN"] {
color: red;
}
<div lang="EN-us">This will be red</div>
<div lang="EN-gb">This will be red</div>
<div lang="PT-pt">This will NOT be red</div>
Selects elements with a given attribute and value where the attribute's value can be represented as Value, VALUE,
vAlUe or any other case-insensitive possibility.
Code: Select all
[lang="EN" i] {
color: red;
}
<div lang="EN">This will be red</div>
<div lang="en">This will be red</div>
<div lang="PT">This will NOT be red</div>
0-1-0
Same as class selector and pseudoclass.
Code: Select all
*[type=checkbox] // 0-1-0
Combinators
Descendant Combinator: selector selector
A descendant combinator, represented by at least one space character (), selects elements that are a descendant of
the defined element. This combinator selects all descendants of the element (from child elements on down).
Code: Select all
div p {
color:red;
}
<div>
<p>My text is red</p>
<section>
<p>My text is red</p>
</section>
</div>
<p>My text is not red</p>
Child Combinator: selector > selector
The child (>) combinator is used to select elements that are children, or direct descendants, of the specified element.
Code: Select all
div > p {
color:red;
}
<div>
<p>My text is red</p>
<section>
<p>My text is not red</p>
</section>
</div>
The second <p> element is not selected because it is not a direct child of the <div>.
Adjacent Sibling Combinator: selector + selector
The adjacent sibling (+) combinator selects a sibling element that immediate follows a specified element.
Code: Select all
p + p {
color:red;
}
<p>My text is not red</p>
<p>My text is red</p>
<p>My text is red</p>
<hr>
<p>My text is not red</p>
General Sibling Combinator: selector ~ selector
The general sibling (~) combinator selects all siblings that follow the specified element.
Code: Select all
p ~ p {
color:red;
}
<p>My text is not red</p>
<p>My text is red</p>
<hr>
<h1>And now a title</h1>
<p>My text is red</p>
Pseudo-classes
Pseudo-classes are keywords which allow selection based on information that lies outside of the document tree or that cannot be expressed by other selectors or combinators. This information can be associated to a certain state (state and dynamic pseudo-classes), to locations (structural and target pseudo-classes), to negations of the former (negation pseudo-class) or to languages (lang pseudo-class). Examples include whether or not a link has been followed (:visited), the mouse is over an element (:hover), a checkbox is checked (:checked), etc.
Syntax
Code: Select all
selector:pseudo-class {
property: VALUE;
}
Code: Select all
:active
Code: Select all
:any
Allows you to build sets of related selectors by creating groups that the included items will match. This is an alternative to repeating an entire selector.
Code: Select all
:target
Code: Select all
:checked
Code: Select all
:default
Code: Select all
:disabled
Code: Select all
:empty
Code: Select all
:enabled
Code: Select all
:first
Code: Select all
:first-child
Code: Select all
:first-of-type
Code: Select all
:focus
Code: Select all
:focus-within
Code: Select all
:full-screen
Code: Select all
:hover
Code: Select all
:indeterminate
Code: Select all
:in-range
Code: Select all
:invalid
Code: Select all
:lang
Code: Select all
:last-child
Code: Select all
:last-of-type
Code: Select all
:left
Code: Select all
:link
Code: Select all
:not()
together.
Code: Select all
:nth-child
Code: Select all
:nth-of-type
Code: Select all
:only-child
:first-child:last-child or :nth-child(1):nth-last-child(1), but with a lower specificity.
Code: Select all
:optional
forms to easily indicate optional fields and to style them accordingly.
Code: Select all
:out-of-range
element is outside the range limits. A value can be outside of a range if it is either smaller or larger than maximum and minimum set values.
Code: Select all
:placeholder-shown
Code: Select all
:read-only
Code: Select all
:read-write
Applies to any element that is editable by a user, such as <input> elements.
Code: Select all
:right
Code: Select all
:root
Code: Select all
:scope
Code: Select all
:target
Code: Select all
:visited
Child Pseudo Class
The :nth-child(an+b) CSS pseudo-class matches an element that has an+b-1 siblings before it in the document tree, for a given positive or zero value for n
Class Name Selectors
The class name selector select all elements with the targeted class name. For example, the class name .warning would select the following <div> element:
Code: Select all
<div class="warning">
<p>This would be some warning copy.</p>
</div>
CSS
Code: Select all
.important {
color: orange;
}
.warning {
color: blue;
}
.warning.important {
color: red;
}
Code: Select all
<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>
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 was included between the two classes in the CSS declaration, it would only select elements that have parent elements with a .warning class names and child elements with .important class names.
Select element using its ID without the high specificity of the ID selector
This trick helps you select an element using the ID as a value for an attribute selector to avoid the high specificity of
the ID selector.
HTML:
Code: Select all
<div id="element">...</div>
Code: Select all
#element { ... } /* High specificity will override many selectors */
[id="element"] { ... } /* Low specificity, can be overridden easily */
The :last-of-type selects the element that is the last child, of a particular type, of its parent. In the example below,
the css selects the last paragraph and the last heading h1.
Code: Select all
p:last-of-type {
background: #C5CAE9;
}
h1:last-of-type {
background: #CDDC39;
}
Code: Select all
<div class="container">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Last paragraph</p>
<h1>Heading 1</h1>
<h2>First heading 2</h2>
<h2>Last heading 2</h2>
</div>
Code: Select all
<style>
input:in-range {
border: 1px solid blue;
}
</style>
Code: Select all
<input type="number" min="10" max="20" value="15">
<p>The border for this value will be blue</p>
ID selectors
ID selectors select DOM elements with the targeted ID. To select an element by a specific ID in CSS, the # prefix is used.
For example, the following HTML div element…
Code: Select all
<div id="exampleID">
<p>Example</p>
</div>
Code: Select all
#exampleID {
width: 20px;
}