📱 Responsive Design

Mobile-First Approach

Responsive Web Design

Create websites that work beautifully on all devices. Responsive design adapts layouts to different screen sizes and capabilities.

💻 Core Concepts

/* Three pillars of responsive design */
/* 1. Fluid grids (flexible layouts) */
/* 2. Flexible images (scale with container) */
/* 3. Media queries (adapt to screen size) */

/* Viewport meta tag (essential) */
<meta name="viewport" content="width=device-width, initial-scale=1.0">

📐 Fluid Layouts

Relative Units

/* Percentage-based widths */
.container {
    width: 90%;          /* Relative to parent */
    max-width: 1200px;   /* Maximum width */
    margin: 0 auto;      /* Center */
}

.column {
    width: 33.333%;      /* One third */
    float: left;         /* Legacy */
}

/* Viewport units */
.hero {
    width: 100vw;        /* 100% of viewport width */
    height: 100vh;       /* 100% of viewport height */
}

.sidebar {
    width: 25vw;         /* 25% of viewport width */
}

/* Relative typography */
.text {
    font-size: 16px;     /* Base size */
    font-size: 1rem;     /* Relative to root */
    font-size: 1.5em;    /* Relative to parent */
    font-size: 4vw;      /* Relative to viewport */
}

Flexbox for Fluid Layouts

/* Flexible containers */
.row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}

.column {
    flex: 1 1 300px;     /* Grow, shrink, min-width */
}

/* Responsive navigation */
nav {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
}

nav .menu {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
}

Grid for Fluid Layouts

/* Responsive grid without media queries */
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

/* Cards automatically adjust to screen size */
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 30px;
}

🖼️ Flexible Images

/* Responsive images */
img {
    max-width: 100%;     /* Never exceed container */
    height: auto;        /* Maintain aspect ratio */
    display: block;      /* Remove inline spacing */
}

/* Background images */
.hero {
    background-image: url('image.jpg');
    background-size: cover;      /* Cover entire area */
    background-position: center; /* Center image */
}

/* Object-fit for images */
.thumbnail {
    width: 100%;
    height: 200px;
    object-fit: cover;   /* Cover area, crop if needed */
}

.contain-image {
    width: 100%;
    height: 300px;
    object-fit: contain; /* Fit inside, no crop */
}

/* Picture element for art direction */
<picture>
    <source media="(min-width: 1024px)" srcset="large.jpg">
    <source media="(min-width: 768px)" srcset="medium.jpg">
    <img src="small.jpg" alt="Description">
</picture>

/* Responsive video */
.video-container {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    height: 0;
    overflow: hidden;
}

.video-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

📏 Breakpoints

/* Common breakpoints */
/* Mobile: < 576px */
/* Tablet: 576px - 768px */
/* Desktop: 768px - 1024px */
/* Large: 1024px - 1440px */
/* XL: > 1440px */

/* Mobile-first approach (recommended) */
/* Base styles for mobile */
.container {
    padding: 15px;
}

/* Tablet and up */
@media (min-width: 768px) {
    .container {
        padding: 30px;
    }
}

/* Desktop and up */
@media (min-width: 1024px) {
    .container {
        padding: 50px;
        max-width: 1200px;
        margin: 0 auto;
    }
}

/* Desktop-first approach (legacy) */
/* Base styles for desktop */
.sidebar {
    width: 25%;
    float: left;
}

/* Tablet */
@media (max-width: 1024px) {
    .sidebar {
        width: 30%;
    }
}

/* Mobile */
@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        float: none;
    }
}

🎯 Mobile-First Strategy

/* Start with mobile styles */
.navigation {
    display: flex;
    flex-direction: column;
}

.menu {
    display: none;  /* Hidden on mobile */
}

.menu-toggle {
    display: block;
}

/* Expand for tablets */
@media (min-width: 768px) {
    .navigation {
        flex-direction: row;
        justify-content: space-between;
    }
    
    .menu {
        display: flex;
        gap: 2rem;
    }
    
    .menu-toggle {
        display: none;
    }
}

/* Enhance for desktop */
@media (min-width: 1024px) {
    .navigation {
        padding: 0 50px;
    }
    
    .menu {
        gap: 3rem;
    }
}

📱 Touch-Friendly Design

/* Minimum touch target size: 44x44px */
.button {
    min-width: 44px;
    min-height: 44px;
    padding: 12px 24px;
}

/* Adequate spacing between touch targets */
.button-group {
    display: flex;
    gap: 12px;  /* Minimum 8px */
}

