css媒体查询的区间

At-media queries are conditions in your stylesheet. The style rules they contain are applied only if certain criteria are met, such as device width, resolution, or browser window dimensions.

媒体查询是样式表中的条件仅当满足某些条件 (例如设备宽度,分辨率或浏览器窗口尺寸)时,才会应用它们包含的样式规则。

Media queries can be written inside a stylesheet, or may be the conditions for using the CSS in an external stylesheet. We‘ll start by exploring the first, and most common, application.

媒体查询可以写在样式表中,也可以是在外部样式表中使用CSS的条件。 我们将从探索第一个也是最常见的应用程序开始。

@媒体规则 (@media rules)

Most stylesheets begin with fundamental declarations that are true in all conditions:

大多数样式表都以在所有情况下适用的 基本声明开头

@charset utf-8
@font-face {font-family: 'Mallory';src: url('Mallory-Light.woff2') format('woff2');font-style: normal;font-weight: 300;
}
* { box-sizing: border-box;
}
body {font-family: Mallory, Gill Sans, sans-serif;background: hsl(240, 67%, 94%);color: black;
}

Note that our stylesheet uses @ rules to set the character encoding and embed a font. There are many uses for the @ symbol in CSS, including keyframe animations and @supports; the character is not specific to media queries.

请注意,我们的样式表使用@规则来设置字符编码并嵌入字体。 CSS中@符号有很多用途,包括关键帧动画和@supports ; 该字符不特定于媒体查询。

Working from this stylesheet, let‘s say we wanted to improve text contrast at smaller window sizes. We could add an @media rule to cover this condition by adding the following:

假设使用此样式表工作,我们希望在较小的窗口尺寸下改善文本对比度 。 我们可以通过添加以下内容来添加@media规则来满足此条件:

@media all and (max-width: 600px) {body {background: #000;color: #fff;}
}

If you test the resulting page, you‘ll see the background and text color change when the browser window narrows to 600px wide or less.

如果测试结果页面,则当浏览器窗口缩小到600px或更小时,您会看到背景和文本颜色发生变化。

There are a few important things to note at this stage:

在此阶段需要注意一些重要事项:

  1. @media rules work in addition to other aspects of responsive design, including fluid images.

    @media规则除了响应式设计的其他方面(包括流畅的图像)之外,还可以工作。

  2. The declarations inside the @media rule contain only the things that are altered if the conditions are met. Do not attempt to rewrite the entire stylesheet inside an @media rule. Only the things that change are added; the base rules that are not affected by the new declarations will still apply under the new conditions.

    @media规则中的声明包含满足条件的更改内容不要试图重写的整个内部样式表@media规则。 仅添加更改的内容; 不受新声明影响的基本规则仍将在新条件下适用。

  3. The @media rule uses curly braces to contain the declarations. You can have as many declarations inside the @media rule as you like. You can also have as many @media rules as you wish, although obviously the more you add, the more complex your CSS becomes.

    @media规则使用大括号包含声明。 您可以根据需要在@media规则中包含任意多个声明。 您也可以根据需要设置@media规则,尽管显然添加的规则越多,CSS越复杂。

  4. In general practice you should usually write your base CSS first in your stylesheet, i.e. the styles that will remain the same under all (or most) conditions, adding the @media rules at the end.

    在一般的做法你通常应该先写你的基地在CSS样式表 ,即会保持所有(或大部分)条件下,相同的样式,添加@media规则在最后

Due to the slightly more complex syntax of @media queries, it makes sense to test that they work first with something dramatic like a background color change, especially when you are just beginning to learn and apply them.

由于@media查询的语法稍微复杂一些,因此有必要测试它们是否首先在诸如背景颜色变化的戏剧性效果下工作 ,特别是在您刚开始学习和应用它们时。

句法 (Syntax)

The first part of the @media rule above – the all – states what kind of media the rule is applied to. all means exactly that: the declarations will be valid for every form of media, provided the condition (and (max-width: 600px)) is met.

上面@media规则的第一部分- all -说明该规则适用于哪种媒体all完全意味着:只要满足条件( and (max-width: 600px) ),声明对于每种形式的媒体都是有效的。

Associating conditions with an @media rule is optional. How a web page will be printed out is often specified in a rule like this, without any further conditions:

将条件与@media规则关联是可选的。 通常在这样的规则中指定如何打印网页 ,而没有任何其他条件:

@media print {/* rules for a print stylesheet here */
}

Technically, you could write your entire stylesheet inside an @media all { } rule, although that would be entirely redundant. @media { } would work too, since the lack of a media condition assumes all.

从技术上讲,您可以在@media all { }规则内编写整个样式表,尽管那完全是多余的。 @media { }也可以工作,因为缺少媒体条件会假设all

Other options for @media include screen (intended for color screens) and speech for speech synthesisers).

