1. Advanced Combinators and Selectors
We previously covered basic selectors (Type, Class, ID) and grouping/chaining. These advanced selectors define relationships between elements beyond simple parent-child relationships.
A. Attribute Selectors
These selectors target elements based on the presence or value of an HTML attribute.
| Selector | Description | Example |
[attr] | Selects elements that have the attribute. | [data-tooltip] |
[attr="val"] | Selects elements where the attribute equals the value exactly. | [type="submit"] |
[attr^="val"] | Selects elements where the attribute value starts with val. | [class^="col-"] (Bootstrap columns) |
[attr$="val"] | Selects elements where the attribute value ends with val. | [href$=".pdf"] (PDF links) |
[attr*="val"] | Selects elements where the attribute value contains val. | [class*="icon"] |
/* Styles all input fields that are type="text" */
input[type="text"] {
border: 1px solid #ccc;
}
B. Combinators (Relationship Selectors)
Combinators define the relationship between two selectors:
| Combinator | Symbol | Description | Example |
| Descendant | (Space) | Selects all descendants of the first element. | div p (Selects all <p> inside a <div>) |
| Child | > | Selects only direct children of the first element. | ul > li (Selects only <li> directly under a <ul>) |
| Adjacent Sibling | + | Selects the element immediately following the first element (must share the same parent). | h1 + p (Selects the first <p> right after an <h1>) |
| General Sibling | ~ | Selects all elements that follow the first element (must share the same parent). | h1 ~ p (Selects all subsequent <p> elements) |
/* Applies a large top margin only to paragraphs that immediately follow an H1 */
h1 + p {
margin-top: 2rem;
}
C. The :not() Pseudo-Class
The negation pseudo-class selects elements that do not match the specified selector.
/* Selects all div elements EXCEPT those with the class="main" */
div:not(.main) {
opacity: 0.8;
}
/* Selects all links that are NOT external (do not start with http) */
a:not([href^="http"]) {
/* ... custom internal link style */
}
2. Advanced CSS Functions
Modern CSS introduced powerful functions that allow for highly flexible, responsive, and dynamic values that adapt to the viewport and content constraints.
A. The clamp() Function
The clamp() function pins a value between a minimum and a maximum size. It takes three arguments: min, preferred, and max.
clamp(min,preferred,max)
- If the
preferredvalue is greater thanmax, the result ismax. - If the
preferredvalue is less thanmin, the result ismin. - Otherwise, the result is
preferred.
/* Example: Fluid Typography */
h1 {
/* Min size: 2rem, Max size: 5rem, Preferred size: 5% of viewport width */
font-size: clamp(2rem, 5vw, 5rem);
}
/* This ensures the H1 scales with the viewport (5vw) but never drops below 2rem (phone) or exceeds 5rem (desktop). */
B. The min() and max() Functions
These functions select the smallest or largest value from a comma-separated list of expressions.
| Function | Purpose | Example |
min() | Uses the smallest computed value. | width: min(50%, 500px); |
max() | Uses the largest computed value. | padding-left: max(2rem, 5vw); |
/* The container will be 50% wide, UNLESS 50% is wider than 500px, in which case it caps at 500px. */
.container {
width: min(50%, 500px);
}
