HTML & CSS · Beginner · 15 min
Adapt a layout to screen width
Use a layout for small screens first and change it with a media query.
Start with one column
A responsive layout adapts to available space. A layout with one column is a useful starting point for small screens. Use flexible widths instead of forcing the whole page to a fixed desktop width. In a standalone HTML document, include the viewport <meta> tag shown here; the preview already provides it.
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.cards { display: grid; gap: 16px; grid-template-columns: 1fr; }
</style>
<div class="cards"><article>HTML</article><article>CSS</article></div>Expected appearance
grid-template-columns: 1fr
Add space when it is available
A min-width media query applies once the viewport reaches the specified width. This example changes to two equal grid columns at 600px. Choose a breakpoint based on the content’s needs. It is not a rule that every device has a particular width.
<style>
.cards { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 600px) {
.cards { grid-template-columns: 1fr 1fr; }
}
</style>
<div class="cards"><article>HTML</article><article>CSS</article></div>Expected appearance
320px: 1 768px: 2
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Build .cards with two article children. Use one grid column at 320px and two equal columns at 768px, with gap: 16px. Add the second column at a 600px breakpoint.
Use a default 1fr track, then override it with 1fr 1fr inside @media (min-width: 600px).
Show worked solution
<style>
.cards { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .cards { grid-template-columns: 1fr 1fr; } }
</style>
<div class="cards"><article>HTML</article><article>CSS</article></div>Use a default 1fr track, then override it with 1fr 1fr inside @media (min-width: 600px).
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Use both preview widths and observe the layout.
- Check a width just below and just above the breakpoint by resizing the browser.