Introduction
In this lesson, I will talk about CSS selectors. as a web designer you need to know this.
Universal Selector
All elements in the document
Type Selector
all elements
ID Selector
element with ID
Class Selector
all elements with class
Attribute Selector
based on the presence or value of an attribute
Pseudo-class Selector
based on their state
Pseudo-element Selector
Selects and styles a part of an element.
Child Selector
Direct children of an element
Descendant Selector
All descendants of an element
Adjacent Sibling Selector
Adjacent sibling element
General Sibling Selector
All sibling elements
Group Selector
Multiple elements
Structural Pseudo-classes
This is used to define a state of element
:first-child
First child of a parent
:last-child
Last child of a parent
:nth-child(n)
nth child of a parent
:nth-last-child(n)
nth child of a parent, counting from the last child
:nth-of-type(n)
nth child of a parent, of a specific type
:nth-last-of-type(n)
nth child of a parent, of a specific type, counting from the last child
:first-of-type
first child of a specific type of a parent
:last-of-type
last child of a specific type of a parent
:only-child
element if it is the only child of its parent
:only-of-type
element if it is the only child of its type of its parent
Dynamic Pseudo-classes
:hover
elements when you mouse over them.
:focus
Selects elements when they are focused.
:active
Selects elements when they are activated (e.g., clicked).
:visited
Selects visited links.
:link
Selects unvisited links.
:checked
Selects checked inputs (radio buttons, checkboxes).
:enabled
Selects enabled form elements.
:disabled
Selects disabled form elements.
:required
Selects required form elements.
:optional
Selects optional form elements.
:read-only
Selects elements that are read-only.
:read-write
Selects elements that are read-write.
:valid
Selects elements with valid input.
:invalid
Selects elements with invalid input.
:in-range
Selects elements with a value within a specified range.
:out-of-range
Selects elements with a value outside a specified range.
UI Element States
:checked
Selects elements that are checked (e.g., checkbox, radio button).
:indeterminate
Selects elements that are in an indeterminate state (e.g., checkbox).
:default
Selects the default button in a group of buttons.
:valid
Selects elements with valid values.
:invalid
Selects elements with invalid values.
:placeholder-shown
Selects input elements displaying placeholder text.
:disabled
Selects disabled form elements.
:enabled
Selects enabled form elements.
:optional
Selects optional form elements.
:required
Selects required form elements.
:in-range
Selects elements with a value within a specified range.
:out-of-range
Selects elements with a value outside a specified range.
Pseudo-element selectors
::after
Inserts content after the element's content.
::before
Inserts content before the element's content.
::first-letter
Selects the first letter of the element's content.
::first-line
Selects the first line of the element's content.
::selection
Selects the portion of an element that is selected by the user.
::placeholder
Selects the placeholder text of an input or textarea.
::marker
Selects the marker box of a list item (usually a bullet or number).
::backdrop
Selects the backdrop of an element, typically used with modal elements.
::cue
Selects the cue text of a media element with WebVTT captions.
::part(name)
Selects a part of a shadow DOM element that is marked with a part attribute.
::slotted(selector)
Selects elements that are distributed into a slot of a shadow DOM element.
Selects the button of a file input element.
Other
:root
Selects the root element of the document.
:empty
Selects elements that have no children.
:target
Selects the target element of a URL fragment.
:not(selector)
Selects elements that do not match the given selector.
:nth-last-child(n)
Selects elements based on their position among their siblings, counting from the end.
:lang(language)
Selects elements based on the language attribute.
:dir(direction)
Selects elements based on the directionality of their text.