Combinators define relationships between selectors.
Selects all matching descendants regardless of depth.
article p {
color: gray;
}
Selects only direct children.
ul > li {
list-style: none;
}
Selects an element immediately preceded by a specific sibling.
h2 + p {
font-weight: bold;
}
Selects all siblings that follow a specific element.
h2 ~ p {
color: #666;
}
Pseudo-classes describe a special state of an element.
a:hover {
color: orange;
}
input:focus {
border-color: blue;
}
li:nth-child(odd) {
background: #f0f0f0;
}
li:nth-child(3n) {
border-bottom: 1px solid #ccc;
}
p:first-child {
margin-top: 0;
}
p:last-child {
margin-bottom: 0;
}
Pseudo-elements target a specific part of an element.
.note::before {
content: "\26A0";
margin-right: 5px;
}
.link::after {
content: " \2197";
}
p::first-line {
font-variant: small-caps;
}
/* Exact match */
input[type="text"] { border: 1px solid gray; }
/* Contains */
a[href*="example"] { color: green; }
/* Starts with */
a[href^="https"] { font-weight: bold; }
/* Ends with */
img[src$=".png"] { border-radius: 4px; }
.faded {
opacity: 0.5;
}
Style a table with alternating row colors using :nth-child(even), add an icon before each link using ::before, and apply a hover effect that changes background color.