AiTechWorlds
AiTechWorlds

1. Fluid layouts use relative units (%, vw, fr) not fixed pixels
2. Flexible media images and videos scale within their containers
3. Media queries apply different styles at different viewport sizes
4. Mobile first start with mobile styles, enhance for larger screens
<meta name="viewport" content="width=device-width, initial-scale=1">Without this, mobile browsers render at desktop width then scale down media queries won't work correctly.
| Name | Width | Devices |
|---|---|---|
xs | < 480px | Small phones |
sm | 480px 767px | Large phones |
md | 768px 1023px | Tablets |
lg | 1024px 1279px | Laptops |
xl | 1280px 1535px | Desktops |
2xl | โฅ 1536px | Large screens |
/* Mobile-first (min-width) โ recommended */
.container { width: 100%; }
@media (min-width: 768px) {
.container { width: 720px; }
}
@media (min-width: 1024px) {
.container { width: 960px; }
}
/* Desktop-first (max-width) โ legacy approach */
.container { width: 960px; }
@media (max-width: 1023px) { .container { width: 720px; } }
@media (max-width: 767px) { .container { width: 100%; } }| Unit | Relative To | Example Use |
|---|---|---|
% | Parent element | Fluid widths |
vw | Viewport width | Full width sections |
vh | Viewport height | Full screen hero |
em | Parent font size | Component spacing |
rem | Root font size | Typography, global spacing |
ch | Width of 0 character | Readable line lengths |
clamp(min, ideal, max) | Viewport + bounds | Fluid typography |
fr | Fraction of grid space | CSS Grid columns |
svh/dvh | Small/dynamic viewport | Mobile hero sections |
/* Never smaller than 1rem, never larger than 2rem, scales between */
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
}
p {
font-size: clamp(1rem, 1.5vw + 0.5rem, 1.25rem);
}
/* Fluid spacing */
.section {
padding: clamp(2rem, 5vw, 5rem);
}/* Stack on mobile, row on desktop */
.nav {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
@media (min-width: 768px) {
.nav {
flex-direction: row;
gap: 2rem;
}
}
/* Auto-wrap cards */
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 300px; /* grow, shrink, basis 300px โ wraps when needed */
}/* Auto-fill columns โ as many as fit, min 250px each */
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1rem;
}
/* Explicit breakpoint grid */
.layout {
display: grid;
grid-template-columns: 1fr; /* 1 column mobile */
}
@media (min-width: 768px) {
.layout {
grid-template-columns: 1fr 1fr; /* 2 columns tablet */
}
}
@media (min-width: 1024px) {
.layout {
grid-template-columns: 240px 1fr; /* sidebar + content */
}
}/* Prevent overflow, maintain aspect ratio */
img {
max-width: 100%;
height: auto;
display: block;
}
/* Object-fit for fixed-height containers */
.card-image {
width: 100%;
height: 200px;
object-fit: cover; /* crop to fill */
object-position: center;
}<!-- srcset: browser picks best resolution -->
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 800px"
alt="Hero image"
/>
<!-- picture: art direction โ different crops per breakpoint -->
<picture>
<source media="(min-width: 768px)" srcset="hero-wide.jpg">
<source media="(min-width: 480px)" srcset="hero-medium.jpg">
<img src="hero-mobile.jpg" alt="Hero">
</picture>Style elements based on their container's size, not the viewport:
.card-wrapper {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: flex;
flex-direction: row;
}
}/* Dark mode */
@media (prefers-color-scheme: dark) {
body { background: #0f172a; color: #f1f5f9; }
}
/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; transition: none !important; }
}
/* Print */
@media print {
nav, footer, .ads { display: none; }
a::after { content: " (" attr(href) ")"; }
}
/* High DPI / Retina */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
.logo { background-image: url(logo@2x.png); }
}
/* Hover capability (touch vs mouse) */
@media (hover: none) {
.tooltip { display: none; } /* don't show hover tooltips on touch devices */
}
/* Orientation */
@media (orientation: landscape) { }
@media (orientation: portrait) { }px for media query breakpoints instead of em zoom causes layout breaks (use em for accessibility)max width harder to maintain; mobile first with min width is simplermin height insteadGet more notes like this daily on Telegram!
Free study notes, cheat sheets & AI tips
Last reviewed on June 13, 2026 by the AiTechWorlds Notes Team. Free cheat sheet no signup required.
Join AiTechWorlds on Telegram and get daily AI tips, prompt engineering templates, coding resources, and exclusive content 100% free!
No spam. Leave anytime.