CSS selectors are patterns used to select and style HTML elements in a web page. They determine which elements the CSS rules will apply to. Here are some common types of CSS selectors:
-
Type Selector: Selects all elements of a specific type (tag).
p { color: blue; /* Styles all <p> elements */ } -
Class Selector: Selects elements with a specific class attribute. It starts with a period (
.)..highlight { background-color: yellow; /* Styles elements with class "highlight" */ } -
ID Selector: Selects a unique element with a specific ID attribute. It starts with a hash (
#).#header { font-size: 24px; /* Styles the element with ID "header" */ } -
Descendant Selector: Selects elements that are nested within a specific parent element.
div p { color: red; /* Styles <p> elements that are inside <div> elements */ } -
Universal Selector: Selects all elements on the page. It is represented by an asterisk (
*).* { margin: 0; /* Resets margin for all elements */ } -
Attribute Selector: Selects elements based on the presence or value of a specific attribute.
a[target="_blank"] { color: green; /* Styles <a> elements with target="_blank" */ }
These selectors can be combined and used in various ways to create complex styles and layouts for web pages.
