CSS variables (custom properties) store reusable values in stylesheets.
:root {
--main-color: #0077ff;
--padding: 10px;
}
button {
background: var(--main-color);
padding: var(--padding);
}
✔ They make styles consistent and easier to maintain.
➡ CSS Variables → live in browser, can change at runtime with JavaScript.
➡ SASS/LESS Variables → processed at compile-time, not dynamic.
They extend CSS with variables, nesting, mixins, and functions. Compiled into normal CSS before deployment.
$primary: #0077ff;
body {
background: $primary;
}
Specificity determines which rule takes precedence. Calculated as (inline, id, class, element):
▶ Relative → doesn’t create new stacking context.
▶ Absolute → positioned relative to nearest positioned ancestor.
▶ Fixed → positioned relative to viewport (creates new stacking context).
Define relationships between selectors:
They define special states or patterns.
p:not(.highlight) { color: gray; }
li:nth-child(2) { color: red; }
div:nth-of-type(3) { border: 1px solid; }
➡ inline-block → simple inline boxes with manual spacing.
➡ flex → 1D layout (row or column).
➡ grid → 2D layout (rows + columns).
/* Using Flexbox */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
/* Using Grid */
.parent {
display: grid;
place-items: center;
}
They replace directional properties (top/left/right/bottom) with flow-relative ones.
margin-inline-start, padding-block-end
Shorthand combines multiple related properties into one line.
margin: 10px 20px; background: #0077ff url(img.jpg) no-repeat;
Transitions animate property changes smoothly over time.
div {
transition: background 0.5s, transform 0.3s;
}
Animatable properties: color, transform, background, opacity, etc.
They restrict element resizing range — controlling its flexibility in responsive layouts.
Critical CSS = essential styles for above-the-fold content. Load inline or early to render pages faster, delay non-critical CSS.
➡ Progressive Enhancement: Start with basic features, add advanced ones if supported.
➡ Graceful Degradation: Build full experience, ensure it still works in older browsers.