Table of Contents
Media Queries
Home Web Front-end Front-end Q&A What is the html5 media query statement?

What is the html5 media query statement?

Jan 28, 2023 am 10:05 AM
html5 Media inquiries

html5 media query statement is composed of a media type and one or more conditional expressions for detecting media characteristics; the media characteristics that can be used for detection in media queries include width, height, color, etc.; using media queries, you can Customize the display effect for specific output devices without changing the page content.

What is the html5 media query statement?

The operating environment of this tutorial: Windows 10 system, HTML5 version, DELL G3 computer

What is the html5 media query statement?

HTML5 Media Queries

Media Queries

- What are media queries?

Media queries allow us to set CSS styles for the device display based on its characteristics (such as viewport width, screen ratio, device orientation: landscape or portrait). Media queries consist of the media type and one or more detections Media properties are composed of conditional expressions. Media properties that can be detected in media queries are width , height , and color (etc.). Using media queries, you can customize the display effect for specific output devices without changing the page content.

- Applicability of media queries and flexible box layout

Media queries: It is best to use media queries when the structure of the page changes. Flexible box: If only the width and height change, try to use the flexible box

-Usage method

<!-- link元素中的CSS媒体查询 -->
<link rel="stylesheet" media="(min-width: 800px)" href="example.css" />

<!-- 样式表中的CSS媒体查询 -->
<style>
@media (max-width: 600px) {
  .class {
    display: none;
  }
}
</style>
Copy after login

@media media type and (media characteristics) {your style}

To use Media Queries, you must start with "@media", then specify the media type (also called device type), and then specify the media characteristics (also called device characteristics). The writing method of media properties is very similar to the writing method of styles. It is mainly divided into two parts. The first part refers to the media properties, and the second part is the value specified by the media properties, and a colon is used between the two parts. Separate. For example:

(max-width: 480px)
Copy after login

Different from CSS attributes, media properties use min/max to express greater than equal to or less than as a logical judgment, instead of using less than (<) and greater than (>). judge by symbols.

-Media Type

all ##All media (default)
screen Color screen
print Print Preview
-Media Properties

##(can add max min prefix)device-width(can add max min prefix)orientation

- 最大宽度max-width

“max-width”是媒体特性中最常用的一个特性,其意思是指媒体类型小于或等于指定的宽度时,样式生效。

@media screen and (max-width:580px){
 body {
   background-color: red;
  }
}
Copy after login

上面表示的是:当屏幕小于或等于580px时,页面的背景颜色变为红色。

- 最小宽度min-width

“min-width”与“max-width”相反,指的是媒体类型大于或等于指定宽度时,样式生效。

@media screen and (min-width:900px){
  .wrapper{width: 980px;}
}
Copy after login

上面表示的是:当屏幕大于或等于900px时,容器“.wrapper”的宽度为980px。

- 多个媒体特性使用

Media Queries可以使用关键词"and"将多个媒体特性结合在一起。也就是说,一个Media Query中可以包含0到多个表达式,表达式又可以包含0到多个关键字,以及一种媒体类型。  当屏幕在600px~900px之间时,body的背景色渲染为“blue”,如下所示。

@media screen and (min-width:600px) and (max-width:900px){
  body {background-color:blue;}
}
Copy after login

- 设备屏幕的输出宽度Device Width

在智能设备上,例如iPhone、iPad等,还可以根据屏幕设备的尺寸来设置相应的样式(或者调用相应的样式文件)。同样的,对于屏幕设备同样可以使用“min/max”对应参数,如“min-device-width”或者“max-device-width”。

<link rel="stylesheet" media="screen and (max-device-width:480px)" href="iphone.css" />
Copy after login

上面的代码指的是“iphone.css”样式适用于最大设备宽度为480px,比如说iPhone上的显示,这里的“max-device-width”所指的是设备的实际分辨率,也就是指可视面积分辨率。

-逗号分隔列表

当使用媒体查询的逗号分隔列表时,如果列表中的任何媒体查询为true,样式表都会被运用。在逗号分隔列表中的每个媒体查询都被作为独立查询对待,运用到一个媒体查询上的任何操作符都不影响其它的。

例如,如果你想应用一套样式在宽度大于等于700px的设备上,或者采用横向模式的便捷式设备上,你可以这样:

@media (min-width: 700px),handheld and (orientation: landscape) { ... }
Copy after login

如果我使用的设备的屏幕宽度大于700px,媒体查询将返回true,样式将被运用。如果我使用的是横向的便捷式设备,第一个媒体查询返回false,但第二个媒体查询将返回true,样式仍将被使用。

- not关键词

使用关键词“not”是用来排除某种制定的媒体类型,也就是用来排除符合表达式的设备。换句话说,not关键词表示对后面的表达式执行取反操作,如:

@media not print and (max-width: 1200px){样式代码}
Copy after login

上面代码表示的是:样式代码将被使用在除打印设备和设备宽度小于1200px下所有设备中。

- only关键词

only操作符表示仅在媒体查询匹配成功时应用指定样式。  可以通过它让选中的样式在老式浏览器中不被应用

media="only screen and (max-width:1000px)"{...}
Copy after login

上面这行代码,在老式浏览器中被解析为media="only",因为没有一个叫only的设备,所以实际上老式浏览器不会应用样式

media="screen and (max-width:1000px)"{...}
Copy after login

上面这行代码,在老式浏览器中被解析为media="screen",它把后面的逻辑表达式忽略了。所以老式浏览器会应用样式。所以,在使用媒体查询时,only最好不要忽略

- 在Media Query中如果没有明确指定Media Type,那么其默认为all,如:

<link rel="stylesheet" media="(min-width:701px) and (max-width:900px)" href="mediu.css" />
Copy after login

-在样式中,还可以使用多条语句来将同一个样式应用于不同的媒体类型和媒体特性中,指定方式如下所示。

<link rel="stylesheet" type="text/css" href="style.css" media="print and (max-width:480px), screen and (min-width:960px)" />
Copy after login

上面代码中style.css样式被用在宽度小于或等于480px的打印预览上,或者被用于屏幕宽度大于或等于960px的设备上。

推荐学习:《HTML5视频教程

width (can add max min prefix)
height
##portrait vertical screen/landscape horizontal screen

The above is the detailed content of What is the html5 media query statement?. 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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles