[BS4] Bootstrap 4 Layout
在 Bootstrap4 的 Layout 使用的是 CSS flexbox 來達到排版的效果,因此建議可以先瞭解 flexbox 屬性的,flex-container 和 flex-items 的意義,將能夠更快理解 Bootstrap4。
以下內容整理自 Bootstrap 官方網站 Grid System。羅列我認為幾個比較重要的概念,如果時間允許的話,建議還是閱讀官方文件。 Bootstrap 4 Gird Examples @ official
Grid
自訂 Grid 寬度
// custom col-*
// set the width of flex-basis and max-width
.col-custom {
  flex: 0 0 25%;
  max-width: 25%;
  position: relative;
  width: 100%;
  min-height: 1px;
  padding-right: 15px;
  padding-left: 15px;
}
Grid Column 的使用
<!--  html  -->
.container>.row>.col-sm{lorem}*3
- Container 提供基本的方式來置中你的內容,使用 .container當你的容器寬度是固定的;如果想要滿版寬,則使用.container-fluid。
- Rows 是列的意思,用來包住裡面的 Columns 以確保每欄可以排列適當。
- 內容則是放在 Columns 裡面,也就是 .row>.col-sm{內容}這樣的結構
- 網格等級基於 min-width,意味著它們適用於一個大於等於它的寬度(例如,.col-sm-4 適用於小型、中型、大型、和超大型設備)。
- 預設共有 12 個柵格
- 一共可以設定 5 種不同的斷點(breakpoints)
| Size | BreakPoint | Class Prefix | 
|---|---|---|
| Extra small | < 576px | .col- | 
| Small | ≥ 576px | .col-sm- | 
| Medium | ≥ 768px | .col-md- | 
| Large | ≥ 992px | .col-lg- | 
| Extra large | ≥ 1200px | .col-xl- | 
在不同螢幕尺寸上有不同寬度
keywords: .col-{size}-{number}
我們可以在同一個 column 上給多的 class 來讓頁面在不同螢幕尺寸時有不同的排版:
<!--
  - 當螢幕尺寸為 md 以上時會有 8:4 來顯示
  - 當螢幕尺寸為 sm 時會以 4:8 來顯示
  - 當螢幕尺寸小於 sm 時會換行顯示
-->
<div class="row">
  <div class="col-sm-4 col-md-8">.col-sm-4 .col-md-8</div>
  <div class="col-sm-8 col-md-4">.col-sm-8 .col-md-4</div>
</div>
所有裝置都使用相同的 column 的寬度
keywords: .col-{number}
如果你希望能夠自行設定寬度,而且套用在所有尺寸的話,可以使用 .col-{number}:
<div class="row">
  <div class="col-8">col-8</div>
  <div class="col-4">col-4</div>
</div>
在特定裝置尺寸以上使用等寬,其餘垂直並列
keywords: .col-{size}
如果你希望在 lg 以上時套用 flex 讓項目等寬水平排列,當在 lg 以下時,則垂直排列的話,可以使用 .col-{size}:
.row
  .col-lg
  .col-lg
  .col-lg
讓各 item 在所有裝 置均等寬
keywords: .col
如果希望 .row 中的每一個 col 都等寬的話,可以不用加上數字,每個 col 會自動等寬(在所有的螢幕尺寸均等寬):
// pug
.container>.row>.col*4