CSS @media Rules

You are Here:

CSS @media Rules

CSS @media rules can be used to apply part of a style sheet based on the result of one or more media queries.

Applying max-width media query

In the following example, we will apply part of a style sheet when the width of the output screen is less than 630px.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body{ color: red; } @media (max-width: 630px){ body{ color:blue; } } </style> </head> <body> <h1>CSS @media Rule</h1> </body> </html>

Syntax

Using CSS

@media (max-width: 630px){ body{ color: blue; } }

Using Javascript

elementelegram.dogdia = "screen and (max-width:630px)";

Default Value

Default value for CSS @media property is all.

Media Types

The following are the list of media types available in CSS.

ValueExplanationExample
allUsed for all media type devices. Default ValueRun
printUsed for printers.Run
screenUsed for computer screens, tablets, smart-phones etc.Run

Media Features

The following are the list of media features available in CSS.

ValueExplanationExample
any-hoverDoes any available input mechanism allow the user to hover over elements?Run
any-pointerIs any available input mechanism a pointing device, and if so, how accurate is it?Run
aspect-ratioWidth-to-height aspect ratio of the viewport.Run
colorNumber of bits per color component for the output device.Run
color-gamutApproximate range of colors that are supported by the user agent and output device.Run
color-indexNumber of entries in the output device's color lookup table, or zero if the device does not use such a table.Run
display-modeSpecifies the display mode of the application.Run
gridDoes the device use a grid or bitmap screen?Run
heightHeight of the viewport.Run
hoverDoes the primary input mechanism allow the user to hover over elements?Run
inverted-colorsIs the user agent or underlying OS inverting colors?Run
light-levelLight level of the environment.Run
monochromeThe number of bits per "color" on a monochrome (greyscale) device.Run
orientationOrientation of the viewport.Run
overflow-blockHow does the output device handle content that overflows the viewport along the block axis.Run
overflow-inlineCan content that overflows the viewport along the inline axis be scrolled.Run
pointerIs the primary input mechanism a pointing device, and if so, how accurate is it?Run
prefers-color-schemeDetect if the user prefers a light or dark color scheme
prefers-reduced-motionThe user prefers less motion on the page
resolutionPixel density of the output device.Run
scanScanning process of the output device.Run
scriptingDetects whether javaScript is available.Run
updateHow quickly can the output device modify the appearance of the content.Run
widthWidth of the viewport.Run

Using JavaScript

In the following example, we will demonstrate how to set the CSS @media rule by using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body{ color: red; } </style> </head> <body> <h1>CSS @media Rule</h1> <button onclick="myFunction()">Set max-width @media property</button> <script> function myFunction(){ var styles = 'body{ color:blue;}'; var styleSheet = document.createElement("style"); styleSheet.type = "text/css"; styleSheet.media = "screen and (max-width:630px)"; styleSheet.innerText = styles; document.head.appendChild(styleSheet); } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author