Master the art of targeting HTML elements and applying styles effectively.
CSS selectors are patterns used to select and style HTML elements. They are the foundation of CSS and allow you to target specific elements or groups of elements on your webpage.
Targets all elements of a specific type.
/* Targets all paragraph elements */
p {
color: blue;
font-size: 16px;
line-height: 1.6;
}
/* Targets all heading elements */
h1, h2, h3 {
color: #333;
font-weight: bold;
}
This paragraph is styled with an element selector (blue color)
This paragraph has no special styling
Targets elements with a specific class attribute.
/* Targets elements with class="highlight" */
.highlight {
background-color: yellow;
padding: 5px;
border-radius: 3px;
}
/* Multiple classes */
.button.primary {
background-color: #007bff;
color: white;
}
This paragraph has the "class-selector" class (yellow background)
This paragraph has no class
Targets a specific element with a unique ID.
/* Targets element with id="header" */
#header {
font-size: 24px;
font-weight: bold;
text-align: center;
}
/* Note: IDs should be unique on a page */
#main-content {
max-width: 1200px;
margin: 0 auto;
}
This paragraph has the ID "id-selector" (large, bold text)
This paragraph has no ID
Targets elements that are descendants of another element.
/* Targets all p elements inside div elements */
div p {
margin: 10px 0;
color: #666;
}
/* Targets all li elements inside ul elements */
ul li {
list-style-type: disc;
margin-bottom: 5px;
}
This paragraph is inside a div (styled with descendant selector)
This paragraph is also inside the same div
This paragraph is outside the div (not styled)
Targets direct child elements only.
/* Targets direct p children of div */
div > p {
border-left: 3px solid #007bff;
padding-left: 10px;
}
/* Targets direct li children of ul */
ul > li {
font-weight: bold;
}
Targets the first sibling that follows immediately after.
/* Targets p that immediately follows h1 */
h1 + p {
font-size: 18px;
color: #666;
margin-top: 10px;
}
Targets all siblings that follow.
/* Targets all p elements that follow h1 */
h1 ~ p {
text-indent: 20px;
}
/* Link states */
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: red; }
a:active { color: orange; }
/* Form states */
input:focus {
border-color: #007bff;
box-shadow: 0 0 5px rgba(0,123,255,0.3);
}
input:disabled {
background-color: #f8f9fa;
cursor: not-allowed;
}
/* Position-based */
li:first-child { font-weight: bold; }
li:last-child { border-bottom: none; }
li:nth-child(odd) { background-color: #f8f9fa; }
li:nth-child(even) { background-color: white; }
/* State-based */
.checkbox:checked + label {
text-decoration: line-through;
}
Hover over this text to see the :hover pseudo-class in action
This is a link (try hovering)/* First letter */
p::first-letter {
font-size: 2em;
color: purple;
float: left;
margin-right: 5px;
}
/* First line */
p::first-line {
font-weight: bold;
color: #333;
}
/* Before and After content */
.button::before {
content: "→ ";
margin-right: 5px;
}
.button::after {
content: " ←";
margin-left: 5px;
}
/* Selection */
::selection {
background-color: yellow;
color: black;
}
This paragraph demonstrates the ::first-letter pseudo-element. The first letter should be larger and purple.
CSS specificity determines which styles are applied when there are conflicting rules. Higher specificity wins.
/* Specificity: 1 point */
p { color: black; }
/* Specificity: 10 points */
.highlight { color: yellow; }
/* Specificity: 11 points (10 + 1) */
p.highlight { color: orange; }
/* Specificity: 100 points */
#header { color: blue; }
/* Specificity: 101 points (100 + 1) */
#header p { color: red; }
/* Specificity: 111 points (100 + 10 + 1) */
#header p.highlight { color: green; }
This has inline style (highest specificity)
This has both ID and class
This has only class
This has only element selector
/* Block */
.card { }
/* Element */
.card__title { }
.card__content { }
.card__button { }
/* Modifier */
.card--featured { }
.card__button--primary { }
.card__button--secondary { }