Responsive Design and Media Queries
Add the viewport meta tag in your HTML head, use flexible widths, and apply media queries to change the layout on small or large screens. A media query looks like @media (min-width: 600px) { ... } and applies its rules only when the condition is true.
Design mobile first: write the small screen styles first, then add min-width queries for bigger screens. You can also query orientation, hover capability and user preferences.
Example
<style>
.cols { display: grid; gap: 10px; }
.cols div { background: #fecaca; padding: 20px; }
@media (min-width: 500px) {
.cols { grid-template-columns: 1fr 1fr 1fr; }
.cols div { background: #bbf7d0; }
}
</style>
<div class="cols"><div>One</div><div>Two</div><div>Three</div></div>
<p>Resize the preview: stacked and red when narrow, three columns and green when wide.</p>- Add <meta name="viewport" content="width=device-width, initial-scale=1"> to your HTML head.
Exercise
Apply the style only on screens at least 600px wide.
@ (min-width: 600px) { body { font-size: 20px; } }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.