流式佈局和書寫模式
CSS 2.1 規範詳細描述了普通流的行為方式,並假設採用水平書寫模式。佈局屬性在垂直書寫模式下也應以相同方式工作。在本指南中,我們將探討流式佈局與不同文件書寫模式結合使用時的行為方式。
這不是一份關於 CSS 中書寫模式使用的全面指南,其目的在於記錄流式佈局可能以意想不到的方式與書寫模式互動的區域。另請參閱部分提供了更多書寫模式資源的連結。
書寫模式規範
CSS 書寫模式級別 3 規範定義了文件書寫模式更改對流式佈局的影響。在書寫模式簡介中,規範指出,
“CSS 中的書寫模式由
writing-mode、direction和text-orientation屬性決定。它主要根據其行內基方向和塊流方向進行定義。”
規範將行內基方向定義為內容在行中排列的方向。這定義了行內方向的起始和結束。起始是句子開始的地方,結束是一行文字在換行之前結束的地方。
塊流方向是框(例如段落)在該書寫模式中堆疊的方向。CSS writing-mode 屬性控制塊流方向。如果你想將你的頁面或頁面的一部分更改為 vertical-lr,你可以在元素上設定 writing-mode: vertical-lr,這將改變塊的方向,從而也改變行內方向。
雖然某些語言會使用特定的書寫模式或文字方向,但我們也可以使用這些屬性來達到創意效果,例如垂直執行標題。
<div class="box">
<h1>A heading</h1>
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney. Their names
were Stephen and Joseph Montgolfier, they were papermakers by trade, and
were noted as possessing thoughtful minds and a deep interest in all
scientific knowledge and new discovery.
</p>
</div>
body {
font: 1.2em sans-serif;
}
h1 {
writing-mode: vertical-lr;
float: left;
}
塊流方向
writing-mode 屬性接受 horizontal-tb、vertical-rl 和 vertical-lr 值。這些值控制塊在頁面上流動的方向。初始值是 horizontal-tb,這是一種從上到下的塊流方向,具有水平行內方向。從左到右的語言(例如英語)和從右到左的語言(例如阿拉伯語)都是 horizontal-tb。
以下示例明確顯示了使用初始 horizontal-tb 值的塊
<div class="box">
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney. Their names
were Stephen and Joseph Montgolfier, they were papermakers by trade, and
were noted as possessing thoughtful minds and a deep interest in all
scientific knowledge and new discovery.
</p>
<p>
Before that night—a memorable night, as it was to prove—hundreds of millions
of people had watched the rising smoke-wreaths of their fires without
drawing any special inspiration from the fact.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: horizontal-tb;
}
將其與 vertical-rl 值進行比較,後者提供從右到左的塊流方向和垂直行內方向,如下一個示例所示。
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: vertical-rl;
}
最後一個示例演示了 writing-mode 的第三個可能值——vertical-lr。這會給你一個從左到右的塊流方向和垂直行內方向。
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: vertical-lr;
}
巢狀書寫模式
當巢狀框被指定與其父級不同的書寫模式時,行內級別框將顯示為具有 display: inline-block。
<div class="box">
<p>
One <span>November</span> night in the year 1782, so the story runs, two
brothers sat over their winter fire in the little French town of Annonay,
watching the grey smoke-wreaths from the hearth curl up the wide chimney.
Their names were Stephen and Joseph Montgolfier, they were papermakers by
trade, and were noted as possessing thoughtful minds and a deep interest in
all scientific knowledge and new discovery.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: vertical-rl;
}
.box span {
writing-mode: horizontal-tb;
padding: 10px;
border: 1px solid rebeccapurple;
}
塊級框將建立一個新的塊格式化上下文,這意味著如果其內部顯示型別是 flow,它將獲得計算的顯示型別 flow-root。這在下一個示例中顯示,其中顯示為 horizontal-tb 的框包含一個浮動元素,該浮動元素由於其父級建立新的 BFC 而被包含。
<div class="box">
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney.
</p>
<div>
<div class="float"></div>
This box should establish a new BFC.
</div>
<p>
Their names were Stephen and Joseph Montgolfier, they were papermakers by
trade, and were noted as possessing thoughtful minds and a deep interest in
all scientific knowledge and new discovery.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: vertical-rl;
}
.box > div {
writing-mode: horizontal-tb;
padding: 10px;
border: 1px solid rebeccapurple;
}
.float {
width: 100px;
height: 150px;
background-color: rebeccapurple;
float: left;
}
替換元素
替換元素(例如影像)不會根據 writing-mode 屬性改變其方向。但是,包含文字的替換元素(例如表單控制元件)應與正在使用的書寫模式匹配。
<div class="box">
<p>
One November night in the year 1782, so the story runs, two brothers sat
over their winter fire in the little French town of Annonay, watching the
grey smoke-wreaths from the hearth curl up the wide chimney.
</p>
<img
alt="a colorful hot air balloon against a clear sky"
src="https://mdn.github.io/shared-assets/images/examples/balloon.jpg" />
<p>
Their names were Stephen and Joseph Montgolfier, they were papermakers by
trade, and were noted as possessing thoughtful minds and a deep interest in
all scientific knowledge and new discovery.
</p>
</div>
body {
font: 1.2em sans-serif;
}
.box {
writing-mode: vertical-rl;
}
邏輯屬性和值
一旦你在 horizontal-tb 以外的書寫模式下工作,許多對映到螢幕物理尺寸的屬性和值就會顯得很奇怪。例如,如果你給一個框設定 100px 的寬度,在 horizontal-tb 中,它將控制行內方向的尺寸。在 vertical-lr 中,它控制塊方向的尺寸,因為它不隨文字旋轉。
<div class="box">
<div class="box1">Box 1</div>
<div class="box2">Box 2</div>
</div>
body {
font: 1.2em sans-serif;
}
.box1 {
writing-mode: horizontal-tb;
border: 5px solid rebeccapurple;
width: 100px;
margin: 10px;
}
.box2 {
writing-mode: vertical-lr;
border: 5px solid rebeccapurple;
width: 100px;
margin: 10px;
}
因此,我們有了新的屬性 block-size 和 inline-size。如果我們給塊設定 100px 的 inline-size,無論我們處於水平還是垂直書寫模式,inline-size 始終表示行內方向的尺寸。
<div class="box">
<div class="box1">Box 1</div>
<div class="box2">Box 2</div>
</div>
body {
font: 1.2em sans-serif;
}
.box1 {
writing-mode: horizontal-tb;
border: 5px solid rebeccapurple;
inline-size: 100px;
margin: 10px;
}
.box2 {
writing-mode: vertical-lr;
border: 5px solid rebeccapurple;
inline-size: 100px;
margin: 10px;
}
CSS 邏輯屬性和值模組包括控制邊距、內邊距和邊框的屬性的邏輯版本,以及我們通常使用物理方向指定的其他對映。
總結
在大多數情況下,當你更改文件或文件部分的寫入模式時,流式佈局會按你預期的方式工作。這可用於正確排版垂直語言或出於創意原因。CSS 透過引入邏輯屬性和值使其變得更容易,這樣在垂直寫入模式下工作時,大小可以基於元素的行內和塊大小。這對於建立可以在不同寫入模式下工作的元件將非常有用。
另見
- 書寫模式
- 在 Smashing Magazine 上關於書寫模式和 CSS 佈局 (2019)
- 在 24ways.org 上關於CSS 書寫模式 (2016)