-
Notifications
You must be signed in to change notification settings - Fork 0
CSS3 Media Queries
Media queries in CSS3 allow you to apply different styles depending on the properties of the device or viewport. For example, you can adjust the layout of your page for small screens like mobile phones and larger screens like desktop computers.
You can think of media queries as a way to "ask" the browser, "What kind of device is this?" and then apply different styles accordingly.
The syntax for a media query is:
@media (condition) {
/* Styles to apply when the condition is true */
}Here, @media is followed by a condition (like max-width, min-width, etc.). If the condition matches the deviceβs characteristics, the styles inside the curly braces {} are applied.
@media (max-width: 600px) {
/* Styles for screens smaller than 600px wide */
body {
background-color: lightblue;
}
h1 {
font-size: 18px;
}
}-
max-width: 600px: This means the styles inside the media query will apply to devices with a screen width of 600px or less (typically mobile devices). - The background color of the page is changed to lightblue and the font size of
h1is set to 18px.
You can also combine multiple conditions using and or or.
@media (min-width: 601px) and (max-width: 1024px) {
/* Styles for devices with a width between 601px and 1024px */
body {
background-color: lightgreen;
}
h1 {
font-size: 24px;
}
}-
min-width: 601pxandmax-width: 1024px: This media query applies styles to devices with screen widths between 601px and 1024px (typically tablets and small laptops). - The background color changes to lightgreen, and the font size of
h1is set to 24px.
Here are some of the most common conditions you can use in media queries:
-
max-width: Targets devices with a screen width less than or equal to a specific value (e.g., 600px). -
min-width: Targets devices with a screen width greater than or equal to a specific value. -
max-height: Targets devices with a screen height less than or equal to a specific value. -
min-height: Targets devices with a screen height greater than or equal to a specific value. -
orientation: Targets devices based on their orientation, either landscape (wide) or portrait (tall). -
resolution: Targets devices with specific screen resolutions (like retina displays).
You can use media queries to apply different styles depending on whether the device is in landscape or portrait mode.
/* Portrait mode */
@media (orientation: portrait) {
body {
background-color: lightyellow;
}
}
/* Landscape mode */
@media (orientation: landscape) {
body {
background-color: lightcoral;
}
}-
orientation: portraitapplies styles when the device is in portrait mode (tall). -
orientation: landscapeapplies styles when the device is in landscape mode (wide). - The background color changes depending on the orientation of the device.
Retina displays have higher pixel density, and you can target them with the resolution condition in media queries. Devices with higher pixel density will show sharper images, so you might want to use higher-quality images.
@media only screen and (min-resolution: 2dppx) {
img {
width: 150%;
}
}-
min-resolution: 2dppx: This targets devices with a screen resolution of 2dppx (dots per pixel), which includes high-definition displays like retina screens. - The image size is increased to 150% to make use of the higher resolution.
For a fully responsive design, you can use multiple media queries to adjust the layout for different screen sizes and orientations.
/* Default Styles for Large Screens (Desktops) */
body {
font-size: 16px;
background-color: white;
}
h1 {
font-size: 36px;
}
/* Styles for Tablets (Between 601px and 1024px) */
@media (min-width: 601px) and (max-width: 1024px) {
body {
background-color: lightgray;
}
h1 {
font-size: 28px;
}
}
/* Styles for Small Screens (Mobile Devices) */
@media (max-width: 600px) {
body {
background-color: lightblue;
}
h1 {
font-size: 20px;
}
}- Default Styles: These styles apply to devices with a large screen, such as desktops.
-
Tablet Styles: For devices with a screen width between 601px and 1024px (tablets), the background color changes to lightgray, and the font size of
h1reduces to 28px. -
Mobile Styles: For devices with a screen width of 600px or less (mobile phones), the background color changes to lightblue, and the font size of
h1further reduces to 20px.
You can also specify the media type, such as screen, print, or speech. This is useful when you want to apply styles specifically for different output devices.
@media print {
body {
font-size: 12pt;
}
h1 {
font-size: 20pt;
}
}-
@media print: This media query applies styles only when the page is being printed. - The font size of
bodyandh1is adjusted for printing purposes.