Flexbox
One-dimensional layout system for distributing space and aligning items in a row or column.
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
}
.item {
flex: 1 1 200px;
align-self: flex-start;
order: 2;
}
.parent {
display: flex;
justify-content: center;
align-items: center;
}
CSS Transitions
Smoothly interpolate property changes between states.
.button {
background: #6366f1;
transform: translateY(0);
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition:
background 0.2s ease,
transform 0.2s ease,
box-shadow 0.2s ease;
transition: all 0.2s ease;
}
.button:hover {
background: #4f46e5;
transform: translateY(-2px);
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
transition: opacity 0.3s ease-in-out 0s;
Media Queries
Apply styles conditionally based on viewport size, orientation, or device capabilities.
@media (min-width: 640px) { }
@media (min-width: 768px) { }
@media (min-width: 1024px) { }
@media (min-width: 1280px) { }
@media (width >= 768px) { }
@media (768px <= width < 1024px) { }
@media (orientation: landscape) { }
@media (prefers-color-scheme: dark) {
:root { --bg: #0f172a; --text: #f8fafc; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; }
}
@media print {
.no-print { display: none; }
}
CSS Custom Properties (Variables)
CSS3Define reusable values with --name syntax, accessed via var(). Live in the cascade and inherit.
:root {
--color-primary: #6366f1;
--color-text: #1f2937;
--spacing-base: 1rem;
--radius-md: 0.5rem;
}
.button {
background: var(--color-primary);
padding: var(--spacing-base);
border-radius: var(--radius-md);
color: var(--color-button-text, white);
}
[data-theme="dark"] {
--color-primary: #818cf8;
--color-text: #f9fafb;
}
CSS Grid
Two-dimensional layout system for rows AND columns simultaneously.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto;
gap: 1rem 2rem;
grid-template-columns: [sidebar-start] 250px [sidebar-end content-start] 1fr [content-end];
}
.item {
grid-column: 1 / 3;
grid-row: 2 / span 2;
}
.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
CSS Transforms
Translate, rotate, scale, and skew elements without affecting document flow.
.el {
transform: translateX(20px);
transform: translateY(-50%);
transform: translate(-50%, -50%);
transform: rotate(45deg);
transform: scale(1.2);
transform: scaleX(0.5);
transform: skewX(10deg);
transform: translateY(-10px) rotate(5deg) scale(1.05);
transform-origin: top left;
transform-origin: 50% 100%;
}
.card {
transform: perspective(1000px) rotateY(15deg);
transform-style: preserve-3d;
backface-visibility: hidden;
}
CSS Animations & Keyframes
Declarative multi-step animations with @keyframes and the animation shorthand.
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.card {
animation: fadeInUp 0.4s ease-out forwards;
animation: fadeInUp 0.4s ease-out 0.1s 1 normal forwards;
}
.loading {
animation: pulse 1.5s ease-in-out infinite;
}
.card:hover { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) {
.card { animation: none; }
}
Pseudo-elements
::before and ::after insert generated content; other pseudo-elements target specific parts of elements.
.badge::before {
content: "★ ";
color: gold;
}
.heading::after {
content: "";
display: block;
width: 60px;
height: 3px;
background: var(--color-primary);
margin-top: 0.5rem;
}
input::placeholder { color: #9ca3af; font-style: italic; }
::selection { background: #6366f1; color: white; }
p::first-letter {
font-size: 3em;
float: left;
line-height: 1;
margin-right: 0.1em;
}
li::marker { color: var(--color-primary); }
aspect-ratio
Maintain a specific width-to-height ratio without padding hacks.
.video-wrapper {
aspect-ratio: 16 / 9;
width: 100%;
}
.thumbnail {
aspect-ratio: 1;
width: 80px;
object-fit: cover;
}
.card-img {
aspect-ratio: 4 / 3;
width: 100%;
object-fit: cover;
}
.old-way {
position: relative;
padding-top: 56.25%;
}
.old-way > * {
position: absolute;
inset: 0;
}
clamp(), min(), max()
Fluid sizing functions that adapt between fixed bounds without media queries.
.heading {
font-size: clamp(1.5rem, 4vw, 3rem);
}
.container {
width: clamp(300px, 90%, 1200px);
width: min(max(300px, 90%), 1200px);
}
.img {
width: min(500px, 100%);
}
.sidebar {
min-height: max(300px, 50vh);
}
.section {
padding: clamp(2rem, 5vw, 6rem);
}
Modern Pseudo-classes
:is(), :where(), :has(), and :not() for powerful selector composition.
:is(h1, h2, h3, h4) { line-height: 1.2; }
:is(article, section, aside) p { margin-bottom: 1rem; }
:where(h1, h2, h3) { margin-top: 1.5em; }
a:not(.button):not([aria-current]) { color: var(--link-color); }
.card:has(img) { padding: 0; }
form:has(:invalid) .submit-btn {
opacity: 0.5;
pointer-events: none;
}
li:has(+ li) { margin-bottom: 0.5rem; }
button:focus-visible { outline: 2px solid var(--color-primary); }
clip-path
Mask elements to arbitrary shapes: circles, polygons, ellipses, or SVG paths.
.circle {
clip-path: circle(50%);
clip-path: circle(60px at center);
}
.polygon {
clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%);
}
.hexagon {
clip-path: polygon(
50% 0%, 100% 25%, 100% 75%,
50% 100%, 0% 75%, 0% 25%
);
}
.ellipse {
clip-path: ellipse(60% 40% at center);
}
.reveal {
clip-path: inset(0 100% 0 0);
transition: clip-path 0.5s ease;
}
.reveal.visible {
clip-path: inset(0 0% 0 0);
}
CSS Logical Properties
Flow-relative properties that work correctly in both LTR and RTL text directions.
.el {
margin-inline: auto;
margin-inline-start: 1rem;
margin-block: 2rem;
padding-inline: 1.5rem;
padding-block: 1rem;
border-inline-start: 3px solid var(--color-primary);
border-block-end: 1px solid #e5e7eb;
inline-size: 100%;
block-size: auto;
max-inline-size: 65ch;
inset-inline-start: 0;
inset: 0 0 auto 0;
}
Container Queries
CSS Containment Level 3Style elements based on their container's size rather than the viewport.
.card-wrapper {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 120px 1fr;
}
}
@container (min-width: 600px) {
.card-title {
font-size: 1.5rem;
}
}
.card-img {
width: 50cqi;
}
Cascade Layers (@layer)
CSS Cascade 5Organize styles into named layers with explicit priority, removing specificity battles.
@layer reset, base, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; margin: 0; }
}
@layer base {
body { font-family: system-ui; line-height: 1.5; }
a { color: var(--link-color); }
}
@layer components {
.button {
padding: 0.5rem 1rem;
border-radius: 0.25rem;
}
}
@layer utilities {
.mt-4 { margin-top: 1rem; }
.hidden { display: none; }
}
.override { color: red; }
@import url("bootstrap.css") layer(third-party);
CSS Subgrid
CSS Grid Level 2Nested grids that inherit parent's track definitions for precise alignment.
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto 1fr auto;
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
.card-title { grid-row: 1; }
.card-body { grid-row: 2; }
.card-footer { grid-row: 3; }
@property (Houdini)
CSS HoudiniRegister typed CSS custom properties with syntax, initial value, and inheritance control.
@property --hue {
syntax: '<number>';
initial-value: 200;
inherits: false;
}
@property --progress {
syntax: '<percentage>';
initial-value: 0%;
inherits: false;
}
.spinner {
--hue: 200;
color: hsl(var(--hue) 80% 60%);
transition: --hue 0.5s;
}
.spinner:hover {
--hue: 320;
}
@keyframes rotate-hue {
to { --hue: 360; }
}
.rainbow {
animation: rotate-hue 3s linear infinite;
color: hsl(var(--hue) 80% 60%);
}