Media Queries
Media queries apply styles conditionally based on device characteristics like screen width, resolution, or orientation.
Demo: Responsive Box (resize to see changes)
HTML
<div class="responsive-box">
Resize the output window to see me change!
</div> CSS
.responsive-box {
background: #6366f1;
color: white;
padding: 20px;
border-radius: 8px;
font-weight: bold;
text-align: center;
transition: 0.3s;
}
@media (max-width: 400px) {
.responsive-box {
background: #dc2626;
font-size: 0.875rem;
padding: 12px;
}
}
@media (min-width: 600px) {
.responsive-box {
background: #059669;
font-size: 1.25rem;
}
} Live Output Window
Common Breakpoints
/* Mobile first */
@media (min-width: 640px) { /* tablet */ }
@media (min-width: 1024px) { /* desktop */ }
@media (min-width: 1280px) { /* wide */ }
Media Query Syntax
@media (condition) {
/* styles */
}
/* Multiple conditions */
@media (min-width: 768px) and (max-width: 1024px) { }
/* OR condition */
@media (max-width: 600px), (orientation: landscape) { }
Media Features
| Feature | Example |
|---|---|
width / min-width / max-width | Viewport width |
height / min-height / max-height | Viewport height |
orientation | portrait or landscape |
prefers-color-scheme | light or dark |
prefers-reduced-motion | Accessibility preference |
resolution | Screen resolution |
Demo: Dark Mode Example
HTML
<div class="theme-demo">
Check with prefers-color-scheme: dark
</div> CSS
.theme-demo {
background: #ffffff;
color: #1e293b;
padding: 20px;
border-radius: 8px;
border: 2px solid #cbd5e1;
font-weight: bold;
text-align: center;
}
@media (prefers-color-scheme: dark) {
.theme-demo {
background: #1e293b;
color: #e2e8f0;
border-color: #475569;
}
} Live Output Window