Open In App

CSS @media Rule

The @media CSS at-rule is used to apply a different set of styles for different media/devices using the Media Queries. A Media Query is mainly used to check the height, width, resolution, and orientation(Portrait/Landscape) of the device. This CSS rule is a way out for making more out of responsive design by delivering a more optimized design for a specific screen type or device i.e. smartphone, or PC. The media queries can also be used for specifying certain styles only for printed documents or for screen readers.

Syntax:



@media not|only mediatype and (media feature and|or|not mediafeature) 
{
    // CSS Property
}

Used Keywords:

Media Types:



Media Features: There are many media features in Media Query some of them are listed below:

Example: This example illustrates the use of the @media rule to implement the various styling properties based on the result of one or more media queries. The @media rule will only work if the media query matches the device on which the content is being used.




<!DOCTYPE html>
<html>
<head>
    <title> CSS @media rule </title>
    <style>
        @media screen and (max-width: 600px) {
 
            h1,
            h2 {
                color: green;
                font-size: 25px;
            }
 
            p {
                background-color: green;
                color: white;
            }
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>CSS @media rule</h2>
    <p>GeeksforGeeks: A computer science portal</p>
</body>
</html>

Output: From the output, we can see when screen width resizes less than or equal to 600px then the text color also changes to green.

Supported Browsers: The browser supported by the @media rule are listed below:


Article Tags :