Flow layout and writing modes

Đặc tả CSS 2.1, mô tả cách hoạt động của normal flow, giả định một writing mode theo chiều ngang. Các thuộc tính layout sẽ hoạt động theo cùng cách trong các writing mode theo chiều dọc. Trong hướng dẫn này, chúng ta xem xét cách flow layout hoạt động khi sử dụng với các writing mode khác nhau của tài liệu.

Đây không phải là hướng dẫn toàn diện về cách sử dụng writing modes trong CSS; mục tiêu ở đây là ghi lại các vùng mà flow layout tương tác với writing modes theo những cách có thể ngoài dự kiến. Phần See also cung cấp liên kết đến thêm tài nguyên về writing modes.

Đặc tả writing modes

Đặc tả CSS Writing Modes Level 3 định nghĩa tác động của việc thay đổi writing mode của tài liệu đến flow layout. Trong phần giới thiệu writing modes, đặc tả nói,

"A writing mode in CSS is determined by the writing-mode, direction, and text-orientation properties. It is defined primarily in terms of its inline base direction and block flow direction."

Đặc tả định nghĩa inline base direction là hướng mà nội dung được sắp xếp trên một dòng. Điều này xác định điểm bắt đầu và kết thúc của chiều inline. Điểm bắt đầu là nơi các câu bắt đầu và điểm kết thúc là nơi một dòng văn bản kết thúc trước khi nó bắt đầu xuống dòng mới.

Block flow direction là hướng mà các box, ví dụ như các đoạn văn, được xếp chồng trong writing mode đó. Thuộc tính CSS writing-mode kiểm soát block flow direction. Nếu bạn muốn thay đổi trang của mình, hoặc một phần của trang, sang vertical-lr thì bạn có thể đặt writing-mode: vertical-lr trên phần tử và điều này sẽ thay đổi hướng của các block và do đó cũng thay đổi chiều inline.

Mặc dù một số ngôn ngữ nhất định sẽ sử dụng một writing mode hoặc text direction cụ thể, chúng ta cũng có thể sử dụng các thuộc tính này cho mục đích sáng tạo, chẳng hạn như hiển thị tiêu đề theo chiều dọc.

html
<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>
css
body {
  font: 1.2em sans-serif;
}
h1 {
  writing-mode: vertical-lr;
  float: left;
}

Block flow direction

Thuộc tính writing-mode chấp nhận các giá trị horizontal-tb, vertical-rlvertical-lr. Các giá trị này kiểm soát hướng mà các block chảy trên trang. Giá trị khởi tạo là horizontal-tb, đây là block flow direction từ trên xuống dưới với chiều inline theo chiều ngang. Các ngôn ngữ từ trái sang phải, như tiếng Anh, và các ngôn ngữ từ phải sang trái, như tiếng Ả Rập, đều là horizontal-tb.

Ví dụ sau hiển thị các block sử dụng giá trị khởi tạo horizontal-tb một cách tường minh:

html
<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>
css
body {
  font: 1.2em sans-serif;
}
.box {
  writing-mode: horizontal-tb;
}

So sánh với giá trị vertical-rl, cho bạn block flow direction từ phải sang trái với chiều inline theo chiều dọc, như thể hiện trong ví dụ tiếp theo.

css
body {
  font: 1.2em sans-serif;
}
.box {
  writing-mode: vertical-rl;
}

Ví dụ cuối cùng minh họa giá trị thứ ba có thể có cho writing-modevertical-lr. Điều này cho bạn block flow direction từ trái sang phải và chiều inline theo chiều dọc.

css
body {
  font: 1.2em sans-serif;
}
.box {
  writing-mode: vertical-lr;
}

Writing modes lồng nhau

Khi một box lồng nhau được gán một writing mode khác với phần tử cha, thì một inline level box sẽ hiển thị như thể nó có display: inline-block.

html
<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>
css
body {
  font: 1.2em sans-serif;
}
.box {
  writing-mode: vertical-rl;
}
.box span {
  writing-mode: horizontal-tb;
  padding: 10px;
  border: 1px solid rebeccapurple;
}

Một block-level box sẽ thiết lập một block formatting context (BFC) mới, nghĩa là nếu kiểu hiển thị bên trong của nó là flow, nó sẽ có kiểu hiển thị tính toán là flow-root. Điều này được thể hiện trong ví dụ tiếp theo, nơi box hiển thị dưới dạng horizontal-tb chứa một float được giữ lại nhờ phần tử cha thiết lập một BFC mới.

html
<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>
css
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;
}

Replaced elements

Các replaced elements như hình ảnh sẽ không thay đổi hướng của chúng dựa trên thuộc tính writing-mode. Tuy nhiên, các replaced elements như các control biểu mẫu có chứa văn bản, sẽ khớp với writing mode đang sử dụng.

html
<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>
css
body {
  font: 1.2em sans-serif;
}
.box {
  writing-mode: vertical-rl;
}

Logical properties và values

Khi bạn làm việc trong các writing mode khác ngoài horizontal-tb, nhiều thuộc tính và giá trị được ánh xạ đến các kích thước vật lý của màn hình sẽ có vẻ lạ. Ví dụ, nếu bạn đặt chiều rộng của một box là 100px, trong horizontal-tb điều đó sẽ kiểm soát kích thước theo chiều inline. Trong vertical-lr nó kiểm soát kích thước theo chiều block vì nó không xoay theo văn bản.

html
<div class="box">
  <div class="box1">Box 1</div>
  <div class="box2">Box 2</div>
</div>
css
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;
}

Do đó, chúng ta có các thuộc tính block-sizeinline-size. Nếu chúng ta đặt inline-size của block là 100px, không quan trọng chúng ta đang ở writing mode ngang hay dọc; inline-size sẽ luôn có nghĩa là kích thước theo chiều inline.

html
<div class="box">
  <div class="box1">Box 1</div>
  <div class="box2">Box 2</div>
</div>
css
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;
}

Module CSS logical properties and values bao gồm các phiên bản logical của các thuộc tính kiểm soát margins, padding và borders cũng như các ánh xạ khác cho những thứ mà chúng ta thường sử dụng hướng vật lý để chỉ định.

Tóm tắt

Trong hầu hết các trường hợp, flow layout hoạt động như mong đợi khi thay đổi writing mode của toàn bộ tài liệu hoặc các phần riêng lẻ. Điều này có thể được sử dụng để sắp xếp đúng cách các ngôn ngữ dọc hoặc vì lý do sáng tạo. Với CSS logical properties và values, việc đặt kích thước theo writing mode dọc có thể dựa trên kích thước inline và block của phần tử. Điều này hữu ích để tạo các component hoạt động trong các writing mode khác nhau.

Xem thêm