DEV Community

Raj
Raj

Posted on

what's difference between the meta viewport and @media screen min-width

meta viewport and media screen are different things. They both help with responsive design, but they work at different levels.

1. Meta viewport

This is written inside the HTML :

It tells the browser how to set up the webpage's viewport on mobile devices.
width=device-width → make the webpage width equal to the device's screen width.
initial-scale=1.0 → start at normal zoom (100%).
"How should the browser display my webpage on the device?"

2. @media screen

This is CSS:
@media screen and (max-width: 600px) {
body {
background: lightblue;
}
}
It tells CSS:

"If the screen width is 600px or less, apply these styles."

"When the screen has this size, change my design."

3. min-width / max-width
For example:
@media screen and (min-width: 768px) {
.box {
width: 500px;
}
}
Here:
min-width: 768px → apply CSS when screen width is 768px or greater.
max-width: 600px → apply CSS when screen width is 600px or smaller.

Easy difference

Simple example:

<style>
    @media screen and (max-width: 600px) {
        .box {
            width: 100%;
        }
    }
</style>
Enter fullscreen mode Exit fullscreen mode


Remember this :

Meta viewport controls how the browser displays the page on the device, while a media query checks the screen size and applies different CSS styles.
So, width=device-width is NOT the same as @media screen and (min-width). One controls the viewport setup; the other controls CSS styling based on conditions.

Top comments (0)