responsive-design
Mobile-first responsive design covering fluid layouts, media queries, flexbox, grid, viewport units, and responsive images
What this skill does
# Responsive Design Skill
## When to Use This Skill
Use this skill when you need to:
- Design and implement mobile-first responsive layouts
- Create fluid, adaptive interfaces that work across all device sizes
- Implement modern CSS layout techniques (Flexbox, Grid)
- Optimize images and media for different screen sizes
- Build accessible, touch-friendly interfaces
- Implement responsive typography and spacing systems
- Create container-based responsive components
- Optimize performance for mobile devices
- Ensure consistent user experience across devices
- Implement responsive navigation patterns
- Design responsive forms and data tables
- Create adaptive card layouts and galleries
- Build responsive dashboards and admin interfaces
## Core Concepts
### Mobile-First Philosophy
Mobile-first design means starting with mobile layout and progressively enhancing for larger screens.
**Why Mobile-First?**
- Forces focus on essential content and features
- Easier to scale up than scale down
- Better performance on mobile devices
- Aligns with modern usage patterns (mobile-first world)
- Simpler CSS with fewer overrides
**Mobile-First Approach:**
```css
/* Base styles for mobile (320px+) */
.container {
padding: 1rem;
width: 100%;
}
/* Tablet (768px+) */
@media (min-width: 768px) {
.container {
padding: 2rem;
max-width: 720px;
margin: 0 auto;
}
}
/* Desktop (1024px+) */
@media (min-width: 1024px) {
.container {
max-width: 960px;
padding: 3rem;
}
}
/* Large Desktop (1280px+) */
@media (min-width: 1280px) {
.container {
max-width: 1200px;
}
}
```
### Breakpoint Strategy
Standard breakpoints based on common device sizes:
```css
/* Extra Small (Mobile) - Default */
/* No media query needed */
/* Small (Large Mobile) */
@media (min-width: 480px) { }
/* Medium (Tablet) */
@media (min-width: 768px) { }
/* Large (Desktop) */
@media (min-width: 1024px) { }
/* Extra Large (Wide Desktop) */
@media (min-width: 1280px) { }
/* XXL (Ultra-wide) */
@media (min-width: 1536px) { }
```
**Custom Breakpoints:**
```css
/* Use custom breakpoints based on content, not devices */
/* When text becomes too long to read comfortably */
@media (min-width: 40em) { /* 640px */ }
/* When sidebar can fit alongside content */
@media (min-width: 60em) { /* 960px */ }
/* When multi-column layout makes sense */
@media (min-width: 75em) { /* 1200px */ }
```
### Fluid Layouts
Create layouts that adapt smoothly to any screen size:
```css
/* Fluid container with constraints */
.container {
width: 100%;
max-width: 1200px;
margin-left: auto;
margin-right: auto;
padding-left: clamp(1rem, 5vw, 3rem);
padding-right: clamp(1rem, 5vw, 3rem);
}
/* Fluid grid */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: clamp(1rem, 3vw, 2rem);
}
/* Fluid typography */
.heading {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
line-height: 1.2;
}
```
## CSS Techniques
### Flexbox for Responsive Layouts
Flexbox excels at one-dimensional layouts (row or column):
```css
/* Responsive navigation */
.nav {
display: flex;
flex-wrap: wrap;
gap: 1rem;
justify-content: space-between;
align-items: center;
}
.nav-links {
display: flex;
flex-wrap: wrap;
gap: 1rem;
list-style: none;
}
@media (max-width: 767px) {
.nav {
flex-direction: column;
align-items: stretch;
}
.nav-links {
flex-direction: column;
}
}
/* Flexible card layout */
.card-container {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 300px; /* grow, shrink, base width */
min-width: 0; /* Allow flex items to shrink below content size */
}
/* Responsive sidebar layout */
.layout {
display: flex;
flex-wrap: wrap;
gap: 2rem;
}
.sidebar {
flex: 1 1 250px;
}
.main-content {
flex: 3 1 500px;
}
```
### CSS Grid for Complex Layouts
Grid is perfect for two-dimensional layouts:
```css
/* Auto-responsive grid */
.grid-auto {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 2rem;
}
/* Named grid areas for semantic layouts */
.page-layout {
display: grid;
grid-template-areas:
"header"
"nav"
"main"
"aside"
"footer";
gap: 1rem;
}
@media (min-width: 768px) {
.page-layout {
grid-template-areas:
"header header"
"nav nav"
"main aside"
"footer footer";
grid-template-columns: 2fr 1fr;
}
}
@media (min-width: 1024px) {
.page-layout {
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 250px;
}
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
/* Responsive grid with precise control */
.gallery {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 1rem;
}
.gallery-item {
grid-column: span 12; /* Full width on mobile */
}
@media (min-width: 480px) {
.gallery-item {
grid-column: span 6; /* Half width on small screens */
}
}
@media (min-width: 768px) {
.gallery-item {
grid-column: span 4; /* Third width on tablets */
}
}
@media (min-width: 1024px) {
.gallery-item {
grid-column: span 3; /* Quarter width on desktop */
}
}
/* Featured item spans more columns */
.gallery-item.featured {
grid-column: span 12;
}
@media (min-width: 768px) {
.gallery-item.featured {
grid-column: span 8;
}
}
```
### Media Queries Advanced Techniques
```css
/* Orientation-based queries */
@media (orientation: portrait) {
.image-hero {
aspect-ratio: 3/4;
}
}
@media (orientation: landscape) {
.image-hero {
aspect-ratio: 16/9;
}
}
/* Hover capability detection */
@media (hover: hover) and (pointer: fine) {
/* Desktop with mouse */
.button:hover {
transform: scale(1.05);
}
}
@media (hover: none) and (pointer: coarse) {
/* Touch devices */
.button:active {
transform: scale(0.95);
}
}
/* Prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* Prefers color scheme */
@media (prefers-color-scheme: dark) {
:root {
--bg-color: #1a1a1a;
--text-color: #f0f0f0;
}
}
/* Combining media queries */
@media (min-width: 768px) and (max-width: 1023px) {
/* Tablet only styles */
.container {
padding: 2rem;
}
}
/* Print styles */
@media print {
.no-print {
display: none;
}
body {
font-size: 12pt;
color: black;
background: white;
}
}
```
### Container Queries
Modern alternative to viewport-based media queries:
```css
/* Define container */
.card-container {
container-type: inline-size;
container-name: card;
}
/* Query based on container size */
.card {
display: flex;
flex-direction: column;
gap: 1rem;
}
@container card (min-width: 400px) {
.card {
flex-direction: row;
align-items: center;
}
.card-image {
width: 40%;
}
.card-content {
width: 60%;
}
}
@container card (min-width: 600px) {
.card-title {
font-size: 1.5rem;
}
}
/* Multiple containers */
.sidebar {
container-type: inline-size;
container-name: sidebar;
}
.main-content {
container-type: inline-size;
container-name: main;
}
@container sidebar (min-width: 300px) {
.widget {
padding: 1.5rem;
}
}
@container main (min-width: 600px) {
.article {
column-count: 2;
}
}
```
## Responsive Images
### Srcset and Sizes
Provide different image sizes for different viewport widths:
```html
<!-- Responsive image with srcset -->
<img
src="image-800.jpg"
srcset="
image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w,
image-1600.jpg 1600w
"
sizes="
(max-width: 480px) 100vw,
(max-width: 768px) 90vw,
(max-width: 1024px) 700px,
1000px
"
alt="ReRelated in frontend
angular-development
IncludedComprehensive Angular framework development covering components, directives, services, dependency injection, routing, and reactive programming based on official Angular documentation
javascript-fundamentals
IncludedCore JavaScript language features, patterns, and best practices including ES6+ syntax, async/await, closures, prototypes, and modern development patterns
nextjs-development
IncludedComprehensive Next.js development skill covering App Router, Server Components, data fetching, routing patterns, API routes, middleware, and full-stack Next.js applications
react-development
IncludedComprehensive React development with hooks, components, state management, context, effects, and performance optimization based on official React documentation
vuejs-development
IncludedComprehensive Vue.js development skill covering Composition API, reactivity system, components, directives, and modern Vue 3 patterns based on official Vue.js documentation
frontend-architecture
IncludedComponent architecture, design patterns, state management strategies, module systems, build tools, and scalable application structure