ข้ามไปที่เนื้อหาหลัก ข้ามไปที่การนำทางเอกสาร
Check
in English

คอลัมน์

เรียนรู้วิธีแก้ไขคอลัมน์ด้วยตัวเลือกมากมายสำหรับการจัดตำแหน่ง การสั่งซื้อ และการชดเชยด้วยระบบกริด flexbox ของเรา นอกจากนี้ ดูวิธีใช้คลาสของคอลัมน์เพื่อจัดการความกว้างขององค์ประกอบที่ไม่ใช่กริด

หัวขึ้น! อย่าลืม อ่านหน้า Gridก่อนดำดิ่งสู่วิธีการแก้ไขและปรับแต่งคอลัมน์กริดของคุณ

ทำงานอย่างไร

  • คอลัมน์สร้างขึ้นบนสถาปัตยกรรม flexbox ของกริด Flexbox หมายความว่าเรามีตัวเลือกสำหรับเปลี่ยนแต่ละคอลัมน์และ ปรับเปลี่ยนกลุ่มของคอลัมน์ ที่ระดับแถว คุณเลือกได้ว่าคอลัมน์จะขยาย ย่อ หรือเปลี่ยนแปลงอย่างไร

  • เมื่อสร้างเค้าโครงกริด เนื้อหาทั้งหมดจะอยู่ในคอลัมน์ ลำดับชั้นของกริดของ Bootstrap จะเปลี่ยนจากคอนเทนเนอร์หนึ่งไปอีกแถวหนึ่งไปยังอีกคอลัมน์หนึ่งไปยังเนื้อหาของคุณ ในบางกรณี คุณอาจรวมเนื้อหาและคอลัมน์เข้าด้วยกัน แต่โปรดทราบว่าอาจมีผลที่ตามมาโดยไม่ได้ตั้งใจ

  • Bootstrap มีคลาสที่กำหนดไว้ล่วงหน้าสำหรับการสร้างเลย์เอาต์ที่รวดเร็วและตอบสนอง ด้วยจุดพักหกจุดและหลายสิบคอลัมน์ในแต่ละระดับของกริด เรามีคลาสหลายสิบคลาสที่สร้างไว้แล้วสำหรับคุณเพื่อสร้างเลย์เอาต์ที่คุณต้องการ สามารถปิดการใช้งานผ่าน Sass ได้หากต้องการ

การจัดตำแหน่ง

ใช้ยูทิลิตี้การจัดตำแหน่ง flexbox เพื่อจัดแนวคอลัมน์ในแนวตั้งและแนวนอน

การจัดตำแหน่งแนวตั้ง

หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
html
<div class="container text-center">
  <div class="row align-items-start">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
  <div class="row align-items-center">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
  <div class="row align-items-end">
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
    <div class="col">
      One of three columns
    </div>
  </div>
</div>
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
หนึ่งในสามคอลัมน์
html
<div class="container text-center">
  <div class="row">
    <div class="col align-self-start">
      One of three columns
    </div>
    <div class="col align-self-center">
      One of three columns
    </div>
    <div class="col align-self-end">
      One of three columns
    </div>
  </div>
</div>

การจัดตำแหน่งแนวนอน

หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
หนึ่งในสองคอลัมน์
html
<div class="container text-center">
  <div class="row justify-content-start">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-center">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-end">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-around">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-between">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
  <div class="row justify-content-evenly">
    <div class="col-4">
      One of two columns
    </div>
    <div class="col-4">
      One of two columns
    </div>
  </div>
</div>

การห่อคอลัมน์

หากวางมากกว่า 12 คอลัมน์ภายในแถวเดียว กลุ่มของคอลัมน์พิเศษแต่ละกลุ่มจะรวมเป็นหนึ่งหน่วยในบรรทัดใหม่

.col-9
.col-4
เนื่องจาก 9 + 4 = 13 > 12 div ทั้ง 4 คอลัมน์นี้จึงถูกรวมไว้ในบรรทัดใหม่เป็นหน่วยที่ต่อเนื่องกัน
.col-6
คอลัมน์ที่ตามมาจะดำเนินต่อไปในบรรทัดใหม่
html
<div class="container">
  <div class="row">
    <div class="col-9">.col-9</div>
    <div class="col-4">.col-4<br>Since 9 + 4 = 13 &gt; 12, this 4-column-wide div gets wrapped onto a new line as one contiguous unit.</div>
    <div class="col-6">.col-6<br>Subsequent columns continue along the new line.</div>
  </div>
</div>

ตัวแบ่งคอลัมน์

การแบ่งคอลัมน์เป็นบรรทัดใหม่ใน flexbox จำเป็นต้องมีการแฮ็กเล็กน้อย: เพิ่มองค์ประกอบด้วยwidth: 100%ทุกที่ที่คุณต้องการรวมคอลัมน์ของคุณไว้ในบรรทัดใหม่ โดยปกติสามารถทำได้ด้วยหลาย ๆ.rows แต่ไม่ใช่ทุกวิธีการใช้งานที่สามารถอธิบายได้

