How do media queries function in responsive web design?
Jul 22, 2025 am 01:05 AMMedia queries apply specific CSS styles by detecting device characteristics, thereby achieving responsive layouts. Its working principle is to check the conditions of the browser or device (such as screen width, height, orientation and resolution) and apply corresponding style rules, such as increasing the font size when the screen width is less than 768px; common conditions include min-width/max-width, min-height/max-height, orientation and resolution; multiple conditions can be combined with and, not, or commas. Best practices for using media queries include adopting a mobile-first strategy, selecting breakpoints based on design requirements, and using relative units to improve adaptability.
Media queries are a core part of making websites work well on different screen sizes. They let you apply CSS styles based on things like the width or height of the browser, the device's orientation, or even its resolution. In short, media queries help your site look good whether someone is viewing it on a phone, tablet, or desktop.

How do media queries actually work?
At their most basic, media queries check certain conditions in the browser or device and apply styles only if those conditions are met. For example, you can say “if the screen is narrower than 768px, make the font bigger.” This helps tailor the layout for different devices without changing the HTML.
Here's what a simple media query looks like:

@media (max-width: 768px) { body { font-size: 18px; } }
This rule will only take effect when the browser window is 768 pixels wide or narrower .
A few common conditions used in media queries include:

-
min-width
/max-width
-
min-height
/max-height
-
orientation
(portrait or landscape) -
resolution
(like high-density screens)
You can also combine multiple conditions using keywords like and
, not
, or even stack them with commas to cover more scenarios.
When should you use media queries?
The main reason people use media queries is to create responsive layouts — that is, websites that adjust themselves depending on the screen size.
For example:
- On mobile, you might want to stack content vertically and hide the navigation menu behind a hamburger icon.
- On tablets, you might display two columns instead of one.
- On desktops, you can bring back the full navigation and wider layouts.
You don't have to write separate pages for each device — just layer your styles using media queries and let the browser pick what fits best.
One important note: It's usually best to start with a mobile-first approach , meaning your base styles are for small screens, and then you add media queries to enhance the layout for larger screens. This keeps things simpler and faster for mobile users.
Common breakpoints and how to choose them
There's no single correct set of breakpoints, but some common ones are:
- Mobile : up to 767px
- Tablet portrait : 768px – 991px
- Tablet landscape/small desktop : 992px – 1199px
- Desktop : 1200px and above
But instead of blindly following these numbers, a better approach is to test your design as you resize the browser and see where things start to break or look awkward. That's when you add a media query to fix it.
Another tip: Use relative units like em
, rem
, or %
inside media queries when possible, because they adapt better across devices and user settings.
Final thoughts
Media queries give you the power to control how your website looks on different screens without needing completely separate versions of the site. They're flexible, widely supported, and work right inside your CSS.
Just remember — responsive design isn't just about screen size. It's also about touch interactions, performance, and accessibility. Media queries are a big piece of the puzzle, but not the whole picture.
Basically that's it.
The above is the detailed content of How do media queries function in responsive web design?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to implement flexible layout and responsive design through vue and Element-plus. In modern web development, flexible layout and responsive design have become a trend. Flexible layout allows page elements to automatically adjust their size and position according to different screen sizes, while responsive design ensures that the page displays well on different devices and provides a good user experience. This article will introduce how to implement flexible layout and responsive design through vue and Element-plus. To begin our work, we

React Responsive Design Guide: How to Achieve Adaptive Front-end Layout Effects With the popularity of mobile devices and the increasing user demand for multi-screen experiences, responsive design has become one of the important considerations in modern front-end development. React, as one of the most popular front-end frameworks at present, provides a wealth of tools and components to help developers achieve adaptive layout effects. This article will share some guidelines and tips on implementing responsive design using React, and provide specific code examples for reference. Fle using React

How to use CSSFlex elastic layout to implement responsive design. In today's era of widespread mobile devices, responsive design has become an important task in front-end development. Among them, using CSSFlex elastic layout has become one of the popular choices for implementing responsive design. CSSFlex elastic layout has strong scalability and adaptability, and can quickly implement screen layouts of different sizes. This article will introduce how to use CSSFlex elastic layout to implement responsive design, and give specific code examples.

How to use PHP to implement mobile adaptation and responsive design Mobile adaptation and responsive design are important practices in modern website development. They can ensure good display effects of the website on different devices. In this article, we will introduce how to use PHP to implement mobile adaptation and responsive design, with code examples. 1. Understand the concepts of mobile adaptation and responsive design Mobile adaptation refers to providing different styles and layouts for different devices based on the different characteristics and sizes of the device. Responsive design refers to the use of

Vue is a very excellent front-end development framework. It adopts the MVVM mode and achieves a very good responsive layout through two-way binding of data. In our front-end development, responsive layout is a very important part, because it allows our pages to display the best effects for different devices, thereby improving user experience. In this article, we will introduce how to use Vue to implement responsive layout and provide specific code examples. 1. Use Bootstrap to implement responsive layout. Bootstrap is a

How to use Layui to develop a responsive web page layout design. In today's Internet era, more and more websites need to have good layout design to provide a better user experience. As a simple, easy-to-use, and flexible front-end framework, Layui can help developers quickly build beautiful and responsive web pages. This article will introduce how to use Layui to develop a simple responsive web layout design, and attach detailed code examples. Introducing Layui First, introduce Layui related files in the HTML file

CSSViewport: How to use vh, vw, vmin and vmax units to implement responsive design, specific code examples required In modern responsive web design, we usually want web pages to adapt to different screen sizes and devices to provide a good user experience. The CSSViewport unit (viewport unit) is one of the important tools to help us achieve this goal. In this article, we’ll cover how to use vh, vw, vmin, and vmax units to achieve responsive design.

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency
