max-block-size

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

* 此特性的某些部分可能存在不同级别的支持。

max-block-size CSS 属性指定了元素在与 writing-mode 指定的书写方向相反方向上的最大尺寸。也就是说,如果书写方向是水平的,那么 max-block-size 等同于 max-height;如果书写方向是垂直的,max-block-sizemax-width 相同。

另一个维度的最大长度使用 max-inline-size 属性指定。

这很有用,因为 max-width 总是用于水平尺寸,max-height 总是用于垂直尺寸,如果你需要根据文本内容的大小设置长度,你需要能够考虑到书写方向。

任何时候你通常使用 max-heightmax-width,都应该改用 max-block-size 来设置内容的最大“高度”(即使这可能不是一个垂直值),并使用 max-inline-size 来设置内容的最大“宽度”(尽管这可能不是水平而是垂直)。请参阅 writing-mode 示例,其中展示了不同书写模式的作用。

试一试

max-block-size: 150px;
writing-mode: horizontal-tb;
max-block-size: 150px;
writing-mode: vertical-rl;
max-block-size: 20px;
writing-mode: horizontal-tb;
max-block-size: 75%;
writing-mode: vertical-lr;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box where you can change the maximum block size. <br />This will
    limit the size in the block dimension, potentially causing an overflow.
  </div>
</section>
#example-element {
  display: flex;
  flex-direction: column;
  background-color: #5b6dcd;
  justify-content: center;
  color: white;
}

语法

css
/* <length> values */
max-block-size: 300px;
max-block-size: 25em;
max-block-size: anchor-size(--my-anchor self-inline, 250px);
max-block-size: calc(anchor-size(width) / 2);

/* <percentage> values */
max-block-size: 75%;

/* Keyword values */
max-block-size: none;
max-block-size: max-content;
max-block-size: min-content;
max-block-size: fit-content;
max-block-size: fit-content(20em);

/* Global values */
max-block-size: inherit;
max-block-size: initial;
max-block-size: revert;
max-block-size: revert-layer;
max-block-size: unset;

max-block-size 属性的值可以是 max-widthmax-height 属性的任何合法值。

<length>

max-block-size 定义为绝对值。

<percentage>

max-block-size 定义为包含块在块轴方向尺寸的百分比。

none

盒子大小没有限制。

max-content

固有的首选 max-block-size

min-content

固有的最小 max-block-size

fit-content

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

fit-content(<length-percentage>)

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

书写模式如何影响方向性

writing-mode 的值会影响 max-block-sizemax-widthmax-height 的映射,如下所示:

writing-mode 的值 max-block-size 等同于
horizontal-tb, lr, lr-tb, rl, rb, rb-rl max-height
vertical-rl, vertical-lr, sideways-rl, sideways-lr, tb, tb-rl max-width

注意: writing-modesideways-lrsideways-rl 在 CSS Writing Modes Level 3 规范的后期设计过程中被移除。它们可能会在 Level 4 中恢复。

注意: 书写模式 lrlr-tbrlrbrb-tlHTML 上下文中不再允许;它们只能在 SVG 1.x 上下文中使用。

正式定义

初始值none
应用于widthheight 相同
继承性
百分比包含块的块大小
计算值max-widthmax-height 相同
动画类型一个长度百分比或 calc();

正式语法

max-block-size = 
<'max-width'>

<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

示例

使用水平和垂直文本设置 max-block-size

在此示例中,相同的文本(赫尔曼·梅尔维尔小说白鲸的开篇句)以 horizontal-tbvertical-rl 两种书写模式呈现。

两个盒子的所有其他内容都相同,包括 max-block-size 使用的值。

HTML

HTML 建立了两个 <div> 块,它们将使用类 horizontalvertical 设置其 writing-mode。两个盒子都共享 standard-box 类,该类设置了颜色、内边距以及它们各自的 max-block-size 值。

html
<p>Writing mode <code>horizontal-tb</code> (the default):</p>
<div class="standard-box horizontal">
  Call me Ishmael. Some years ago—never mind how long precisely—having little or
  no money in my purse, and nothing particular to interest me on shore, I
  thought I would sail about a little and see the watery part of the world. It
  is a way I have of driving off the spleen and regulating the circulation.
</div>

<p>Writing mode <code>vertical-rl</code>:</p>
<div class="standard-box vertical">
  Call me Ishmael. Some years ago—never mind how long precisely—having little or
  no money in my purse, and nothing particular to interest me on shore, I
  thought I would sail about a little and see the watery part of the world. It
  is a way I have of driving off the spleen and regulating the circulation.
</div>

CSS

CSS 定义了三个类。第一个 standard-box 应用于两个盒子,如上所示。它提供了标准样式,包括最小和最大块大小、字体大小等。

之后是 horizontalvertical 类,它们向盒子添加了 writing-mode 属性,其值根据使用的类设置为 horizontal-tbvertical-rl

css
.standard-box {
  padding: 4px;
  background-color: #abcdef;
  color: black;
  font:
    16px "Open Sans",
    "Helvetica",
    "Arial",
    sans-serif;
  max-block-size: 160px;
  min-block-size: 100px;
}

.horizontal {
  writing-mode: horizontal-tb;
}

.vertical {
  writing-mode: vertical-rl;
}

结果

规范

规范
CSS 逻辑属性和值第 1 级
# propdef-max-block-size
CSS Box Sizing Module Level 4
# sizing-values

浏览器兼容性

另见