Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. CSS:层叠样式表
  3. CSS 参考
  4. 属性
  5. max-width

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

max-width

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

* Some parts of this feature may have varying levels of support.

max-widthCSS 属性设置元素的最大宽度。它可防止width 属性的应用值大于max-width 指定的值。

尝试一下

max-width: 150px;
max-width: 20em;
max-width: 75%;
max-width: 20ch;
<section>  <div>    Change the maximum width.  </div></section>
#example-element {  display: flex;  flex-direction: column;  background-color: #5b6dcd;  height: 80%;  justify-content: center;  color: #ffffff;}

max-width 会覆盖width 的设置,但min-width 的设置会覆盖max-width

语法

css
/* <length> 值 */max-width: 3.5em;/* <percentage> 值 */max-width: 75%;/* 关键字值 */max-width: none;max-width: max-content;max-width: min-content;max-width: fit-content;max-width: fit-content(20em);/* 全局值 */max-width: inherit;max-width: initial;max-width: revert;max-width: revert-layer;max-width: unset;

<length>

以绝对值定义max-width

<percentage>

以包含区块的宽度百分比定义max-width

none

盒子大小没有限制。

max-content

固有首选max-width

min-content

固有最小max-width

fit-content

使用可用空间,但不得超过max-content,即min(max-content,max(min-content,stretch))

fit-content(<length-percentage>)实验性

使用fit-content 公式,用指定参数替换可用空间,即min(max-content, max(min-content, argument))

无障碍考虑

确保设置了max-width 的元素在页面缩放以增大文字大小时不会被截断和/或遮挡其他内容。

形式定义

初始值none
适用元素all elements but non-replaced inline elements, table rows, and row groups
是否是继承属性
Percentagesrefer to the width of the containing block
计算值the percentage as specified or the absolute length ornone
动画类型alength,percentage or calc();

形式语法

max-width =
none|
<length-percentage [0,∞]>|
min-content|
max-content|
fit-content(<length-percentage [0,∞]>)|
<calc-size()>|
<anchor-size()>|
stretch|
fit-content|
contain

<length-percentage> =
<length>|
<percentage>

<calc-size()> =
calc-size(<calc-size-basis> ,<calc-sum>)

<anchor-size()> =
anchor-size([<anchor-name>||<anchor-size>]? ,<length-percentage>?)

<calc-size-basis> =
<size-keyword>|
<calc-size()>|
any|
<calc-sum>

<calc-sum> =
<calc-product>[[ '+'| '-']<calc-product>]*

<anchor-name> =
<dashed-ident>

<anchor-size> =
width|
height|
block|
inline|
self-block|
self-inline

<calc-product> =
<calc-value>[[ '*'| /]<calc-value>]*

<calc-value> =
<number>|
<dimension>|
<percentage>|
<calc-keyword>|
(<calc-sum>)

<calc-keyword> =
e|
pi|
infinity|
-infinity|
NaN

示例

设置最大像素宽度

在本例中,“child”的宽度为 150 像素或“parent”的宽度,以较小者为准。

HTML

html
<div>  <div>    Fusce pulvinar vestibulum eros, sed luctus ex lobortis quis.  </div></div>

CSS

css
#parent {  background: lightblue;  width: 300px;}#child {  background: gold;  width: 100%;  max-width: 150px;}

规范

Specification
CSS Box Sizing Module Level 4
# sizing-values

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp