Bootstrap 5 網格 - 中等螢幕
中等螢幕網格示例
超小號 | 小 | 中 | 大 | 特大尺寸 | XXL | |
---|---|---|---|---|---|---|
類字首 | .col- |
.col-sm- |
.col-md- |
.col-lg- |
.col-xl- |
.col-xxl- |
螢幕寬度 | <576px | >=576px | >=768px | >=992px | >=1200px | >=1400px |
在上一個章節中,我們展示了一個帶有小螢幕裝置的網格示例。我們使用了兩個 div(列),並將它們分成了 25%/75% 的比例。
<div class="col-sm-3">....</div>
<div class="col-sm-9">....</div>
但在中等螢幕裝置上,設計可能最好是 50%/50% 的比例。
中等螢幕裝置定義為螢幕寬度在 768 畫素到 991 畫素之間。
對於中等螢幕裝置,我們將使用 .col-md-*
類。
<div class="col-sm-3 col-md-6">....</div>
<div class="col-sm-9 col-md-6">....</div>
現在 Bootstrap 將會這樣理解:“在小尺寸螢幕上,檢視帶有 -sm- 的類並使用它們。在中等尺寸螢幕上,檢視帶有 -md- 的類並使用它們。”
以下示例在小尺寸螢幕上將產生 25%/75% 的分割,在中等(以及大、超大、XXL)螢幕上將產生 50%/50% 的分割。在特小尺寸螢幕上,它們將自動堆疊(100% 寬度)。
.col-sm-3 .col-md-6
.col-sm-9 .col-md-6
示例
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-6">
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-9 col-md-6">
<p>Sed ut perspiciatis...</p>
</div>
</div>
</div>
自己動手試一試 »
注意: 確保總和為 12 或更少(不要求使用所有 12 個可用列)。
只使用中等螢幕
在下面的示例中,我們只指定了 .col-md-6
類(沒有 .col-sm-*
)。這意味著中等、大、超大和 XXL 螢幕將以 50%/50% 的比例分割 - 因為該類會向上相容。但是,對於小和特小尺寸螢幕,它們將垂直堆疊(100% 寬度)。
示例
<div class="row">
<div class="col-md-6">
<p>Lorem ipsum...</p>
</div>
<div class="col-md-6">
<p>Sed ut perspiciatis...</p>
</div>
</div>
自己動手試一試 »自動佈局列
在 Bootstrap 5 中,有一種簡單的方法可以為所有裝置建立等寬的列:只需移除 .col-md-*
中的數字,而只在指定的 col 元素 上使用 .col-md
類。Bootstrap 將識別有多少列,並且每列將獲得相同的寬度。
如果螢幕尺寸小於 768px,列將水平堆疊。
<!-- 兩列:中等及以上螢幕寬度為 50% -->
<div class="row">
<div class="col-md">1/2</div>
<div class="col-md">2/2</div>
</div>
<!-- 四列:中等及以上螢幕寬度為 25% -->
<div class="row">
<div class="col-md">1/4</div>
<div class="col-md">2/4</div>
<div class="col-md">3/4</div>
<div class="col-md">4/4</div>
</div>
1 of 2
2 of 2
1 of 4
2 of 4
3 of 4
4 of 4
下一章將展示如何為大螢幕裝置新增不同的分割百分比。