/* Mobile-first base styles (no media query needed) */
.container {
width: 100%;
padding: 1rem;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: 1fr; /* Single column on mobile */
gap: 1rem;
}
.nav {
flex-direction: column;
}
/* Small tablets - 640px */
@media (min-width: 640px) {
.container {
max-width: 640px;
}
.grid {
grid-template-columns: repeat(2, 1fr); /* 2 columns */
}
.text-sm-center {
text-align: center;
}
}
/* Tablets - 768px */
@media (min-width: 768px) {
.container {
max-width: 768px;
padding: 1.5rem;
}
.grid {
grid-template-columns: repeat(3, 1fr); /* 3 columns */
}
.nav {
flex-direction: row;
}
.sidebar {
display: block; /* Show sidebar on tablets */
}
h1 {
font-size: 2.5rem;
}
}
/* Desktop - 1024px */
@media (min-width: 1024px) {
.container {
max-width: 1024px;
padding: 2rem;
}
.grid {
grid-template-columns: repeat(4, 1fr); /* 4 columns */
gap: 2rem;
}
.two-column-layout {
display: grid;
grid-template-columns: 250px 1fr;
}
h1 {
font-size: 3rem;
}
}
/* Large desktop - 1280px */
@media (min-width: 1280px) {
.container {
max-width: 1280px;
}
.grid-wide {
grid-template-columns: repeat(6, 1fr);
}
}
/* Extra large - 1536px */
@media (min-width: 1536px) {
.container {
max-width: 1536px;
}
}
/* Max-width queries (desktop-first approach - less common) */
@media (max-width: 767px) {
.hide-on-mobile {
display: none;
}
}
/* Range queries (between two breakpoints) */
@media (min-width: 768px) and (max-width: 1023px) {
.tablet-only {
display: block;
}
}
/* Orientation queries */
@media (orientation: portrait) {
.portrait-layout {
flex-direction: column;
}
}
@media (orientation: landscape) {
.landscape-layout {
flex-direction: row;
}
}
/* Dark mode support */
@media (prefers-color-scheme: dark) {
:root {
--bg-color: #1a1a1a;
--text-color: #ffffff;
--border-color: #444;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
}
.card {
background: #2a2a2a;
border-color: var(--border-color);
}
}
@media (prefers-color-scheme: light) {
:root {
--bg-color: #ffffff;
--text-color: #000000;
--border-color: #ddd;
}
}
/* Reduced motion for accessibility */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* High contrast mode */
@media (prefers-contrast: high) {
.button {
border: 2px solid currentColor;
}
}
/* Print styles */
@media print {
.no-print {
display: none;
}
body {
font-size: 12pt;
color: black;
background: white;
}
a {
text-decoration: underline;
color: black;
}
a[href]::after {
content: " (" attr(href) ")";
}
}
/* Hover capability detection */
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: blue;
}
}
/* Touch device detection */
@media (hover: none) and (pointer: coarse) {
.button {
padding: 1rem; /* Larger touch targets */
min-height: 44px;
}
}
/* Container queries (modern approach) */
.card-container {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 150px 1fr;
}
}
/* Fluid typography with clamp() */
h1 {
font-size: clamp(1.5rem, 5vw, 3rem);
/* min: 1.5rem, preferred: 5vw, max: 3rem */
}
p {
font-size: clamp(1rem, 2vw + 0.5rem, 1.25rem);
}
/* Responsive spacing */
.section {
padding: clamp(1rem, 5vw, 3rem);
margin-bottom: clamp(2rem, 10vh, 5rem);
}
/* Aspect ratio for responsive media */
.video-container {
aspect-ratio: 16 / 9;
}
@media (max-width: 767px) {
.video-container {
aspect-ratio: 4 / 3;
}
}
/* Responsive utilities */
@media (min-width: 768px) {
.md\:hidden { display: none; }
.md\:block { display: block; }
.md\:flex { display: flex; }
.md\:grid { display: grid; }
}
@media (min-width: 1024px) {
.lg\:w-1\/2 { width: 50%; }
.lg\:w-1\/3 { width: 33.333333%; }
.lg\:w-2\/3 { width: 66.666667%; }
}
/* Responsive images */
img {
max-width: 100%;
height: auto;
}
.responsive-img {
width: 100%;
height: auto;
object-fit: cover;
}
@media (min-width: 768px) {
.responsive-img {
width: 50%;
}
}
Media queries adapt layouts to different screen sizes using @media rules with breakpoints. I use mobile-first approach starting with base mobile styles and progressively enhancing with min-width queries. Common breakpoints are 640px (sm), 768px (md), 1024px (lg), and 1280px (xl). The orientation feature detects portrait vs landscape mode. Using prefers-color-scheme detects dark mode preference. The prefers-reduced-motion respects accessibility preferences for animations. Viewport units (vw, vh, vmin, vmax) create fluid layouts. Container queries with @container enable component-based responsive design. The clamp() function creates fluid typography that scales between minimum and maximum values.