Skip to main content

Command Palette

Search for a command to run...

Understanding Viewports and Media Queries

How to use CSS to detect and respond to different screen sizes.

Updated
3 min read
Understanding Viewports and Media Queries
P

I am a Frontend developer with 3+ years of professional experience in the React.js, Next.js, and Typescript ecosystem. I have developed a strong skillset in writing beautiful and functional user interfaces. My expertise also extends creating visually appealing designs using raw CSS and frameworks like TailwindCSS.

In the previous article, we discussed the importance of responsive design and how it enables websites to adapt to various devices and screen sizes. In this article, we'll dive deeper into the technical aspects of responsive design, including viewports and media queries.

Viewports

A viewport is the user's visible area of a web page. It's the portion of the page that's currently visible to the user on their device. Depending on the device being used, the size and orientation of the viewport will vary. It's essential to consider the viewport when designing a responsive website as it determines how the content will be displayed.

By default, most mobile devices use a viewport width that's much narrower than a desktop's screen width. Therefore, if we don't account for this difference, our website will not be responsive on mobile devices.

To overcome this problem, we need to use the viewport meta tag. The viewport meta tag allows us to control the width and scale of the viewport, which, in turn, affects the layout of the website.

Here's an example of how the viewport meta tag can be used:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

The above code sets the width of the viewport to the device width, which means that the website will adjust to the width of the device's screen. The initial-scale property sets the initial zoom level when the page is first loaded.

Media Queries

Media queries are an essential aspect of responsive design. They allow us to apply different styles based on the user's device, screen size, and orientation. Media queries work by testing for certain conditions, and if the condition is met, the CSS inside the media query is applied.

Here's an example of a media query:

@media screen and (max-width: 768px) {
   /* Styles applied when screen width is less than or equal to 768px */
}

In the example above, we're using a media query to apply specific styles when the screen width is less than or equal to 768px. Media queries can also be used to apply styles to specific devices, such as smartphones, tablets, and desktops.

It's important to note that media queries are not supported in older browsers, such as Internet Explorer 8 and earlier versions. Therefore, it's essential to have a fallback plan, such as using polyfills or providing a separate layout for older browsers.

Conclusion

Understanding viewports and media queries is critical for designing responsive websites that work well on various devices and screen sizes. By using the viewport meta tag and media queries, we can create websites that adapt to the user's device, ensuring an optimal user experience.

What's next?

In the coming article, we will be covering CSS Grids and how to make responsive layouts with them. Don't forget to follow and share this with your friends that are just starting their web development journey

CSS for beginners

Part 16 of 26

CSS basics for beginners is designed to provide you with a comprehensive understanding of CSS and how it works. It's perfect for newbies or those looking to refresh their skills.

Up next

Creating Responsive Layouts with CSS Grid

How to use CSS Grid to create responsive grids that adapt to different screen sizes.

More from this blog

C

Codeskills blog

33 posts

Learn web development with Html, Css, and JavaScript on my beginner-friendly blog by Paul Ehikhuemen. Whether you're a complete newbie or just need a refresher, Get started today!