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

Giá trị space-between của align-content trong CSS Flexbox

Giá trị space-between của align-content phân bố các flex lines: dòng đầu chạm đầu container, dòng cuối chạm cuối container, các dòng giữa cách đều nhau.

Điều kiện

  • display: flex + flex-wrap: wrap
  • Container có height/min-height > tổng chiều cao lines
  • Ít nhất 2 dòng (3+ lines mới thấy rõ khoảng cách giữa)

Ví dụ: align-content: space-between

<!DOCTYPE html>
<html>
<head>
  <style>
    .mycontainer {
      display: flex;
      height: 200px;
      background-color: #e74c3c;
      align-content: space-between;
      flex-wrap: wrap;
      padding: 10px;
      gap: 10px;
    }
    .mycontainer > div {
      background-color: #f1c40f;
      text-align: center;
      line-height: 60px;
      font-size: 30px;
      width: 100px;
      margin: 5px;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <h1>Queue</h1>
  <div class="mycontainer">
    <div>Q1</div>
    <div>Q2</div>
    <div>Q3</div>
    <div>Q4</div>
    <div>Q5</div>
    <div>Q6</div>
    <div>Q7</div>
    <div>Q8</div>
  </div>
</body>
</html>

Minh họa trực quan

Container: 200px | 4 lines × 60px = 240px (giả sử fit)
Actually: lines fit with gap, remaining space distributed BETWEEN lines

Line1 (top edge)
    ↑ space
Line2
    ↑ space  
Line3
    ↑ space
Line4 (bottom edge)

So sánh space-between vs space-around vs space-evenly

Giá trịĐầu/CuốiGiữa các lines
space-betweenChạm mép (0)Khoảng cách đều
space-aroundNửa khoảng cáchKhoảng cách đều
space-evenlyKhoảng cách đầy đủKhoảng cách đều (y hệt 2 đầu)

Ứng dụng: Sticky header + footer trong flex column

.page-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  align-content: space-between; /* Header top, footer bottom */
}
.header { /* top */ }
.main { flex: 1; }
.footer { /* bottom */ }

Lưu ý

  • Chỉ tác dụng khi nhiều dòng (flex-wrap: wrap)
  • Single line: dùng justify-content: space-between
  • Cần container height xác định