
- 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
The @media At-rules in CSS
The CSS @media rule is used to specify different styles for different media types (such as print, screen, all, etc.) in a single style sheet. It is usually followed by a comma-separated list of media types and the CSS declarations block containing the styles rules for target media.
Syntax
Following is the syntax −
@media not | only mediatype and (expressions) { CSS-Code; }
Example
Let’s see an example of CSS @media rule −
<!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 rule −
<!DOCTYPE html> <html> <head> <style type="text/css"> p { background-origin: content-box; background-repeat: no-repeat; background-size: cover; box-shadow: 0 0 3px black; padding: 20px; background-origin: border-box; } @media screen and (max-width: 900px) { p{ background: url("https://www.tutorialspoint.com/swing/images/swing.jpg"); color: #c303c3; } } @media screen and (max-width: 500px) { p { color: black; background: url("https://www.tutorialspoint.com/cplusplus/images/cplusplus.jpg"); } } </style> </head> <body> <p>This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. </p> </body> </html>
Output
This will produce the following output −
When screen size is above 500px −
When screen size is below 500px −
- Related Articles
- The @import At-rules in CSS
- How to specify the target media types of a set of CSS rules
- Set Media Queries for different CSS style rules for different size devices
- Style Rules in CSS
- Some CSS Rules
- Paged Media in CSS
- CSS media types
- CSS @media rule
- Different Media Types in CSS
- Aural Media with CSS
- Aural Media CSS Properties
- Rules to override Style Sheet Rule in CSS
- CSS rest Speech Media property
- Responsive Web Design with Media Queries in CSS
- CSS voice-volume Speech Media property

Advertisements