Bootstrap擁有一個(gè)強(qiáng)大的移動(dòng)優(yōu)先的網(wǎng)格系統(tǒng),使用該系統(tǒng)可以用來創(chuàng)建各種形狀和尺寸的布局。該系統(tǒng)是基于個(gè)12列的布局,有很多的等級(jí),為每個(gè)媒體查詢范圍創(chuàng)建了一個(gè)等級(jí)。
當(dāng)然,我們也可以根據(jù)自己的需要,來對(duì)列數(shù)進(jìn)行定義,如下:
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
4 | 4 | 4 | |||||||||
4 | 8 | ||||||||||
6 | 6 | ||||||||||
12 |
Bootstrap4 的網(wǎng)格系統(tǒng)是響應(yīng)式的,列會(huì)根據(jù)屏幕大小自動(dòng)重新排列。
Bootstrap 4 網(wǎng)格系統(tǒng)有以下 5 個(gè)類:
1、col- 針對(duì)所有設(shè)備
2、col-sm- 平板 - 屏幕寬度等于或大于 576px
3、col-md- 桌面顯示器 - 屏幕寬度等于或大于 768px)
4、col-lg- 大桌面顯示器 - 屏幕寬度等于或大于 992px)
5、col-xl- 超大桌面顯示器 - 屏幕寬度等于或大于 1200px)
Bootstrap4 網(wǎng)格系統(tǒng)規(guī)則如下:
下表總結(jié)了 Bootstrap 網(wǎng)格系統(tǒng)如何在不同設(shè)備上工作的:
超小設(shè)備 <576px | 平板 ≥576px | 桌面顯示器 ≥768px | 大桌面顯示器 ≥992px | 超大桌面顯示器 ≥1200px | |
---|---|---|---|---|---|
容器最大寬度 | None (auto) | 540px | 720px | 960px | 1140px |
類前綴 | .col- | .col-sm- | .col-md- | .col-lg- | .col-xl- |
列數(shù)量和 | 12 | ||||
間隙寬度 | 30px (一個(gè)列的每邊分別 15px) | ||||
可嵌套 | Yes | ||||
列排序 | Yes |
以下代碼為 Bootstrap 4 網(wǎng)格的基本結(jié)構(gòu):
<!-- 第一個(gè)例子:控制列的寬度及在不同的設(shè)備上如何顯示 -->
<div class="row">
<div class="col-*-*"></div>
</div>
<div class="row">
<div class="col-*-*"></div>
<div class="col-*-*"></div>
<div class="col-*-*"></div>
</div>
<!-- 第二個(gè)例子:或讓 Bootstrap 者自動(dòng)處理布局 -->
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col"></div>
</div>
第一個(gè)例子:創(chuàng)建一行(<div class="row">)。然后, 添加是需要的列( .col-*-* 類中設(shè)置)。 第一個(gè)星號(hào) (*) 表示響應(yīng)的設(shè)備: sm, md, lg 或 xl, 第二個(gè)星號(hào) (*) 表示一個(gè)數(shù)字, 同一行的數(shù)字相加為 12。
第二個(gè)例子: 不在每個(gè) col 上添加數(shù)字,讓 bootstrap 自動(dòng)處理布局,同一行的每個(gè)列寬度相等: 兩個(gè) "col" ,每個(gè)就為 50% 的寬度。三個(gè) "col"每個(gè)就為 33.33% 的寬度,四個(gè) "col"每個(gè)就為 25% 的寬度,以此類推。同樣,你可以使用 .col-sm|md|lg|xl 來設(shè)置列的響應(yīng)規(guī)則。
實(shí)例:
1、創(chuàng)建相等寬度的列,Bootstrap 自動(dòng)布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<p>創(chuàng)建三個(gè)相等寬度的列! 嘗試在 class="row" 的 div 中添加新的 class="col" div,會(huì)顯示四個(gè)等寬的列。</p>
<div class="row">
<div class="col" style="background-color:lavender;">.col</div>
<div class="col" style="background-color:orange;">.col</div>
<div class="col" style="background-color:lavender;">.col</div>
</div>
</div>
</body>
</html>
2、等寬響應(yīng)式列
以下實(shí)例演示了如何在平板及更大屏幕上創(chuàng)建等寬度的響應(yīng)式列。 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會(huì)上下堆疊排版:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<p>重置瀏覽器大小查效果。</p>
<p> 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會(huì)上下堆疊排版。</p>
<div class="row">
<div class="col-sm-3" style="background-color:lavender;">.col-sm-3</div>
<div class="col-sm-3" style="background-color:lavenderblush;">.col-sm-3</div>
<div class="col-sm-3" style="background-color:lavender;">.col-sm-3</div>
<div class="col-sm-3" style="background-color:lavenderblush;">.col-sm-3</div>
</div>
</div>
</body>
</html>
3、不等寬響應(yīng)式列
以下實(shí)例演示了在平板及更大屏幕上創(chuàng)建不等寬度的響應(yīng)式列。 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會(huì)上下堆疊排版:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<p>重置瀏覽器大小查效果。</p>
<p>在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會(huì)上下堆疊排版。</p>
<div class="row">
<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>
<div class="col-sm-8" style="background-color:lavenderblush;">.col-sm-8</div>
</div>
</div>
</body>
</html>
4、平板和桌面端
以下實(shí)例演示了在桌面設(shè)備的顯示器上兩個(gè)列的寬度各占 50%,如果在平板端則左邊的寬度為 25%,右邊的寬度為 75%, 在移動(dòng)手機(jī)等小型設(shè)備上會(huì)堆疊顯示。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>平板與桌面的網(wǎng)格布局</h1>
<p>以下實(shí)例演示了在桌面設(shè)備的顯示器上兩個(gè)列的寬度各占 50%,如果在平板端則左邊的寬度為 25%,右邊的寬度為 75%, 在移動(dòng)手機(jī)等小型設(shè)備上會(huì)堆疊顯示。
</p>
<p>重置瀏覽器窗口大小,查看效果。</p>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-6 bg-success">
W3Cschool
</div>
<div class="col-sm-9 col-md-6 bg-warning">
學(xué)編程,上W3Cschool
</div>
</div>
</div>
</div>
</body>
</html>
5、平板、桌面、大桌面顯示器、超大桌面顯示器
以下實(shí)例在平板、桌面、大桌面顯示器、超大桌面顯示器的寬度比例為分別為:25%/75%、50%/50%、33.33%/66.67%、16.67/83.33%, 在移動(dòng)手機(jī)等小型設(shè)備上會(huì)堆疊顯示。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>平板、桌面、大桌面顯示器、超大桌面顯示器</h1>
<p>以下實(shí)例在平板、桌面、大桌面顯示器、超大桌面顯示器的寬度比例為分別為:25%/75%、50%/50%、33.33%/66.67%、16.67/83.33%, 在移動(dòng)手機(jī)等小型設(shè)備上會(huì)堆疊顯示。</p>
<p>重置瀏覽器窗口大小,查看效果。</p>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-6 col-lg-4 col-xl-2 bg-success">
W3Cschool
</div>
<div class="col-sm-9 col-md-6 col-lg-8 col-xl-10 bg-warning">
學(xué)編程,上W3Cschool
</div>
</div>
</div>
</div>
</body>
</html>
6、偏移列
偏移列通過 offset-*-* 類來設(shè)置。第一個(gè)星號(hào)( * )可以是 sm、md、lg、xl,表示屏幕設(shè)備類型,第二個(gè)星號(hào)( * )可以是 1 到 11 的數(shù)字。
為了在大屏幕顯示器上使用偏移,請(qǐng)使用 .offset-md-* 類。這些類會(huì)把一個(gè)列的左外邊距(margin)增加 * 列,其中 * 范圍是從 1 到 11。
例如:.offset-md-4 是把.col-md-4 往右移了四列格。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
<script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></script>
</head>
<body>
<div class="container-fluid">
<h1>偏移列</h1>
<p>.offset-md-4 是把.col-md-4 往右移了四列格。</p>
<div class="container-fluid">
<div class="row">
<div class="col-md-4 bg-success">.col-md-4</div>
<div class="col-md-4 offset-md-4 bg-warning">.col-md-4 .offset-md-4</div>
</div>
<div class="row">
<div class="col-md-3 offset-md-3 bg-success">.col-md-3 .offset-md-3</div>
<div class="col-md-3 offset-md-3 bg-warning">.col-md-3 .offset-md-3</div>
</div>
<div class="row">
<div class="col-md-6 offset-md-3 bg-success">.col-md-6 .offset-md-3</div>
</div>
</div>
</div>
</body>
</html>
以上就是Bootstrap4 網(wǎng)格系統(tǒng)的相關(guān)介紹了,希望對(duì)各位小伙伴有所幫助。
更多建議: