Home Web Front-end CSS Tutorial CSS media queries: apply different styles for different devices and screen sizes

CSS media queries: apply different styles for different devices and screen sizes

Nov 18, 2023 pm 05:28 PM
Equipment adaptation screen size css media queries

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:

  1. 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;
Copy after login

}
}

  1. 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;
Copy after login

}
}

  1. Apply specific styles for horizontal screen display:

@media screen and (orientation: landscape) {
/ Style applied when displayed in landscape orientation/
body {

  background-color: yellow;
Copy after login

}
}

  1. 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;
Copy after login

}
}

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Redmi K70 Extreme Edition screen size Redmi K70 Extreme Edition screen size Jul 02, 2024 am 11:13 AM

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 tips for achieving perfect responsive design: quickly adapt your web pages to different devices CSS framework tips for achieving perfect responsive design: quickly adapt your web pages to different devices Jan 16, 2024 am 09:43 AM

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 create responsive layout using CSS properties How to create responsive layout using CSS properties Nov 18, 2023 pm 12:25 PM

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

What is the screen size of Huawei Enjoy 70z? What is the screen size of Huawei Enjoy 70z? Mar 18, 2024 pm 10:58 PM

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: apply different styles for different devices and screen sizes CSS media queries: apply different styles for different devices and screen sizes Nov 18, 2023 pm 05:28 PM

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

Choosing what units to adapt to different screen sizes is a best practice for responsive layouts Choosing what units to adapt to different screen sizes is a best practice for responsive layouts Jan 27, 2024 am 09:59 AM

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 CSS Viewport units to adjust font size based on screen size How to use CSS Viewport units to adjust font size based on screen size Sep 13, 2023 am 08:57 AM

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

How to use CSS Viewport units to adjust element position based on screen size How to use CSS Viewport units to adjust element position based on screen size Sep 13, 2023 pm 01:18 PM

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

See all articles