</> Code Corner

CSS · CSS Advanced · Lesson 58

CSS Media Queries

Apply styles for different devices and sizes.

Media queries apply CSS only when a condition is true, such as a minimum or maximum viewport width, orientation or color scheme.

Example

<style>
body { background: pink; font-family: Arial; }
@media screen and (min-width: 480px) {
  body { background: lightgreen; }
}
@media (max-width: 400px) {
  .menu { display: none; }
}
@media (prefers-color-scheme: dark) {
  .card { background: #222; color: #eee; }
}
.card { padding: 10px; border: 1px solid #ccc; }
</style>
<p class="menu">Menu (hidden under 400px)</p>
<div class="card">Background turns green at 480px and wider.</div>
Tip: Mobile-first design writes base styles for phones and adds min-width queries for bigger screens.

Keep reading “CSS Media Queries”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