Course
Media Queries
CSS Tutorial
This CSS tutorial is designed for beginners to navigate through the essentials and intricate aspects of CSS styling. Upon finishing this tutorial, participants will possess a comprehensive understanding of CSS, setting a solid foundation for further exploration and mastery. This guide aims to equip you with the skills necessary to transform your visions into visually appealing web designs, laying the groundwork for your journey towards becoming an accomplished web designer.
Media Queries
CSS media queries allow to apply CSS styles to a webpage based on the characteristics of the device or browser viewing the page. With media queries, we can create designs that adapt to different screen sizes, resolutions, and orientations.
Media queries are used for the following:
- To target specific media for the <style>, <link>, <source> and other HTML components using media= attribute.
- To test media states and monitor them using Window.matchMedia() and EventTarget.addEventListener() methods.
Syntax
@media [media-type] and ([media-feature]) { /* CSS rules to apply when the media query conditions are met */}
Here, media-type can be things like screen, print, speech, etc., and media-feature can be characteristics such as width, height, aspect ratio, orientation, and more.
Media Types
Media types are used in CSS media queries to apply different styles based on device. The most common media types are all, print, and screen. If you don't specify a media type, it matches all devices. But, if you use the not or only logical operators, you must specify a media type.
- all − Default Value. This value applies to all media types.
- print − This value is only valid when printing the document.
- screen − This value is only valid for screens, such as computers, tablets, and smartphones.
JavaScript provides a CSS object model interface called CSSMediaRule, which can be used to access the rules created using the @media CSS at-rule.
Logical Operators
Media queries can be combined using logical operators such as listed in the below table:
You can't use the not keyword to negate a single condition in a media query, it can only be applied to an entire media query.
CSS Media Type - All
The following example demonstrates the use of a CSS media query with the media type all
<html><head><style> @media all { h3 { color: red; } p { background-color: yellow; color: blue; } }</style></head><body> <h3>Tutorialspoint</h3> <p>CSS Media Type - All</p></body></html>
CSS Media Type - Print
Following example demonstrates use of a CSS media query with the media type print. When webpage viewed on a screen the text color will be red. When you print it, the text will be blue.
<html><head><style> h3,p { color: red; } @media print { h3,p { color: blue; } } button { background-color: violet; padding: 5px; }</style></head><body> <h3>Tutorialspoint</h3> <p>CSS Media Type - Print</p> <p>Click on below button to see the effect when you print the page.</p>
<button onclick="printPage()">Print Page</button> <script> function printPage() { window.print(); } </script></body></html>
CSS Media Type - Screen
The following example demonstrates that if the screen size is greater than 500px, the background color changes to pink and text color changes to blue
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> h2,h4 { background-color: red; } @media screen and (min-width: 500px) { h2,h4 { background-color: pink; color: blue; } }</style></head><body> <p>Resize the browser window to see the effect.</p> <h2>Tutorialspoint</h2> <h4>CSS Media Type - Screen</h4></body></html>
CSS Media Type - With Comma
The following example demonstrates using media types with comma separation. The background color changes to yellow and text color changes to blue when in print mode and when screen size is > 500px
<html><head><style> body { color: red } @media screen and (min-width: 500px), print { body { background-color: yellow; color: blue; } } button { background-color: violet; padding: 5px; }</style></head><body> <h1>CSS Media Type - Screen and Print</h1> <p>Resize the window to less than 500px to see the background and font color changes to default.</p> <p>Click on below button to see the effect when you print the page.</p>
<button onclick="printPage()">Print Page</button> <script> function printPage() { window.print(); } </script></body></html>
CSS Media Type - With Only
The following example demonstrates that when the screen width is between 500px and 700px, the background color changes to pink and text color changes to blue
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> body { color: red; } @media only screen and (min-width: 500px) and (max-width: 700px) { body { color: blue; background-color: pink; } }</style></head><body> <h1>Tutorialspoint</h1> <p>CSS Media Type - Screen</p> <p>Resize the browser window to see the effect.</p></body></html>
CSS Media Queries - Range
The short and easy new range syntax for media query for testing various features that involve a range.
@media (width > 700px) { /* Styles for screens that are at least 700px wide */}
The following example demonstrates that when the screen width is between 600px and 800px, the background color changes to yellow and text color changes to red
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> p { color: blue; } @media (600px < width < 800px) { p { background-color: yellow; color: red; } }</style></head><body> <h1>Tutorialspoint</h1> <p>CSS Media Type - Screen</p> <p>Resize the browser size to see the effect.</p></body></html>
Media Features
CSS media feature is used to apply different styles to the webpage based on the a specific characteristic, output device, or environment.
Following is the list of CSS properties related to media features:
Creating Complex Media Query
You can create complex media queries by combining multiple conditions using the and, not, and only operators. You can also combine multiple media queries into a comma-separated list.
The and operator is used to group multiple conditions together. The not operator negates a condition. The only operator is used to prevent older browsers from applying the styles.
If you don't mention a media type, all media type is used by default. However, when you use the not or only operators, you need to specify a media type.
CSS Media Queries - Combining Multiple Types or Features
The and keyword joins a media feature with either a media type or other media features to create more specific media queries.
The following example demonstrates that when the screen width is greater tahn 500px and the device is in landscape orientation, the text color changes to blue
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> body { color: red; } @media (min-width: 500px) and (orientation: landscape) { body { color: blue; } }</style></head><body> <h3>Resize the browser window to see the effect.</h3> <p>The text color changed to blue when the viewport width is at least 500px and the device is in landscape orientation.</p></body></html>
The following example demonstrates that when the device is a screen media type, the screen width is greater than 600px and the device is in landscape orientation, the background color is changes to yellow
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> body { background-color: red; } @media screen and (min-width: 600px) and (orientation: landscape) { body { background-color: yellow; } }</style></head><body> <h3>Resize the browser window to see the effect.</h3> <p>The background color of the body element will change to yellow only for screen media type devices that have at least 600px wide and in landscape orientation.</p></body></html>
CSS Media Queries - Testing For Multiple Queries
The following example demonstrates that when the viewport height is greater than 700px and the device is a screen with portrait orientation, the text color changes to red
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style> body { color: green; } @media (min-height: 700px), screen and (orientation: portrait) { body { color: red; } }</style></head><body> <h3>Resize the browser window to see the effect.</h3> <h1>Testing For Multiple Queries</h1></body></html>
CSS Media Queries - Inverting a Query's Meaning
CSS not keyword reverse the logic of a media query, so it only applies when the media query conditions are not met. It can only be used to negate entire media queries, not individual feature queries.
The following example demonstrates that the media query@media not all and (monochrome) reverses its meaning. If the device does not support a monochrome (black and white) display, the text color changes to blue otherwise the default text color is black.
<html><head><style> h1 { color: black; } @media not all and (monochrome) { h1 { color: blue; } }</style></head><body> <h1>Inverting a Query's Meaning</h1></body></html>
The not keyword is evaluated last in a media query, so the above media query would be evaluated as follows:
@media not (all and (monochrome)) { /* … */}
It's not evaluated like this:
@media (not all) and (monochrome) { /* … */}
CSS Media Queries - Improving Compatibility With Older Browsers
CSS media queries only keyword is used to make sure that older browsers that do not support media queries with media features to ignore the given styles. It doesn't impact modern browsers:
@media only screen and (color) { /* … */}