Advertisement
Google Ad Slot: content-top
CSS Selectors
CSS selectors are patterns used to select and apply styles to HTML elements. They define which elements the style rules will affect. Selectors can be simple (like targeting an element directly) or more complex (targeting based on attributes, classes, or relationships between elements).
Basic Types of Selectors:
- Element Selector
- Class Selector
- ID Selector
- Universal Selector
- Attribute Selector
Element Selector:
Targets all instances of a specified HTML element.
Class Selector:
Targets elements with a specific class attribute. It is prefixed with a dot (.).
ID Selector:
Targets an element with a specific id attribute. It is prefixed with a hash (#).
Universal Selector:
Targets all elements on the page. It is denoted by an asterisk (*).
Attribute Selector:
Targets elements based on their attributes.
Combinators (Relationship Between Elements):
- Descendant Selector
- Child Selector (
>) - Adjacent Sibling Selector (
+) - General Sibling Selector (
~) - Pseudo-Class Selector
- Pseudo-Element Selector
- Grouping Selector
Descendant Selector:
Targets elements that are descendants (children, grandchildren, etc.) of a specific parent element.
Child Selector (>):
Targets elements that are direct children of a specified element.
Adjacent Sibling Selector (+):
Targets an element that is immediately adjacent to a specified element.
General Sibling Selector (~):
Targets all siblings of a specified element.
Pseudo-Class Selector:
Targets elements based on their state or position (like hover, first-child, etc.).
Pseudo-Element Selector:
Targets and styles specific parts of an element, like the first letter or first line.
Grouping Selector:
Apply the same style to multiple elements by separating them with a comma.