Skip to content

CSS Responsive Design

Overview

Responsive design ensures your website looks good on all devices and screen sizes.

Media Queries

Basic Media Query

css
/* Default styles for mobile */
.container {
    width: 100%;
    padding: 0 15px;
}

/* Tablet styles */
@media (min-width: 768px) {
    .container {
        width: 750px;
        margin: 0 auto;
    }
}

/* Desktop styles */
@media (min-width: 1024px) {
    .container {
        width: 970px;
    }
}

/* Large desktop */
@media (min-width: 1200px) {
    .container {
        width: 1170px;
    }
}

Multiple Media Query Conditions

css
@media (min-width: 768px) and (max-width: 1023px) {
    /* Tablet-specific styles */
    .sidebar {
        float: left;
        width: 250px;
    }
    
    .main-content {
        margin-left: 270px;
    }
}

Responsive Units

Relative Units

css
/* em - relative to parent font size */
.text-element {
    font-size: 1.2em;
    margin: 0.5em 0;
}

/* rem - relative to root font size */
.heading {
    font-size: 2.5rem;
    margin-bottom: 1rem;
}

/* % - relative to parent */
.container {
    width: 90%;
    max-width: 1200px;
    margin: 0 auto;
}

Viewport Units

css
/* vw - viewport width */
.full-width {
    width: 100vw;
}

.half-width {
    width: 50vw;
}

/* vh - viewport height */
.full-height {
    height: 100vh;
}

.hero-section {
    height: 80vh;
    min-height: 400px;
}

/* vmin/vmax - smaller/larger of viewport dimensions */
.square {
    width: 50vmin;
    height: 50vmin;
}

Responsive Images

css
.responsive-image {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Responsive background images */
.hero-image {
    background-image: url('hero.jpg');
    background-size: cover;
    background-position: center;
    height: 60vh;
}

Responsive Typography

css
/* Fluid typography */
html {
    font-size: 16px;
}

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

@media (min-width: 1024px) {
    html {
        font-size: 20px;
    }
}

/* Using clamp() for responsive sizing */
.heading {
    font-size: clamp(1.5rem, 4vw, 2.5rem);
}

Mobile-First Approach

css
/* Base styles (mobile) */
.navigation {
    display: flex;
    flex-direction: column;
    padding: 1rem;
}

/* Tablet and up */
@media (min-width: 768px) {
    .navigation {
        flex-direction: row;
        justify-content: space-between;
        padding: 1rem 2rem;
    }
}

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

Responsive Grid

css
.grid-container {
    display: grid;
    gap: 20px;
    grid-template-columns: 1fr; /* Mobile: 1 column */
}

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

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

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

Responsive Flexbox

css
.flex-container {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
}

.flex-item {
    flex: 1 1 300px; /* Grow, shrink, basis */
}

@media (min-width: 768px) {
    .flex-item {
        flex: 1 1 200px; /* Smaller basis on tablet */
    }
}

Common Patterns

Responsive Navigation

css
.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
}

.nav-toggle {
    display: block; /* Show on mobile */
}

@media (min-width: 768px) {
    .nav-toggle {
        display: none; /* Hide on desktop */
    }
    
    .nav-menu {
        display: flex; /* Show on desktop */
    }
}

Responsive Cards

css
.card-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
}

@media (min-width: 768px) {
    .card-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .card-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

.card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    padding: 1.5rem;
}

Content is for learning and research only.