A reference of CSS selectors. This includes new selectors from CSS Selectors Level 3 (CSS3):
Selects everything.
Selects the root element. All elements are descendants of the root element, and it almost always references the <html> element.
Selects all elements of the type.
Selects the element with the provided ID.
Selects all elements with the provided class.
Selects elements that have the provided attribute.
Selects elements that have the provided attribute and value.
Selects elements if the value is part of a white-space separated list of values.
Selects elements who’s value for an attribute starts with the provided string.
Selects elements who’s value for an attribute ends with the provided string.
Selects elements who’s value for an attribute contains the substring provided.
Selects elements if the value is the left part in a hyphen-separated list.
Selects the element(s) provided on the right if it’s a descendant of the element on the left.
Selects the element(s) provided on the right if it’s a direct child of the element on the left.
Selects the element provided on the right if it’s immediately preceded by the element on the left.
Selects the element provided on the right if it’s preceded by the element on the left.
Selects elements that don’t match the simple selector provided in parenthesis.
Generated content before the element.
Generated content after the element.
The first letter of the element.
The first line of the element.
Selects the n-th sibling if it’s the provided type.
Counting from the last child, selects the n-th sibling if it’s the provided type.
Selects the n-th sibling, only counting the same type.
Counting from the last child and only counting the provided type, selects the n-th sibling.
Selects the first of child of the parent if it matches the provided type of element.
Selects the last of child of the parent if it matches the provided type of element.
Selects the first of the provided type within a parent.
Selects the last of the provided type within a parent.
Selects the element if it’s the only child of the parent element.
Selects the element if it’s the only one of its type inside the parent element.
Selects elements with no children.
A link that hasn’t been visited. The starting point for links
A link that has been visited.
When the element is active.
When the user’s pointing device is on top of the element.
When the element has the focus. For example, when the user clicks inside an input field, the field has the focus.
Selected when the element on the left is the current target as defined by the url.
Element(s) with the specified lang attribute.
When the element(s) on the left is enabled.
When the element(s) on the left is disabled.
When the element(s) on the left is checked. Associated with the inputs of type radio or checkbox.
Remember that to target multiple selectors in one CSS declaration you use a comma between each selector. The following example selects all <p> elements that don’t have the article-par class and also selects all <h2> elements:
p:not(.article-par), h2 {
font-weight: bold;
}
Also, if you want to select an element while targeting a combination of multiple classes and/or ID, you’ll simple note the multiple classes and/or ID without using a space between them. For example, if you want to select and element that has the btn and btn-large classes:
.btn.btn-large {
font-weight: bold;
}
In the above example, you don’t want your selector to be .btn .btn-large, because then it would select .btn-large elements that are descendants of .btn elements.
Now say that the element you want to select also has an ID of #main-btn:
#main-btn.btn.btn-large {
font-weight: bold;
}
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
best summary/guide to CSS selectors i’ve come across thanks