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.
Example
p {
color : blue;
font-size: 40 px;
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Class Selector: Targets elements with a specific class attribute. It is prefixed with a dot (. ).
Example
.container {
padding : 20 px; /* Targets elements with the class "container" */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
ID Selector: Targets an element with a specific id attribute. It is prefixed with a hash (# ).
Example
#header {
background-color: gray; /* Targets the element with id="header" */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Universal Selector: Targets all elements on the page. It is denoted by an asterisk (* ).
Example
* {
text-align: center;
color : red;
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Attribute Selector: Targets elements based on their attributes.
Example
input[type="text" ] {
border : 3 px solid red; /* Targets <input> elements with type="text" */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
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.
Example
div p {
color : red; /* Targets <p> elements inside <div> elements */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Child Selector ( > ) :Targets elements that are direct children of a specified element.
Example
div > p {
color : green; /* Targets <p> elements that are direct children of <div> */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Adjacent Sibling Selector ( + ) :Targets an element that is immediately adjacent to a specified element.
Example
h1 + p {
font-size: 30 px; /* Targets the <p> element directly after an <h1> */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
General Sibling Selector ( ~ ) : Targets all siblings of a specified element.
Example
h1 ~ p {
color : blue; /* Targets all <p> elements that are siblings of <h1> */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Pseudo-Class Selector :Targets elements based on their state or position (like hover , first-child , etc.).
Example
a :hover {
color : red; /* Changes the color of <a> elements when hovered */
}
p :first-child {
font-weight: bold; /* Targets the first <p> child in a container */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Pseudo-Element Selector : Targets and styles specific parts of an element, like the first letter or first line.
Example
p ::first-letter {
font-size: 2 em; /* Enlarges the first letter of each <p> */
}
p ::before {
content : "Note: " ; /* Inserts text before each <p> element */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Grouping Selector :Apply the same style to multiple elements by separating them with a comma.
Example
h1, h2, h3 {
color : navy; /* Targets all <h1>, <h2>, and <h3> elements */
}
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself