CSS Media Queries (original) (raw)

Last Updated : 21 Jan, 2026

CSS Media Queries enable web pages to adjust their layout and styles based on different screen sizes, devices, or orientations. They are essential for building responsive and adaptable web designs.

<style>
    .gfg {
        color: black;
    }
    @media screen and (max-width: 500px) {
        .gfg {
            color: green;
        }
    }
</style>
Sample Example of Media Query

`

**Syntax:

@media mediatype and (condition) { /* CSS styles */}

**Note: It is compulsory to add the viewport meta tag for media queries to work correctly. Learn more about it here.

Media types specify which devices the styles should apply to. Commonly used types include:

Media Type Description
all Suitable for all media devices.
print Used for printers.
screen Targeted at computer screens, tablets, smartphones, etc.
speech Designed for screen readers that read the content aloud.

Media Queries for Multiple Screen Sizes

HTML `

<style>
    .gfg {
        color: black;
    }
    @media screen and (max-width: 800px) {
        .gfg {
            color: blue;
        }
    }
    @media screen and (max-width: 500px) {
        .gfg {
            color: green;
        }
    }
</style>
Sample Example of Media Query

`

Media Queries for Multiple Screen Sizes with Additional Styles

HTML `

<style>
    .gfg {
        color: black;
        font-size: 20px;
        padding: 10px;
    }
    @media screen and (max-width: 800px) {
        .gfg {
            color: blue;
            font-size: 18px;
        }
    }
    @media screen and (max-width: 500px) {
        .gfg {
            color: green;
            font-size: 16px;
            text-align: center;
        }
    }
</style>
Sample Example of Media Query

`

Media queries allow developers to check various device characteristics. Here are some important features:

**Feature **Description
color Specifies the number of bits per color component for the device.
grid Checks whether the device is grid or bitmap.
height Represents the height of the viewport.
aspect ratio Defines the width-to-height ratio of the viewport.
color-index Indicates how many colors the device can display.
max-resolution The highest resolution the device can achieve, measured in dpi or dpcm.
monochrome Shows the number of bits per color on a monochrome device.
scan Refers to the method of scanning used by the output device.
update Describes how fast the device can update its display.
width Represents the width of the viewport.