width CSS media feature can be used to test the width of the viewport (or the page box, for paged media ).

句法

width feature is specified as a <length> value representing the viewport width. It is a range feature, meaning that you can also use the prefixed min-width and max-width variants to query minimum and maximum values, respectively.

范例

HTML

<div>Watch this element as you resize your viewport's width.</div>
					

CSS

/* Exact width */
@media (width: 360px) {
  div {
    color: red;
  }
}
/* Minimum width */
@media (min-width: 35rem) {
  div {
    background: yellow;
  }
}
/* Maximum width */
@media (max-width: 50rem) {
  div {
    border: 2px solid blue;
  }
}
					

结果

规范

规范 状态 注释
Media Queries Level 4
The definition of 'width' in that specification.
候选推荐 The value can now be negative, in which case it computes to false.
Media Queries
The definition of 'width' in that specification.
推荐 Initial definition. The value must be nonnegative.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
width media feature Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 2 IE 完整支持 9 Opera 完整支持 10 Safari 完整支持 3 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

元数据

  • 最后修改: