Cascading Style Sheets (CSS) define how HTML content is presented—layout, colour,
motion, and even accessibility traits. Browsers apply the cascade algorithm
to resolve conflicts and compute the final computed style
for every element.
<h1 style="color:orange">
<head>
<link rel="stylesheet" href="styles.css">
Tip: External style‑sheets are cached across pages, offering the best performance & maintainability.
/* Type, class & ID */
p { … } /* element */
.alert { … } /* class */
#header { … } /* id */
.nav a { … } /* descendant */
.nav > li { … } /* direct child */
.card + .card { … } /* adjacent sibling */
h2 ~ p { … } /* general sibling */
[data-state] { … }
input[type="radio"] { … }
a[href^="https://"] { … }
:is()
, :where()
(zero specificity), :not()
, and the powerful relational pseudo‑class
:has()
let you style a parent based on its children and compose complex queries. The
:has()
selector now enjoys >90 % global support.
/* If an input inside the .field is invalid, outline the .field */
.field:has(input:invalid){
outline:2px solid red;
}
:hover
, :focus-visible
, :first-child
…)Describe an element’s state.
::before
, ::marker
, ::selection
…)Generate or target virtual sub‑parts of an element.
button::before{
content:"→";
margin-inline-end:.4em;
}
When multiple rules match, precedence is determined by
origin (author > user > browser),
importance (!important
),
specificity, and source order.
(inline, IDs, classes|pseudo‑classes|attrs, elements|pseudo‑elements).
/* 0‑1‑0 */
#brand{…}
/* 0‑0‑2 */
nav .link:hover{…}
/* 1‑0‑0 */
style="…"
Typographic properties (color
, font‑family
) inherit by default;
box‑model properties do not. Use inherit
, initial
, revert
, and
revert‑layer
to control behaviour.
px
, cm
, pt
em
, rem
, ex
, ch
vw
, vh
, dvh
, lvh
, svh
cqw
, cqh
(see §10)Beyond sRGB: lab()
, lch()
, oklab()
, oklch()
, and the
color-mix() function for dynamic blends.
p{
color: color-mix(in lch, purple 40%, black);
}
Use clamp() for fluid sizing: clamp(1rem,2vw,1.6rem)
.
Each element generates boxes with content, padding, border, margin.
box-sizing:border-box
simplifies layout by including padding + border in width/height.
Control overflow with overflow
, overflow-x
/y
. In 2025 you can drive
animations with scroll‑timeline
for smooth parallax effects.
Value | Description |
---|---|
block | Starts on new line, full width |
inline | Flows with text |
inline-block | Intrinsic size, flows inline |
flex /grid | Modern layout modes |
contents | Element disappears, children remain |
.sticky-bar{
position: sticky;
top:0;
}
1‑D layout—axis & order control.
.toolbar{
display:flex;
gap:.5rem;
flex-wrap:wrap;
justify-content:space-between;
}
2‑D layout with explicit tracks; subgrid
lets children inherit track sizing from ancestors.
.cards{
display:grid;
grid-template-columns:repeat(auto-fill,minmax(16rem,1fr));
gap:1.2rem;
}
figure{
display:subgrid;
grid-template-rows:auto 1fr auto;
}
Use columns
for magazine‑style layouts, fallback to floats/clears when legacy support is critical.
@media (min-width:48rem){
nav ul{ flex-direction:row; }
}
Let components adapt to their parent’s size or style.
.card{
container-type:inline-size;
}
@container (min-width:35cqw){
.card{ grid-template-columns:14rem 1fr; }
}
As of 2025 you can query styles (@container style(--theme:dark)
) and scroll states for even richer adaptability.
@font-face
with font-variation-settings
)margin-inline
, text-align:start
text-wrap:balance;
for auto balanced headings.@font-face{
font-family:"Inter";
src:url("InterVar.woff2") format("woff2-variations");
}
.btn{
transition: background .25s ease, transform .3s;
}
.btn:hover{
background:orange;
transform:translateY(-2px);
}
@keyframes pulse{
50%{ transform:scale(1.06);}
}
.logo{
animation:pulse 3s infinite alternate;
}
Smooth page‑to‑page transforms with view-transition-name
.
:root{
--radius:1.2rem;
}
.card{
border-radius:var(--radius);
border:1px solid oklch(60% 0.18 200);
}
@layer reset, base, components, utilities;
@layer base{
h1{margin-bottom:.5em;}
}
Layers (@layer
) allow you to control cascade order without over‑specificity.
@media (prefers-reduced-motion:reduce)
@media (prefers-color-scheme:dark)
:focus-visible
and :target
for keyboard navigation@supports (grid-template-rows:subgrid){
.photo-grid{ grid-template-rows:subgrid; }
}
caniuse‑lite
with Autoprefixercontent‑visibility:auto
for heavy off‑screen sections.media="print" onload
trick.