Create websites that work beautifully on all devices and screen sizes.
Responsive design is an approach to web design that makes web pages render well on a variety of devices and window or screen sizes. It ensures that users have a good viewing experience regardless of the device they're using.
The viewport meta tag is crucial for responsive design. It tells the browser how to control the page's dimensions and scaling on different devices.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Media queries allow you to apply different styles based on device characteristics like screen width, height, orientation, and resolution.
/* Mobile-first approach */
.container {
width: 100%;
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
width: 750px;
margin: 0 auto;
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.container {
width: 1000px;
}
}
/* Large desktop */
@media (min-width: 1200px) {
.container {
width: 1200px;
}
}
/* Extra small devices (phones, 576px and down) */
@media (max-width: 575.98px) { }
/* Small devices (landscape phones, 576px and up) */
@media (min-width: 576px) { }
/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) { }
/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) { }
/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) { }
Resize your browser window to see the responsive behavior
/* Orientation */
@media (orientation: landscape) {
.sidebar {
width: 200px;
}
}
@media (orientation: portrait) {
.sidebar {
width: 100%;
height: 200px;
}
}
/* Resolution */
@media (min-resolution: 2dppx) {
.image {
background-image: url('image@2x.jpg');
}
}
/* Multiple conditions */
@media (min-width: 768px) and (max-width: 1024px) {
.container {
width: 90%;
}
}
/* Print styles */
@media print {
.no-print {
display: none;
}
body {
font-size: 12pt;
line-height: 1.4;
}
}
Use relative units instead of fixed pixels for responsive layouts.
/* Percentage - relative to parent */
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
}
/* Viewport units */
.full-height {
height: 100vh; /* 100% of viewport height */
}
.half-width {
width: 50vw; /* 50% of viewport width */
}
/* Relative to font size */
.text {
font-size: 1rem; /* 16px by default */
line-height: 1.5em; /* 1.5 times the font size */
margin: 1em; /* 1 times the font size */
}
/* Root-relative units */
.large-text {
font-size: 1.5rem; /* 1.5 times root font size */
}
.small-text {
font-size: 0.875rem; /* 0.875 times root font size */
}
90% width container
50% width container
25% width container
/* min() - takes the smaller value */
.responsive-width {
width: min(90%, 800px);
}
/* max() - takes the larger value */
.minimum-width {
width: max(300px, 50%);
}
/* clamp() - clamps a value between min and max */
.fluid-text {
font-size: clamp(1rem, 4vw, 2rem);
}
/* calc() - perform calculations */
.flexible-margin {
margin: calc(2rem + 2vw);
}
/* Grid with auto-fit */
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
Make images scale properly within their containers.
/* Basic responsive image */
img {
max-width: 100%;
height: auto;
}
/* Responsive image with aspect ratio */
.image-container {
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
position: relative;
}
.image-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
/* Picture element for different image sources */
<picture>
<source media="(min-width: 768px)" srcset="large.jpg">
<source media="(min-width: 480px)" srcset="medium.jpg">
<img src="small.jpg" alt="Description">
</picture>
Design for mobile devices first, then enhance for larger screens. This approach ensures better performance and user experience.
/* Base styles (mobile first) */
.container {
width: 100%;
padding: 1rem;
margin: 0;
}
.navigation {
display: block;
width: 100%;
}
.navigation ul {
list-style: none;
padding: 0;
margin: 0;
}
.navigation li {
display: block;
padding: 0.5rem;
border-bottom: 1px solid #ddd;
}
/* Tablet styles */
@media (min-width: 768px) {
.container {
width: 90%;
margin: 0 auto;
}
.navigation {
display: flex;
justify-content: space-between;
}
.navigation ul {
display: flex;
gap: 2rem;
}
.navigation li {
border: none;
padding: 0;
}
}
/* Desktop styles */
@media (min-width: 1024px) {
.container {
width: 80%;
max-width: 1200px;
}
}
/* Start with basic functionality */
.button {
padding: 0.5rem 1rem;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
}
/* Add hover effects for devices that support it */
@media (hover: hover) {
.button:hover {
background: #0056b3;
transform: translateY(-2px);
transition: all 0.2s ease;
}
}
/* Add animations for devices that support it */
@media (prefers-reduced-motion: no-preference) {
.animated {
animation: fadeIn 0.5s ease-out;
}
}
/* Responsive grid */
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
padding: 1rem;
}
/* Grid with different column counts */
.responsive-grid {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 768px) {
.responsive-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.responsive-grid {
grid-template-columns: repeat(3, 1fr);
}
}
@media (min-width: 1200px) {
.responsive-grid {
grid-template-columns: repeat(4, 1fr);
}
}
/* Responsive flexbox */
.flex-container {
display: flex;
flex-direction: column;
gap: 1rem;
}
.flex-item {
flex: 1;
min-width: 0; /* Prevents flex items from overflowing */
}
/* Responsive flex direction */
@media (min-width: 768px) {
.flex-container {
flex-direction: row;
}
.flex-item {
flex: 1 1 300px; /* grow, shrink, basis */
}
}
/* Responsive wrapping */
.flex-wrap {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.flex-wrap > * {
flex: 1 1 250px;
}
/* Use efficient selectors */
/* Good */
.container .item { }
/* Better */
.item { }
/* Use will-change sparingly */
.animated-element {
will-change: transform;
}
/* Optimize animations */
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}