Media queries in CSS allow you to apply styles based on the characteristics of the device or viewport. They play a crucial role in creating responsive and adaptive designs for various screen sizes and devices.
Media queries are written using the '@media'
rule, followed by the media type and optional conditions.
/* Basic Media Query Syntax */
@media screen and (max-width: 600px) {
/* Styles applied when the screen width is 600 pixels or less */
body {
font-size: 14px;
}
}
Common media types include 'all'
, 'screen'
, 'print'
, 'speech'
, etc.
/* Media Query with Media Type */
@media print {
/* Styles applied when printing */
body {
color: #000;
}
}
Use media queries to create breakpoints for responsive design.
/* Responsive Design with Media Queries */
/* Default styles for larger screens */
body {
font-size: 16px;
}
/* Styles for smaller screens */
@media screen and (max-width: 768px) {
body {
font-size: 14px;
}
}
Combine conditions using logical operators ('and'
, 'or'
) for more precise targeting.
/* Combining Conditions in Media Queries */
@media screen and (min-width: 768px) and (max-width: 1024px) {
/* Styles for screens between 768px and 1024px */
body {
line-height: 1.5;
}
}
Adjust styles based on device orientation ('portrait'
or 'landscape'
).
/* Media Query for Device Orientation */
@media screen and (orientation: landscape) {
/* Styles applied when the device is in landscape orientation */
body {
background-color: #f0f0f0;
}
}
Use media queries to provide styles for high-resolution (Retina) displays.
/* Media Query for Retina Displays */
@media only screen and (min-resolution: 192dpi) {
/* Styles for high-resolution displays */
img {
max-width: 100%;
height: auto;
}
}
Adjust styles based on the resolution of the device’s screen.
/* Resolution Media Query */
@media screen and (min-resolution: 2dppx) {
/* Styles for high-resolution screens (Retina) */
img {
max-width: 100%;
}
}
Tailor styles based on the aspect ratio of the viewport.
/* Aspect Ratio Media Query */
@media screen and (min-aspect-ratio: 16/9) {
/* Styles for wide-screen aspect ratio */
video {
width: 100%;
}
}
Adopt a mobile-first approach by writing styles for smaller screens first.
/* Mobile-First Media Query */
body {
font-size: 16px;
}
@media screen and (min-width: 768px) {
/* Styles for larger screens */
body {
font-size: 18px;
}
}
Test media queries on various devices and browsers for consistent behavior.
Be mindful of overlapping conditions to prevent unintended styles.
Implement a mobile-first typography approach for better readability.
/* Mobile-First Typography with Media Queries */
body {
font-size: 16px;
@media screen and (min-width: 768px) {
font-size: 18px;
}
@media screen and (min-width: 1200px) {
font-size: 20px;
}
}
Use feature queries to check for browser capabilities before applying styles.
/* Feature Query for Flexbox Support */
@media (display: flex) {
/* Styles applied only if the browser supports flexbox */
.container {
display: flex;
justify-content: space-between;
}
}
Use media queries to hide or show elements based on screen size.
/* Hiding/Showing Elements with Media Queries */
@media screen and (max-width: 768px) {
/* Hide a navigation menu on smaller screens */
nav {
display: none;
}
}
Adjust font sizes for better readability on different devices.
/* Adaptive Typography with Media Queries */
body {
font-size: 16px;
}
@media screen and (min-width: 768px) {
/* Increase font size for larger screens */
body {
font-size: 18px;
}
}
Implement complex grid layouts with media queries for different screen sizes.
/* Advanced Grid Layouts with Media Queries */
.container {
display: grid;
grid-template-columns: 1fr;
@media screen and (min-width: 768px) {
grid-template-columns: repeat(2, 1fr);
}
@media screen and (min-width: 1200px) {
grid-template-columns: repeat(3, 1fr);
}
}
Make images responsive by adjusting their size based on screen width.
/* Responsive Images with Media Queries */
img {
max-width: 100%;
@media screen and (min-width: 768px) {
max-width: 50%;
}
@media screen and (min-width: 1200px) {
max-width: 33.33%;
}
}
Create styles specifically for print layouts.
/* Print Styles with Media Query */
@media print {
/* Styles for printed documents */
body {
color: #000;
}
}
Combine multiple media features for precise targeting.
/* Combining Media Features */
@media screen and (min-width: 768px) and (orientation: landscape) {
/* Styles for landscape orientation on screens wider than 768px */
body {
background-color: #f0f0f0;
}
}
Media queries are an integral part of modern web design, enabling developers to create layouts that adapt to various devices and screen sizes. From basic syntax to advanced techniques, understanding media queries allows you to build responsive and user-friendly websites. Happy Coding! ❤️