对于其他选项@media包括screen (用于彩色显示屏)以及speech语音合成器)。

最小宽度和最大宽度 (min-width and max-width)

The two most common conditions associated with all or screen are and (min-width: x) and and (max-width: x). Both measure the browser viewport width, and are usually the most straightforward way of determining it‘s size.

allscreen相关的两个最常见的条件是and (min-width: x)and (max-width: x) 。 两者都测量浏览器视口的宽度,并且通常是确定其大小的最直接的方法。

It‘s easy to get min-width and max-width confused; thankfully, there‘s also an easy way to remember which is which.

min-widthmax-width容易混淆; 值得庆幸的是,还有一种简单的方法可以记住哪个。

  • min-width means “the browser window must be at least this width or greater

    min-width表示“浏览器窗口必须至少等于或大于此宽度

  • max-width means “the browser can be up to this width or smaller

    max-width表示“ 浏览器可以达到此宽度或更小

You‘ll typically use one or the other in your stylesheet; it‘s rare (and usually confusing) to use both. Which one you use comes down to your design approach:

通常,您将在样式表中使用一个另一个。 很少同时使用这两种方法(通常会造成混淆)。 您使用哪一种取决于您的设计方法:

  • if you are using a mobile-first approach, then you are writing your base CSS for the smallest browser width, and adding adjustments to it as the browser widens with @media all and (min-width: x) { }.

    如果您使用的是移动第一种方法 ,那么你正在写的最小的浏览器的宽度你的基地CSS,并加入调整它作为浏览器扩大@media all and (min-width: x) { }

  • if you take a desktop-first approach, then you are writing your CSS for what appears on your monitor and adjusting it as the browser narrows using @media all and (max-width: x) { }.

    如果您采用桌面优先的方法,那么您使用监视器上显示的内容编写CSS,并使用@media all and (max-width: x) { }在浏览器缩小时对其进行调整。

单位 (Units)

Technically, the measurement defined for min-width and max-width can use almost any CSS length unit, with the exception of vw, vh, vmin or vmax. Using pixels is common, but comes with some caveats:

从技术上讲,为min-widthmax-width定义的度量可以使用几乎任何CSS长度单位 ,但vwvhvminvmax除外。 使用像素很常见,但有一些警告:

  • Pixels are not what you think they are in CSS. For example, the resolution for the product specs of the iPhone are not a direct part of the calculations for min-width and max-width.

    像素不是您认为CSS中的像素 。 例如,iPhone产品规格的分辨率不是min-widthmax-width的直接计算部分。

  • It‘s best to ignore device sizes. People tend to become obsessed with the exact pixel dimensions of the browser in the iPhone XIII, or whatever their mobile device is. That is a fool‘s errand: you‘ll constantly be chasing new mobile releases to update your stylesheet, and inevitably ignore other devices. Instead, write @media rules as interventions where the design of your site needs them. By making the browser window wider or narrower, you can find where your particular design “breaks”, and intervene at that point, an approach that will work for every device.

    最好忽略设备尺寸 。 人们趋向于迷恋iPhone XIII或任何移动设备中的浏览器的确切像素尺寸。 这是一个愚蠢的事情:您将不断追随新的移动版本来更新样式表,并且不可避免地会忽略其他设备。 而是@media规则编写为网站设计需要它们的干预 。 通过使浏览器窗口变宽或变窄,您可以找到特定设计的“中断”位置,并在此进行干预该方法将适用于所有设备。

组合器 (combinators)

You‘ve already seen one combinator in an @media query: and. It‘s also possible to use not and only and a comma delimiter:

您已经在@media查询中看到一个组合器: and 。 它也可以使用notonly和逗号分隔符:

@media only screen {…
}

Or:

要么:

@media screen, print {…
}

You can use and and or as many times as you wish to chain together conditions:

您可以使用andor任意多次将条件链接在一起:

@media screen and (max-width: 600px) and (min-resolution: 200dpi) {…
}

There are many conditions that can be added in an @media query, but it is always recommended to start with the basics first.

@media查询中可以添加许多条件,但是始终建议首先从基础开始。

变化 (Variations)

As mentioned at the start of this article, it‘s also possible to write media queries inside link elements:

如本文开头所述,还可以在link元素中编写媒体查询:

<link rel="stylesheet" href="styles_h.css"media="only screen and (max-width: 480px)">

Contrary to expectations, media conditions do not prevent the browser from loading the stylesheet. The browser loads the file regardless, but it does not use the CSS in the stylesheet unless it matches the associated conditions.

与预期相反, media条件不会阻止浏览器加载样式表。 浏览器无论如何都会加载文件,但是除非符合相关条件,否则它将不使用样式表中CSS。

条件,局限性和结论 (Conditions, Limitations and Conclusions)

As powerful as they are, @media queries do have a few limitations that you should be aware of:

