CSS Responsive Design

CSS Media Queries

CSS Media Queries are used to apply different styles based on device type, screen size, resolution, or orientation.

CSS Media Queries are used to apply different styles based on the device type, screen size, resolution, or orientation.

Media Queries are the foundation of Responsive Web Design (RWD) because they allow a website to adapt to different devices such as:

  • Mobile phones
  • Tablets
  • Laptops
  • Desktop computers
  • Large screens

Instead of creating separate websites for different devices, Media Queries allow a single website to respond dynamically to various screen sizes.

Syntax

Media Query Syntax

css

@media condition {
    selector {
        property: value;
    }
}

Media Query Example

css

@media (max-width: 768px) {
    body {
        background-color: lightblue;
    }
}

Changes the background color when the screen width is 768px or smaller.

Attributes

FeatureDescriptionExample
max-widthApplies styles up to a specified widthmax-width: 768px
min-widthApplies styles from a specified width and abovemin-width: 768px
orientationDetects portrait or landscape modeorientation: landscape
max-heightApplies styles up to a specified heightmax-height: 600px
min-heightApplies styles from a specified height and abovemin-height: 600px

Example

CSS Media Queries Complete Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Media Queries Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: white;
        }
        .container {
            width: 80%;
            margin: auto;
            padding: 20px;
            background-color: lightgray;
        }
        @media (max-width: 768px) {
            .container {
                width: 100%;
                background-color: lightblue;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        Resize the browser window to see the effect.
    </div>
</body>
</html>

Output

Browser Output

css

On large screens:
Container width = 80%
Background = Light Gray

On screens 768px or smaller:
Container width = 100%
Background = Light Blue

The layout automatically adapts to smaller screens

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE
YesYesYesYesYesYes

Notes

Mobile Screen Example

Mobile Screen Example

css

@media (max-width: 480px) {
    body {
        font-size: 14px;
    }
}

Applies styles only on small mobile devices.

Tablet Screen Example

Tablet Screen Example

css

@media (min-width: 768px) {
    body {
        font-size: 16px;
    }
}

Applies styles for tablets and larger devices.

Landscape Mode Example

Landscape Mode Example

css

@media (orientation: landscape) {
    body {
        background-color: lightgreen;
    }
}

Applies styles when the device is in landscape orientation.

Common Breakpoints

DeviceWidth
MobileUp to 480px
Tablet481px - 768px
Small Laptop769px - 1024px
Desktop1025px and above

Conclusion

CSS Media Queries are essential for building responsive websites that adapt to different screen sizes and devices. By using conditions such as max-width, min-width, and orientation, you can create layouts that provide an optimal user experience across mobile phones, tablets, laptops, and desktops.