.col-6 .col-sm-3
.col-6 .col-sm-3
.col-6 .col-sm-3
.col-6 .col-sm-3
html
<div class="container text-center">
  <div class="row">
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>

    <!-- Force next columns to break to new line -->
    <div class="w-100"></div>

    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
    <div class="col-6 col-sm-3">.col-6 .col-sm-3</div>
  </div>
</div>

คุณยังสามารถใช้ตัวแบ่งนี้ที่จุดสั่งหยุดเฉพาะด้วยยูทิลิตีการแสดงผลที่ตอบสนองของ เรา

.col-6 .col-sm-4
.col-6 .col-sm-4
.col-6 .col-sm-4
.col-6 .col-sm-4
html
<div class="container text-center">
  <div class="row">
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>

    <!-- Force next columns to break to new line at md breakpoint and up -->
    <div class="w-100 d-none d-md-block"></div>

    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
    <div class="col-6 col-sm-4">.col-6 .col-sm-4</div>
  </div>
</div>

จัดเรียงใหม่

สั่งซื้อคลาส

ใช้.order-คลาสเพื่อควบคุมลำดับภาพของเนื้อหาของคุณ คลาสเหล่านี้ตอบสนองได้ ดังนั้นคุณสามารถตั้งค่าorderตามจุดพัก (เช่น.order-1.order-md-2) รวมการสนับสนุน1ผ่าน5ระดับกริดทั้งหก

ที่แรกใน DOM ไม่มีคำสั่งซื้อ
ที่สองใน DOM ด้วยคำสั่งซื้อที่ใหญ่กว่า
อันดับสามใน DOM โดยมีคำสั่งเท่ากับ 1
html
<div class="container text-center">
  <div class="row">
    <div class="col">
      First in DOM, no order applied
    </div>
    <div class="col order-5">
      Second in DOM, with a larger order
    </div>
    <div class="col order-1">
      Third in DOM, with an order of 1
    </div>
  </div>
</div>

นอกจากนี้ยังมีการตอบสนอง.order-firstและ.order-lastคลาสที่เปลี่ยนorderองค์ประกอบโดยการใช้order: -1และorder: 6ตามลำดับ คลาสเหล่านี้ยังสามารถผสมกับ.order-*คลาสที่มีหมายเลขได้ตามต้องการ

อันดับแรกใน DOM สั่งครั้งสุดท้าย
ที่สองใน DOM ไม่มีลำดับ
ที่สามใน DOM สั่งก่อน
html
<div class="container text-center">
  <div class="row">
    <div class="col order-last">
      First in DOM, ordered last
    </div>
    <div class="col">
      Second in DOM, unordered
    </div>
    <div class="col order-first">
      Third in DOM, ordered first
    </div>
  </div>
</div>

คอลัมน์ออฟเซ็ต

คุณสามารถออฟเซ็ตคอลัมน์กริดได้สองวิธี: .offset-คลาสกริดที่ตอบสนองของเรา และยูทิลิตี้มาร์ จิ้นของเรา คลาสกริดมีขนาดให้ตรงกับคอลัมน์ในขณะที่ระยะขอบมีประโยชน์มากกว่าสำหรับเลย์เอาต์ด่วนที่ความกว้างของออฟเซ็ตเป็นตัวแปร

คลาสออฟเซ็ต

ย้ายคอลัมน์ไปทางขวาโดยใช้.offset-md-*คลาส ชั้นเรียนเหล่านี้เพิ่มระยะขอบด้านซ้ายของคอลัมน์ทีละ*คอลัมน์ ตัวอย่างเช่น.offset-md-4ย้าย.col-md-4มากกว่าสี่คอลัมน์

.col-md-4
.col-md-4 .offset-md-4
.col-md-3 .offset-md-3
.col-md-3 .offset-md-3
.col-md-6 .offset-md-3
html
<div class="container text-center">
  <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 offset-md-4">.col-md-4 .offset-md-4</div>
  </div>
  <div class="row">
    <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
    <div class="col-md-3 offset-md-3">.col-md-3 .offset-md-3</div>
  </div>
  <div class="row">
    <div class="col-md-6 offset-md-3">.col-md-6 .offset-md-3</div>
  </div>
</div>

นอกจากการล้างคอลัมน์ที่จุดสั่งหยุดแบบตอบสนองแล้ว คุณอาจต้องรีเซ็ตออฟเซ็ตด้วย ดูการดำเนินการนี้ในตัวอย่างกริด

