blob: d18fb02b2b4228ab42675c743b0f50e98289d2c5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
---
title: aspect-ratio
slug: Web/CSS/@media/aspect-ratio
translation_of: Web/CSS/@media/aspect-ratio
---
<div>{{cssref}}</div>
<p><strong><code>aspect-ratio</code></strong> <a href="/zh-CN/docs/CSS">CSS</a> <a href="/zh-CN/docs/Web/CSS/Media_Queries/Using_media_queries#Media_features">媒体属性</a> 可以用来测试 {{glossary("viewport")}} 的宽高比。</p>
<h2 id="语法">语法</h2>
<p>宽高比属性被指定为{{cssxref("<ratio>")}}值来代表viewport的宽高比。其为一个范围,这意味着你可以使用<strong><code>min-aspect-ratio</code></strong>和<code><strong>max-aspect-ratio</strong></code>分别查询最小和最大的值。</p>
<h2 id="例子">例子</h2>
<p>下面的例子包含一个 {{htmlElement("iframe")}} ,拥有它自身的viewport。调整的<code><iframe></code>宽高来观察<code>aspect-ratio</code>的变化。</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html notranslate"><div id='inner'>
Watch this element as you resize your viewport's width and height.
</div>
</pre>
<h3 id="CSS">CSS</h3>
<pre class="brush: css notranslate">/* 最小宽高比 */
@media (min-aspect-ratio: 8/5) {
div {
background: #9af; /* blue */
}
}
/* 最大宽高比 */
@media (max-aspect-ratio: 3/2) {
div {
background: #9ff; /* cyan */
}
}
/* 明确的宽高比, 放在最下部防止同时满足条件时的覆盖*/
@media (aspect-ratio: 1/1) {
div {
background: #f9a; /* red */
}
}
</pre>
<div class="hidden">
<h2 id="_Example">_Example</h2>
<p>used iframe and DataURL to enable this iframe could resize</p>
<h3 id="HTML_2">HTML</h3>
<pre class="brush: html notranslate"><label id="wf" for="w">width:165</label>
<input id="w" name="w" type="range" min="100" max="250" step="5" value="165">
<label id="hf" for="w">height:165</label>
<input id="h" name="h" type="range" min="100" max="250" step="5" value="165">
<iframe id="outer" src="data:text/html,<style> @media (min-aspect-ratio: 8/5) { div { background: %239af; } } @media (max-aspect-ratio: 3/2) { div { background: %239ff; } } @media (aspect-ratio: 1/1) { div { background: %23f9a; } }</style><div id='inner'> Watch this element as you resize your viewport's width and height.</div>">
</iframe>
</pre>
<h3 id="CSS_2">CSS</h3>
<pre class="brush: css notranslate">iframe{
display:block;
}</pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: js notranslate">outer.style.width=outer.style.height="165px"
w.onchange=w.oninput=function(){
outer.style.width=w.value+"px"
wf.textContent="width:"+w.value
}
h.onchange=h.oninput=function(){
outer.style.height=h.value+"px"
hf.textContent="height:"+h.value
}</pre>
</div>
<h3 id="结果">结果</h3>
<div style="overflow: auto;">
<p>{{ EmbedLiveSample('_Example', '300px', '400px') }}</p>
</div>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('CSS4 Media Queries', '#aspect-ratio', 'aspect-ratio')}}</td>
<td>{{Spec2('CSS4 Media Queries')}}</td>
<td>No change.</td>
</tr>
<tr>
<td>{{SpecName('CSS3 Media Queries', '#aspect-ratio', 'aspect-ratio')}}</td>
<td>{{Spec2('CSS3 Media Queries')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("css.at-rules.media.aspect-ratio")}}</p>
|