Objective: Create flexible, mobile-friendly layouts that adapt to any screen using fluid units, flexible media, and media queries.
Responsive design uses flexible grids, images, and media queries so pages adapt from desktop to mobile. Mobile-friendly sites rank better on Google.
em, rem, vw, vh, fr.| Unit | Description | Example |
|---|---|---|
| % | Relative to parent | width:80%; |
| em | Relative to current font size | margin:2em; |
| rem | Relative to root font size | font-size:1.2rem; |
| vw/vh | Viewport width/height | height:100vh; |
.container{
width:90%;
margin:0 auto;
}img{
max-width:100%;
height:auto;
display:block;
}
@media (max-width:768px){
body{ background-color:#f0f0f0; }
}| Device | Max Width |
|---|---|
| Large desktops | 1200px |
| Laptops | 992px |
| Tablets | 768px |
| Mobile phones | 576px |
Mobile-first alternative: @media (min-width:769px){ ... }
/* Base (mobile) */
body{ font-size:16px; }
/* Tablets */
@media (min-width:768px){
body{ font-size:18px; }
}
/* Desktops */
@media (min-width:1024px){
body{ font-size:20px; }
}Smaller devices load simpler styles first.
<section class="gallery">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
<div class="item">4</div> <div class="item">5</div> <div class="item">6</div>
</section>
/* CSS */
.gallery{ display:grid; grid-template-columns:1fr; gap:10px; padding:20px; }
.item{ background:#0E477B; color:#fff; text-align:center; padding:30px 0; border-radius:8px; }
/* Tablet */ @media (min-width:768px){ .gallery{ grid-template-columns:repeat(2,1fr); } }
/* Desktop */ @media (min-width:1024px){ .gallery{ grid-template-columns:repeat(3,1fr); } }<header class="nav">
<div class="logo">StreetGeek</div>
<nav>
<a href="#">Home</a> <a href="#">Courses</a> <a href="#">Blog</a> <a href="#">Contact</a>
</nav>
</header>
.nav{ display:flex; justify-content:space-between; align-items:center; background:#0E477B; color:#fff; padding:10px 20px; }
.nav nav a{ color:#fff; text-decoration:none; margin-left:15px; }
/* Mobile */
@media (max-width:600px){
.nav{ flex-direction:column; }
.nav nav a{ display:block; margin:5px 0; }
}max-width:100% on an image ensure?Create a Responsive 3-Section Homepage using:
@media at 2+ breakpoints*{ transition: all .3s ease; }Next up: π MODULE 10: Final Project β Responsive Portfolio β bring structure, style, layout, and responsiveness together.