Media queries are a key feature of responsive design. They allow you to apply different CSS styles depending on the device’s screen size, orientation, resolution, or other features. This ensures your website looks great on desktops, tablets, and mobile phones.
@media media-type and (condition) {
/* CSS rules go here */
}
/* Default styles for desktop */
body {
font-size: 18px;
background-color: lightgray;
}
/* Styles for screens 600px wide or smaller */
@media screen and (max-width: 600px) {
body {
font-size: 16px;
background-color: lightblue;
}
}
@media screen and (min-width: 601px) and (max-width: 1024px) {
.container {
width: 90%;
padding: 15px;
}
}
/* Landscape mode */
@media screen and (orientation: landscape) {
body {
background-color: lightgreen;
}
}
/* Portrait mode */
@media screen and (orientation: portrait) {
body {
background-color: lightpink;
}
}