HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

CSS Tutorial

CSS Introduction

What is CSSCSS SyntaxHow to Add CSSCSS Comments

CSS Foundations

Cascade & InheritanceCSS SpecificityCSS UnitsBox SizingCSS VariablesCSS Math Functions

CSS Selectors

CSS Element SelectorCSS Class SelectorCSS ID SelectorCSS Universal SelectorCSS Group SelectorCSS CombinatorsCSS Attribute SelectorsCSS Pseudo ClassesCSS Pseudo Elements

CSS Colors and Backgrounds

CSS ColorsCSS HEX ColorsCSS RGB ColorsModern ColorsCSS Background ColorCSS Background ImageLayered Backgrounds

CSS Text & Fonts

CSS Text ColorCSS Text AlignmentCSS Text DecorationCSS Font FamilyCSS Font SizeWeb Fonts & TypographyText Wrapping

CSS Box Model

CSS MarginCSS PaddingCSS BorderCSS OutlineCSS Width and Height

CSS Layout

Normal FlowCSS DisplayCSS PositionAnchor PositioningCSS FloatCSS OverflowCSS Z-indexIntrinsic SizingAspect Ratio & Object FitMulti-column Layout

CSS Flexbox

Flex ContainerFlex DirectionJustify ContentAlign ItemsFlex WrapFlex Item Sizing

CSS Grid

CSS Grid ContainerCSS Grid ColumnsCSS Grid RowsCSS Grid GapCSS Grid Layout ExampleGrid PlacementGrid AreasCSS Subgrid

CSS Lists, Tables & Forms

CSS ListsCSS TablesCSS Form StylingCSS Input Fields

CSS Effects

CSS ShadowsCSS GradientsCSS FiltersCSS OpacityClipping & MaskingBlending & BackdropScroll Snap

CSS Animations & Transitions

CSS TransformCSS 2D TransformCSS 3D TransformCSS TransitionsEntry & Exit TransitionsCSS AnimationsScroll AnimationsView TransitionsMotion Paths

CSS Responsive Design

CSS Media QueriesResponsive LayoutMobile First DesignContainer QueriesPreference Media QueriesFeature Queries

Modern CSS Authoring

Cascade LayersNesting & ScopeLogical PropertiesGenerated Content

CSS Accessibility

Accessible CSSCSS ThemingModern Form StylingCSS User Interface

CSS Output & Performance

Print StylesCSS PerformanceCSS ArchitectureDebugging CSS

CSS Reference

CSS At-rulesCSS FunctionsCSS Tutorial PDF

CSS Projects

Responsive CSS Project
  1. Home
  2. /
  3. Tutorials
  4. /
  5. CSS Tutorial
  6. /
  7. CSS Media Queries

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: The supporting concepts are explained in Responsive Layout and Mobile First Design.

  • 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

Feature
Chrome
Edge
Firefox
Safari
@media11213

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

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.

PreviousMotion PathsNextResponsive Layout