CSS Media Queries are a technique in CSS that allows a webpage layout to adapt to different screen sizes, resolutions, and device types. They are a core feature of Responsive Web Design.
@media (condition) {
/* CSS rules that apply only under this condition */
}
1. Adjusting for different screen widths:
/* For screens with a maximum width of 600px (e.g., smartphones) */
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
2. Detecting landscape vs. portrait orientation:
@media (orientation: landscape) {
body {
background-color: lightgreen;
}
}
3. Styling for print output:
@media print {
body {
font-size: 12pt;
color: black;
background: none;
}
}
✅ Mobile-first design: Optimizing websites for small screens first and then expanding for larger screens.
✅ Dark mode: Adjusting styles based on user preference (prefers-color-scheme
).
✅ Retina displays: Using high-resolution images or specific styles for high pixel density screens (min-resolution: 2dppx
).
Responsive Design is a web design approach that allows a website to automatically adjust to different screen sizes and devices. This ensures a seamless user experience across desktops, tablets, and smartphones without needing separate versions of the site.
Responsive Design is achieved using the following techniques:
1. Flexible Layouts
2. Media Queries (CSS)
@media (max-width: 768px) {
body {
background-color: lightgray;
}
}
→ This changes the background color for screens smaller than 768px.
3. Flexible Images and Media
img {
max-width: 100%;
height: auto;
}
4. Mobile-First Approach
✅ Better user experience across all devices
✅ SEO advantages, as Google prioritizes mobile-friendly sites
✅ No need for separate mobile and desktop versions, reducing maintenance
✅ Higher conversion rates, since users can navigate the site easily
Responsive Design is now the standard in modern web development, ensuring optimal display and usability on all devices.