/* Larger text for mobile */
body {
    font-size: 16px;  /* Never below 16px on mobile */
}

@media (min-width: 768px) {
    body {
        font-size: 18px;
    }
}

/* Hover states only on devices that support hover */
@media (hover: hover) {
    .button:hover {
        background: #0056b3;
    }
}

/* Touch devices */
@media (hover: none) {
    .button:active {
        background: #0056b3;
    }
}

🎨 Content Strategies

Hide/Show Content

/* Hide on mobile */
.desktop-only {
    display: none;
}

@media (min-width: 768px) {
    .desktop-only {
        display: block;
    }
}

/* Hide on desktop */
.mobile-only {
    display: block;
}

@media (min-width: 768px) {
    .mobile-only {
        display: none;
    }
}

/* Better approach: Use different layouts */
.sidebar {
    order: 2;  /* After content on mobile */
}

@media (min-width: 768px) {
    .sidebar {
        order: 1;  /* Before content on desktop */
    }
}

Responsive Typography

/* Fluid typography */
h1 {
    font-size: 2rem;     /* Mobile */
}

@media (min-width: 768px) {
    h1 {
        font-size: 3rem; /* Tablet */
    }
}

@media (min-width: 1024px) {
    h1 {
        font-size: 4rem; /* Desktop */
    }
}

/* Clamp for fluid scaling */
h1 {
    font-size: clamp(2rem, 5vw, 4rem);
    /* min: 2rem, preferred: 5vw, max: 4rem */
}

body {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    line-height: 1.6;
}

/* Responsive line length */
.text-content {
    max-width: 65ch;  /* 65 characters max */
}

🎯 Practical Examples

/* Responsive navigation */
nav {
    padding: 1rem;
}

.nav-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav-menu {
    display: none;
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    background: white;
    flex-direction: column;
    padding: 1rem;
}

.nav-menu.active {
    display: flex;
}

.menu-toggle {
    display: block;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
}

@media (min-width: 768px) {
    .nav-menu {
        display: flex;
        position: static;
        flex-direction: row;
        gap: 2rem;
        padding: 0;
    }
    
    .menu-toggle {
        display: none;
    }
}

/* Responsive card grid */
.cards {
    display: grid;
    grid-template-columns: 1fr;  /* 1 column on mobile */
    gap: 20px;
    padding: 20px;
}

@media (min-width: 576px) {
    .cards {
        grid-template-columns: repeat(2, 1fr);  /* 2 columns */
    }
}

@media (min-width: 1024px) {
    .cards {
        grid-template-columns: repeat(3, 1fr);  /* 3 columns */
        gap: 30px;
    }
}

@media (min-width: 1440px) {
    .cards {
        grid-template-columns: repeat(4, 1fr);  /* 4 columns */
    }
}

/* Responsive hero section */
.hero {
    padding: 40px 20px;
    text-align: center;
}

.hero h1 {
    font-size: 2rem;
    margin-bottom: 1rem;
}

.hero p {
    font-size: 1rem;
    margin-bottom: 2rem;
}

@media (min-width: 768px) {
    .hero {
        padding: 80px 40px;
    }
    
    .hero h1 {
        font-size: 3.5rem;
    }
    
    .hero p {
        font-size: 1.25rem;
    }
}

@media (min-width: 1024px) {
    .hero {
        padding: 120px 60px;
    }
    
    .hero h1 {
        font-size: 4.5rem;
    }
}

/* Responsive form */
.form {
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
    padding: 20px;
}

@media (min-width: 768px) {
    .form {
        grid-template-columns: repeat(2, 1fr);
        gap: 20px;
    }
    
    .form .full-width {
        grid-column: 1 / -1;
    }
}

/* Responsive table */
table {
    width: 100%;
    border-collapse: collapse;
}

/* Stack table on mobile */
@media (max-width: 767px) {
    table, thead, tbody, th, td, tr {
        display: block;
    }
    
    thead tr {
        display: none;
    }
    
    tr {
        margin-bottom: 15px;
        border: 1px solid #ddd;
    }
    
    td {
        position: relative;
        padding-left: 50%;
        text-align: right;
    }
    
    td::before {
        content: attr(data-label);
        position: absolute;
        left: 10px;
        font-weight: bold;
        text-align: left;
    }
}

/* Responsive sidebar layout */
.layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
}

@media (min-width: 1024px) {
    .layout {
        grid-template-columns: 300px 1fr;
    }
    
    .sidebar {
        position: sticky;
        top: 20px;
        height: fit-content;
    }
}

🎯 Key Takeaways