
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
Understanding CSS Media Types and Queries
CSS Media Types and Queries help the user to define certain styles in accordance with a device’s general type (screen, print, etc.) or its characteristics (screen resolution, viewport dimensions, etc.)
Syntax
Following is the syntax for media queries −
@media not | only mediatype and (expressions) { CSS-Code; }
Here, media query is applied if −
- mediatype matches the device type on which document is rendered.
- not/only operators are not defined then media query applies to all mediatypes.
- not/only operators are specified then media query is not/only applied to certain mediatype respectively.
A user can have different stylesheets each corresponding to a different media. For example, a user can have a different stylesheet for print and screen media.
<link rel="stylesheet" media="print and|not|only (expressions)" href="print.css">
and,
<link rel="stylesheet" media="screen and|not|only (expressions)" href="screen.css">
Following are the media types listed in CSS3 −
Sr.No | Value & Description |
---|---|
1 | all Stylesheet applies to all media type devices |
2 | print Stylesheet applies to printers |
3 | screen Stylesheet applies to computer screens, tablets, smart-phones etc. |
4 | speech Stylesheet applies to screen readers that "reads" the page out loud |
Following are the methods to create media dependent stylesheets −
- Using @media At-rules
- Using @import At-rules
- Using HTML <link> element with media attribute
Example
Let’s see an example for CSS media query −
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box; } .col { float: left; width: 20%; padding: 40px; } body { background-color: lemonchiffon; margin: 20px; } @media screen and (max-width: 850px) { .col { width: 50%; } body { background-color: mediumseagreen; } } @media screen and (max-width: 550px) { .col { width: 100%; } body { background-color: powderblue; } } </style> </head> <body> <div class="row"> <div class="col" style="background-color:#373;"> </div> <div class="col" style="background-color:#363;"> </div> <div class="col" style="background-color:#353;"> </div> <div class="col" style="background-color:#343;"> </div> <div class="col" style="background-color:#333;"> </div> </div></body></html>
Output
This will produce the following output −
When screen size is above 850px −
When screen size is between 550px and 850px −
When screen size is below 550px −
Example
Let’s see another example for CSS media query: HTML Document −
<!DOCTYPE html> <html> <head> <style type="text/css"> @import url(screen.css) screen; @import url(print.css) print; </style> </head> <body> <p> Vivamus commodo, dolor eu porttitor sagittis, orci nisl consectetur ipsum, vel volutpat nibh lectus at erat. Cras scelerisque faucibus tellus aliquam commodo.Donec sem urna, facilisis at ipsum id, viverra sollicitudin est. Nam rhoncus sollicitudin lorem, a accumsan purus varius eget. </p> <p class="two">In ultrices lectus nisi. Nulla varius ex ut tortor congue viverra. Sed sodales vehicula leo, vitae interdum elit vehicula nec. Donec turpis nunc, iaculis et nisi sit amet, feugiat lacinia metus. </p> <p>Suspendisse eget ligula et risus lobortis ornare id at elit. Suspendisse potenti. Vivamus pellentesque eleifend pellentesque. Vestibulum neque ante, iaculis a sagittis et, fermentum at metus.</p> </body> </html>
CSS document (screen.css): p { color: navy; font-style: italic; } .two { color: #c303c3; font-size: 20px; } body { background-color: honeydew;} CSS document (print.css): p { color: red; font-style: italic;} .two { color: #989898; font-size: 40px; }
Output
This will produce the following output −
When document is visible in a screen mediatype −
When document is visible in a print mediatype −
- Related Articles
- CSS media types
- Responsive Web Design with Media Queries in CSS
- Different Media Types in CSS
- Create a responsive navigation menu with CSS Media Queries
- Understanding Media
- How to create a printable webpage using CSS media queries?
- How to use media queries for common device breakpoints with CSS?
- Media queries with CSS3
- Set Media Queries for different CSS style rules for different size devices
- Understanding CSS Selector and Declarations
- Media Bias: Meaning and Types
- Culture Media: Types and Uses
- How to use media queries with JavaScript?
- Understanding CSS Absolute and Relative Units
- Understanding CSS Units
