search
HomeWeb Front-endFront-end Q&AWhat is the html5 media query statement?

What is the html5 media query statement?

Jan 28, 2023 am 10:05 AM
html5Media 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>

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

@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)

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 (). 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;
  }
}

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

- 最小宽度min-width

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

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

上面表示的是:当屏幕大于或等于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;}
}

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

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

<link>

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

-逗号分隔列表

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

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

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

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

- not关键词

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

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

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

- only关键词

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

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

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

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

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

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

<link>

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

<link>

上面代码中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
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
CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

CSS ID and Class: common mistakesCSS ID and Class: common mistakesMay 13, 2025 am 12:11 AM

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

What is thedifference between class and id selector?What is thedifference between class and id selector?May 12, 2025 am 12:13 AM

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

CSS IDs vs Classes: The real differencesCSS IDs vs Classes: The real differencesMay 12, 2025 am 12:10 AM

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

CSS: What if I use just classes?CSS: What if I use just classes?May 12, 2025 am 12:09 AM

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and Class Selectors in CSS: A Beginner's GuideID and Class Selectors in CSS: A Beginner's GuideMay 12, 2025 am 12:06 AM

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

See all articles

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.