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

瀏覽器相容性

另見