Selectors are used to target HTML elements and apply the same CSS properties to them.
There are several types of selectors:
id
attribute. Syntax: #idname { color: black; }class
attribute. Syntax: .classname { color: blue; }If you want to apply a property to all elements, you can use the universal selector: *
Pseudo-classes are keywords added to selectors that define a special state of an element.
Syntax: selector:pseudoclass { property: value; }
:nth-of-type(3)
selects the third matching element.:nth-of-type(3n)
selects every third element (3, 6, 9...)Read the MDN documentation for more pseudo-classes.
Pseudo-elements allow you to style specific parts of elements.
Syntax: selector::pseudo-element
Examples include ::before
, ::after
, ::first-line
, and ::first-letter
.
Read MDN for more details.
If you add !important
to a CSS property, it will override all other rules regardless of order.
Some CSS properties (like color
and font-family
) are inherited by child elements from their parent.