🎨

Responsive Design

Beginner
80 XP
30 min
Lesson Content

Responsive Design

Responsive design ensures your website looks great on all devices - from mobile phones to desktop computers. Use media queries and flexible units to create adaptive layouts.

Media Queries

Media queries allow you to apply CSS rules based on screen size:

@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

Common Breakpoints

  • Mobile: max-width: 600px
  • Tablet: 601px - 1024px
  • Desktop: min-width: 1025px

Flexible Units

  • %: Percentage of parent
  • vw/vh: Viewport width/height (1vw = 1% of viewport width)
  • rem/em: Relative to font size
  • max-width: Limits maximum size

Example

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

@media (max-width: 768px) {
  .container {
    padding: 10px;
  }
}
Example Code

Make a layout that adapts to different screen sizes

<!DOCTYPE html>
<html><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
  width: 400px;
  background: #fff;
  padding: 20px;
  margin: 20px auto;
  border: 2px solid #e5e7eb;
}

@media (max-width: 600px) {
  .box {
    width: 100%;
    background: #f8fafc;
    padding: 15px;
    margin: 10px;
  }
}
</style>
</head><body>
<div class="box">Responsive box - resizes on small screens</div>
</body></html>

Expected Output:

Responsive box - resizes on small screens
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code