.col-sm-5 .col-md-6
.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0
.col-sm-6 .col-md-5 .col-lg-6
.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0
html
<div class="container text-center">
  <div class="row">
    <div class="col-sm-5 col-md-6">.col-sm-5 .col-md-6</div>
    <div class="col-sm-5 offset-sm-2 col-md-6 offset-md-0">.col-sm-5 .offset-sm-2 .col-md-6 .offset-md-0</div>
  </div>
  <div class="row">
    <div class="col-sm-6 col-md-5 col-lg-6">.col-sm-6 .col-md-5 .col-lg-6</div>
    <div class="col-sm-6 col-md-5 offset-md-2 col-lg-6 offset-lg-0">.col-sm-6 .col-md-5 .offset-md-2 .col-lg-6 .offset-lg-0</div>
  </div>
</div>

ยูทิลิตี้มาร์จิ้น

เมื่อย้ายไปที่ flexbox ใน v4 คุณสามารถใช้ยูทิลิตี้มาร์จิ้น เช่น.me-autoบังคับให้คอลัมน์พี่น้องออกจากกัน

.col-md-4
.col-md-4 .ms-auto
.col-md-3 .ms-md-auto
.col-md-3 .ms-md-auto
.col-auto .me-auto
.col-auto
html
<div class="container text-center">
  <div class="row">
    <div class="col-md-4">.col-md-4</div>
    <div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
  </div>
  <div class="row">
    <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
    <div class="col-md-3 ms-md-auto">.col-md-3 .ms-md-auto</div>
  </div>
  <div class="row">
    <div class="col-auto me-auto">.col-auto .me-auto</div>
    <div class="col-auto">.col-auto</div>
  </div>
</div>

คลาสคอลัมน์สแตนด์อโลน

คลาส.col-*ยังสามารถใช้ภายนอก a .rowเพื่อให้องค์ประกอบมีความกว้างเฉพาะ เมื่อใดก็ตามที่คลาสของคอลัมน์ถูกใช้เป็นลูกที่ไม่ใช่โดยตรงของแถว ช่องว่างภายในจะถูกละเว้น

.col-3: ความกว้าง 25%
.col-sm-9: ความกว้าง 75% เหนือเบรกพอยต์ sm
html
<div class="col-3 bg-light p-3 border">
  .col-3: width of 25%
</div>
<div class="col-sm-9 bg-light p-3 border">
  .col-sm-9: width of 75% above sm breakpoint
</div>

คลาสสามารถใช้ร่วมกับยูทิลิตี้เพื่อสร้างอิมเมจโฟลตที่ตอบสนองได้ ตรวจสอบให้แน่ใจว่าได้ห่อเนื้อหาใน.clearfixกระดาษห่อเพื่อล้างทุ่นหากข้อความสั้นลง

Placeholder Responsive floated image

ย่อหน้าของตัวแทนข้อความ เรากำลังใช้มันที่นี่เพื่อแสดงการใช้คลาส clearfix เรากำลังเพิ่มวลีที่ไม่มีความหมายบางอย่างที่นี่เพื่อแสดงให้เห็นว่าคอลัมน์โต้ตอบกับภาพที่ลอยอย่างไร

อย่างที่คุณเห็นย่อหน้าล้อมรอบรูปภาพที่ลอยอย่างสวยงาม ทีนี้ลองนึกภาพว่าเนื้อหาจริงจะออกมาเป็นอย่างไรในที่นี้ แทนที่จะเป็นเพียงข้อความในตัวยึดตำแหน่งที่น่าเบื่อซึ่งดำเนินไปเรื่อยๆ แต่จริงๆ แล้วไม่ได้สื่อถึงข้อมูลที่จับต้องได้ มันใช้พื้นที่และไม่ควรอ่านจริงๆ

ถึงกระนั้น คุณยังคงยืนกรานที่จะอ่านข้อความตัวยึดตำแหน่งนี้ โดยหวังว่าจะได้รับข้อมูลเชิงลึกเพิ่มเติม หรือเนื้อหาไข่อีสเตอร์ที่ซ่อนอยู่ เรื่องตลกบางที น่าเสียดายที่ไม่มีที่นี่

html
<div class="clearfix">
  <img src="..." class="col-md-6 float-md-end mb-3 ms-md-3" alt="...">

  <p>
    A paragraph of placeholder text. We're using it here to show the use of the clearfix class. We're adding quite a few meaningless phrases here to demonstrate how the columns interact here with the floated image.
  </p>

  <p>
    As you can see the paragraphs gracefully wrap around the floated image. Now imagine how this would look with some actual content in here, rather than just this boring placeholder text that goes on and on, but actually conveys no tangible information at. It simply takes up space and should not really be read.
  </p>

  <p>
    And yet, here you are, still persevering in reading this placeholder text, hoping for some more insights, or some hidden easter egg of content. A joke, perhaps. Unfortunately, there's none of that here.
  </p>
</div>