


CSS media queries: apply different styles for different devices and screen sizes
CSS media queries are a very useful technique in web design that allow us to apply different styles according to different devices and screen sizes. Through media queries, we can adjust the layout and appearance of the web page based on the type of device used by the user, such as computer, tablet or mobile phone, as well as factors such as screen width and height, to provide a better user experience.
Before using CSS media queries, we need to understand some basic concepts and usage. First of all, we need to make it clear that media queries are a CSS3 function, so the browser is required to support CSS3 to work properly.
Media queries are defined using the @media rule and are usually written at the top of the CSS file so that they are loaded first. The syntax of media query is as follows:
@media mediatype and (condition) {
CSS style
}
Among them, mediatype represents the media type. Common media types are as follows Several types:
- all: suitable for all media devices;
- screen: suitable for computer screens;
- print: suitable for printers and print previews;
- handheld: Suitable for handheld devices such as mobile phones and tablets.
The condition part is the core of the media query, and the conditions are used to filter out the devices or screen sizes that meet the conditions. Conditions can include the following commonly used attributes:
- width: screen width;
- height: screen height;
- device-width: device width;
- device-height: device height;
- orientation: screen orientation;
- aspect-ratio: screen aspect ratio;
- resolution: screen resolution.
Here are some specific code examples showing how to use media queries to adapt to different devices and screen sizes:
- Apply specific styles for large screen devices:
@media screen and (min-width: 1200px) {
/ Style applied when the screen width is greater than or equal to 1200px/
body {
font-size: 18px;
}
}
- Apply specific styles for small screen devices:
@media screen and (max-width: 767px) {
/ Styles applied when the screen width is less than or equal to 767px/
body {
font-size: 14px;
}
}
- Apply specific styles for horizontal screen display:
@media screen and (orientation: landscape) {
/ Style applied when displayed in landscape orientation/
body {
background-color: yellow;
}
}
- Use media queries in combination with multiple conditions:
@media screen and (min-width: 768px) and (max-width: 1199px) {
/ Styles applied when screen width is between 768px and 1199px/
body {
font-size: 16px;
}
}
Pass the above As an example, we can see that media queries can apply different styles according to different devices and screen sizes, thereby achieving responsive design of web pages. By flexible use of media queries, we can provide users with different devices with a better user experience, and achieve good interface display effects whether on computers, tablets or mobile phones.
Of course, media queries are only part of responsive design, and other technologies and practices need to be combined to complete a complete responsive web design. In practical applications, we can select appropriate media query conditions based on project needs and user groups, and write corresponding CSS styles to achieve the best responsive design.
The above is the detailed content of CSS media queries: apply different styles for different devices and screen sizes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics



The Redmi K70 Extreme Edition is about to be released. The parameter configuration of the Redmi K70 Extreme Edition has basically been exposed. Not only is it facing a price increase, but all aspects have been comprehensively upgraded, including the screen, appearance, photography, etc. of the Redmi K70 Extreme Edition. every aspect. And it is reported that the screen size of the Redmi K70 Extreme Edition will also be adjusted, come and find out. Redmi K70 Extreme Edition screen size: 6.67-inch Redmi K70 Extreme Edition will use a 6.67-inch 1.5K direct screen. The height of the phone is approximately 162.78 mm, the width is approximately 75.44 mm, and the thickness is approximately 8.7 mm. K70 Extreme Edition screen parameters detailed screen supplier: Huaxing Optoelectronics Screen refresh rate: Supports 144Hz e-sports refresh rate In addition, the k70 Extreme Edition screen uses Huaxing Optoelectronics

CSS framework skills to quickly implement responsive design: To make your web page appear perfectly on different devices, specific code examples are required. With the widespread popularity of mobile devices, responsive design of web pages has become an important requirement for modern web development. To make web pages appear perfectly on different devices, an important tool is the CSS framework. The CSS framework provides us with a set of optimized codes to enable adaptive adjustments of web pages on different devices. This article will introduce some CSS framework techniques to quickly implement responsive design, and provide specific code.

How to use CSS properties to create responsive layouts With the popularity of mobile devices and the rise of multiple terminals, responsive layouts have attracted more and more attention from developers. By using CSS properties, we can easily implement responsive layout so that web pages can achieve good display effects on different terminals. This article explains how to use CSS properties to create responsive layouts and provides some concrete code examples. 1. Media queries Media queries are one of the most commonly used methods to implement responsive layout. By using media queries, we can base the

As mobile phones continue to develop, mobile phone screens are becoming larger and larger. In fact, for many people, the size of the mobile phone screen has exceeded the range of one-handed control. When using the mobile phone, everyone has to operate it with two hands, which brings inconvenience to everyone's daily use experience. So as a newly released new phone, what is the screen size of Huawei Enjoy 70z? What is the screen size of Huawei Enjoy 70z? The screen size is 6.75 inches. Huawei Enjoy 70z has a 6.75-inch LCD water drop screen with a resolution of up to 1600×720, and the visual effect is clear and comfortable. With a thin and light body of 8.98mm and a weight of 199g, it feels great in the hand. Three stylish colors: Magic Night Black, Galaxy Blue, and Snowy White will make you stand out among many mobile phones! Although there is no

CSS media queries are a very useful technique in web design that allow us to apply different styles based on different devices and screen sizes. Through media queries, we can adjust the layout and appearance of the web page based on the type of device used by the user, such as computer, tablet or mobile phone, as well as factors such as screen width and height, to provide a better user experience. Before using CSS media queries, we need to understand some basic concepts and usage. First of all, we need to make it clear that media queries are a CSS3 function, so browsers are required to support

What units should a responsive layout use to adapt to different screen sizes? In today's era of ubiquitous mobile devices, web developers are faced with an important problem: how to make web pages render well on different screen sizes. In order to solve this problem, responsive layout (ResponsiveDesign) came into being. Responsive design is a web design method that automatically adapts to different screen sizes and resolutions. It can automatically adjust the layout and layout of web pages according to the screen size and orientation of the device, so that it can

How to use CSSViewport units to adjust font size according to screen size CSSViewport unit is a unit relative to the viewport size, which can help us dynamically adjust font size according to screen size. In the era of mobile devices, this technology can help us solve the problem of too large or too small fonts caused by diverse screen sizes. This article will introduce how to use CSSViewport units to adjust the font size according to the screen size, and provide some specific code

Tips on how to use CSSViewport units to adjust the position of elements according to screen size In web development, we often encounter the need to adjust the position and size of elements according to different screen sizes. To achieve this goal, CSSViewport unit is widely used. Viewport units are units relative to the size of the browser window. By using it, we can dynamically adjust the position of elements based on the screen size, thereby providing a better user experience. 1. Understanding Vi
