Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ความกว้าง Flex Container เมื่อ flex-flow ตั้งเป็น column wrap ใน HTML


หากคุณมี container ที่มี flex height และ display flex ตามนี้หากต้องการให้ children จัดเรียงเป็นคอลัมน์

เปลี่ยนการตั้งค่า:

-webkit-flex-flow: column wrap;

และ Chrome จะเพิ่มช่องว่างระหว่าง div และ border top อัตโนมัติ หากไม่ต้องการเพิ่มช่องว่างอัตโนมัติ ให้ใช้:

margin-bottom:100%;

สิ่งนี้จะย้าย item ขึ้นไป

.flex-container {
  position: fixed;
  height: 80%; // เปลี่ยนความสูง flex container เป็น 80%
  padding: 1;
  margin: 1; // เปลี่ยน margin เป็น 1
  list-style: none;
  border: 7px solid red; // เปลี่ยน border เป็น solid red
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: column wrap;
  justify-content: flex-start;
  align-content: flex-start;
  align-items: flex-start;
}
/** Just to show the elements */
body {
  margin: 0;
  padding: 0;
}
.flex-item {
  background: tomato;
  padding: 5px;
  width: 100px;
  height: 100px;
  margin-bottom: 100%;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="flex-container">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
</ul>