Bootstrap 4 網格 小號
小號網格示例
超小屏 | 小 | 中 | 大 | 特大尺寸 | |
---|---|---|---|---|---|
類字首 | .col- |
.col-sm- |
.col-md- |
.col-lg- |
.col-xl- |
螢幕寬度 | <576px | >=576px | >=768px | >=992px | >=1200px |
假設我們有一個簡單的兩欄佈局。我們希望對於小號裝置,欄的寬度比例為 25%/75%。
小號裝置定義為螢幕寬度從576畫素到767畫素。
對於小號裝置,我們將使用 .col-sm-*
類。
我們將為我們的兩欄新增以下類
<div class="col-sm-3">....</div>
<div class="col-sm-9">....</div>
現在 Bootstrap 會說:“在小號尺寸下,查詢帶有 -sm- 的類並使用它們”。
以下示例將在小號(以及中號、大號和特大號)裝置上實現 25%/75% 的分割。在超小號裝置上,它們將自動堆疊(100%)
.col-sm-3
.col-sm-9
示例
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 bg-success">
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-9 bg-warning">
<p>Sed ut perspiciatis...</p>
</div>
</div>
</div>
自己動手試一試 »注意: 確保總和為 12 或更少(不要求使用所有 12 個可用列)。
要實現 33.3%/66.6% 的分割,您可以使用 .col-sm-4
和 .col-sm-8
(對於 50%/50% 的分割,您可以使用 .col-sm-6
和 .col-sm-6
)。
.col-sm-4
.col-sm-8
.col-sm-6
.col-sm-6
示例
<!-- 33.3/66.6% 分割: -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-4 bg-success">
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-8 bg-warning">
<p>Sed ut perspiciatis...</p>
</div>
</div>
</div>
<!-- 50%/50% 分割: -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 bg-success">
<p>Lorem ipsum...</p>
</div>
<div class="col-sm-6 bg-warning">
<p>Sed ut perspiciatis...</p>
</div>
</div>
</div>
自己動手試一試 »自動佈局列
在 Bootstrap 4 中,有一種簡單的方法可以為所有裝置建立等寬欄:只需移除 .col-sm-*
中的數字,並在指定的列元素上僅使用 .col-sm
類。Bootstrap 會識別有多少列,並且每列將獲得相同的寬度。
如果螢幕尺寸小於576px,列將水平堆疊。
<!-- 兩欄: 在所有螢幕上寬度為50%,除了超小號螢幕(寬度100%) -->
<div class="row">
<div class="col-sm">1 of 2</div>
<div class="col-sm">2 of 2</div>
</div>
<!-- 四欄: 在所有螢幕上寬度為25%,除了超小號螢幕(寬度100%)-->
<div class="row">
<div class="col-sm">1 of 4</div>
<div class="col-sm">2 of 4</div>
<div class="col-sm">3 of 4</div>
<div class="col-sm">4 of 4</div>
</div>
1 of 2
2 of 2
1 of 4
2 of 4
3 of 4
4 of 4
下一章將展示如何為中號裝置新增不同的分割百分比。