aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/@media/width/index.html
blob: 79b6612aac742642d180b082e269e421fb50c99a (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
121
122
123
124
125
126
---
title: width
slug: Web/CSS/@media/width
tags:
  - '@media'
  - CSS
  - NeedsBrowserCompatibility
  - Reference
  - media feature
translation_of: Web/CSS/@media/width
---
<div>{{cssref}}</div>

<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">可以使用</span></font><strong>width</strong></code> <a href="/en-US/docs/CSS">CSS</a> {{cssxref("@media")}} 基于{{glossary("viewport")}}宽度(或页面框,用于<a href="/en-US/docs/Web/CSS/Paged_media">paged media</a>)应用样式。</p>

<h2 id="语法"><strong>语法</strong></h2>

<p><code>width</code>特性被指定为{{cssxref("&lt;length&gt;")}},表示viewport宽度的值。这是一个范围特性,也就是说,您也可以使用前缀<strong><code>min-width</code></strong><code><strong>max-width</strong></code>分别查询最小值和最大值。</p>

<h2 id="例子"><strong>例子</strong></h2>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;div&gt;改变viewport的宽度的同时,观察这个元素的变化。&lt;/div&gt;</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">/* 精确宽度 */
@media (width: 360px) {
  div {
    color: red;
  }
}

/* 最小宽度 */
@media (min-width: 35rem) {
  div {
    background: yellow;
  }
}

/* 最大宽度 */
@media (max-width: 50rem) {
  div {
    border: 2px solid blue;
  }
}
</pre>

<h3 id="结果">结果</h3>

<p>{{EmbedLiveSample('Example','90%')}}</p>

<h2 id="规范"><strong>规范</strong></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', '#width', 'width')}}</td>
   <td>{{Spec2('CSS4 Media Queries')}}</td>
   <td>The value can now be negative, in which case it computes to false.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Media Queries', '#width', 'width')}}</td>
   <td>{{Spec2('CSS3 Media Queries')}}</td>
   <td>Initial definition. The value must be nonnegative.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility"><strong>浏览器兼容性</strong></h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>9.0</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>