Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách làm cho flex items hiển thị theo cột với JavaScript

Thuộc tính flexFlow là shorthand kết hợp flex-direction và flex-wrap. Dùng column nowrap để sắp xếp các item theo cột dọc, không xuống dòng.

Ví dụ minh họa

Container ban đầu flex-flow: row wrap. Nhấn nút để chuyển sang cột không wrap:

<!DOCTYPE html>
<html>
<head>
  <style>
    #box {
      border: 1px solid #000;
      width: 100px;
      height: 150px;
      display: flex;
      flex-flow: row wrap;
    }
    #box div {
      height: 40px;
      width: 40px;
    }
  </style>
</head>
<body>
  <div id="box">
    <div style="background-color:orange;">DIV1</div>
    <div style="background-color:blue;">DIV2</div>
    <div style="background-color:yellow;" id="myID">DIV3</div>
  </div>
  <p>Sử dụng thuộc tính flexDirection</p>
  <button onclick="display()">Thiết lập</button>
  <script>
    function display() {
      document.getElementById("box").style.flexFlow = "column nowrap";
    }
  </script>
</body>
</html>

Cú pháp flexFlow

flex-flow: <flex-direction> <flex-wrap>

Ví dụHành vi
row nowrapHàng ngang, không wrap (mặc định)
row wrapHàng ngang, wrap khi cần
column nowrapCột dọc, không wrap
column wrapCột dọc, wrap khi cần