@media查询功能强大,但确实有一些限制,您应该注意以下几点:

  1. Currently @media inspects conditions in the browser viewport and device, not the state of individual elements. So-called “element queries” are planned for CSS; right now, a JavaScript polyfill is recommended to create the functionality.

    当前,@ @media检查浏览器视口和设备中的条件, 而不是单个元素的状态。 所谓的“元素查询” 计划于CSS; 现在, 建议使用JavaScript polyfill创建功能。

  2. @media queries can‘t use CSS variables in their conditions, nor can they be nested inside elements. If you want those kinds of features, use Sass or another CSS pre-processor.

    @media查询不能在其条件下使用CSS变量 ,也不能嵌套在元素内。 如果需要这些功能,请使用Sass或其他CSS预处理器。

翻译自: https://thenewcode.com/154/CSS-media-queries

css媒体查询的区间

css媒体查询的区间_CSS媒体查询相关推荐

  1. 1631 小鲨鱼在51nod小学(线段树区间修改+单点查询:不用下传lazy的区间修改)

    题目描述: 1631 小鲨鱼在51nod小学 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务. 每一个职务都有一个起始时间A和结束时间B, ...

  2. 媒体查询中常用的媒体类型罗列

    关于媒体查询的概念这里就不多介绍了,具体可以参阅 媒体查询详解 一章节. 下面罗列一下媒体查询中媒体的类型,当然并不是所有的媒体类型都是经常用到的,有的仅作了解即可. 列表如下: (1).all – ...

  3. lintcode---线段树查询||(区间元素个数)

    对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现一个 query 的方法,该 ...

  4. python【蓝桥杯vip练习题库】ALGO-1区间k大数查询

    试题 算法训练 区间k大数查询 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示 ...

  5. 蓝桥杯-区间k大数查询(java)

    算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB问题描述给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个.输入格式第一行包含一个数n,表示序列长度.第二行包含n个 ...

  6. 树状数组之区间修改单点查询

    树状数组的区间修改单点查询 树状数组其实本质还单点修改区间查询,但是我们怎么延伸到这个呢,我们建立一个差分数组, 比如:                a[10]={4, 6, 7, 5, 1, 6, ...

  7. java:区间k大数查询

    试题 算法训练 区间k大数查询 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示 ...

  8. java 蓝桥杯 算法训练 区间k大数查询(题解)

    试题 算法训练 区间k大数查询 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示 ...

  9. 树状数组相关应用之区间更新单点查询问题

    区间更新单点查询 树状数组的基本应用是单点更新,区间查询(例如求区间和). 鉴于树状数组的空间复杂度和时间复杂度都比线段树小 而且代码也短 所以就有大神用强大的脑洞YY出了区间修改+单点查询的树状数组 ...

最新文章

  1. linux ssh权限漏洞,OpenSSH do_setup_env函数权限提升漏洞(CVE-2015-8325)
  2. iOS 开发 高级:使用 宏定义macros (#,##,...,__VA_ARGS_)
  3. 企业选择数据中心的建议
  4. 牛客题霸 NC13 二叉树的最大深度
  5. exls导入数据库 php_PHP读取excel文件并导入数据库
  6. ngnix 执行php 慢,【nginx】PHP有什么办法可以排查那些脚本或方法执行慢,需要优化?...
  7. JavaScript 你必须了解的主流趋势!
  8. python 测试开发请关注这个新框架
  9. Activiz 使用笔记-4 数据源(2)
  10. 【JAVA秒会技术之ActiveMQ】ActiveMQ的快速入门
  11. Deepin在matlab的安装
  12. 智慧水务数字化云平台建设方案
  13. 安卓开发 给控件左边右边下边添加阴影_Android 控件布局实现卡片效果,阴影效果...
  14. Dapper使用技巧和基础CRUD
  15. 轩辕剑--资料集(三)
  16. Illegal base64 character 20
  17. 小程序搜索页面搜索记录和热门搜索实现
  18. C#快速调用海康威视工业相机的SDK拍照获取图片
  19. 治愈系英语笔记-2-一般、否定疑问句,现表将来
  20. 装上后这 10个扩展后,VSCode 猛虎添翼,无敌了

热门文章

  1. textarea 赋值 文本换行显示
  2. jquery ajax 不执行success,jQuery通过ajax方法获取json数据不执行success的原因及解决方法...
  3. AFT三代引擎登录加密算法
  4. mysql环境安装及登录
  5. MySQL卸载以及重装
  6. 高德地图自定义创建地图
  7. DeblurGAN:使用条件对抗网络的盲目运动去模糊
  8. 进程间通信的方式及其特点
  9. 零视科技 H5S视频平台 GetUserInfo 信息泄漏漏洞 CNVD-2020-67113
  10. 《数据结构》—— 哈夫曼树