Bootstrap網(wǎng)格系統(tǒng)是創(chuàng)建響應(yīng)式網(wǎng)頁布局的最快,最簡單的方法。
Bootstrap 提供了一套響應(yīng)式、移動(dòng)設(shè)備優(yōu)先的流式網(wǎng)格系統(tǒng),隨著屏幕或視口(viewport)尺寸的增加,系統(tǒng)會自動(dòng)分為最多 12 列。
我們也可以根據(jù)自己的需要,定義列數(shù):
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
4 | 4 | 4 | |||||||||
4 | 8 | ||||||||||
6 | 6 | ||||||||||
12 |
Bootstrap 4 的網(wǎng)格系統(tǒng)是響應(yīng)式的,列會根據(jù)屏幕大小自動(dòng)重新排列。
Bootstrap 4包含預(yù)定義的網(wǎng)格類,可為不同類型的設(shè)備(例如手機(jī),平板電腦,筆記本電腦和臺式機(jī)等)快速制作網(wǎng)格布局。例如,您可以使用這些.col-*類為縱向模式下的超小型設(shè)備創(chuàng)建網(wǎng)格列,同樣,您可以使用這些.col-sm-*類為小屏幕設(shè)備(如橫向模式下的移動(dòng)電話),.col-md-*中型屏幕設(shè)備(如平板電腦),.col-lg-*大型設(shè)備(如臺式機(jī))和.col-xl-*超大臺式機(jī)屏幕的類創(chuàng)建網(wǎng)格列。
Bootstrap 4 網(wǎng)格系統(tǒng)有以下 5 個(gè)類:
.col- 針對所有設(shè)備
.col-sm- 平板 - 屏幕寬度等于或大于 576px
.col-md- 桌面顯示器 - 屏幕寬度等于或大于 768px)
.col-lg- 大桌面顯示器 - 屏幕寬度等于或大于 992px)
.col-xl- 超大桌面顯示器 - 屏幕寬度等于或大于 1200px)
Bootstrap4 網(wǎng)格系統(tǒng)規(guī)則:
網(wǎng)格每一行需要放在設(shè)置了 .container (固定寬度) 或 .container-fluid (全屏寬度) 類的容器中,這樣就可以自動(dòng)設(shè)置一些外邊距與內(nèi)邊距。
使用行來創(chuàng)建水平的列組。
內(nèi)容需要放置在列中,并且只有列可以是行的直接子節(jié)點(diǎn)。
預(yù)定義的類如 .row 和 .col-sm-4 可用于快速制作網(wǎng)格布局。
列通過填充創(chuàng)建列內(nèi)容之間的間隙。 這個(gè)間隙是通過 .rows 類上的負(fù)邊距設(shè)置第一行和最后一列的偏移。
網(wǎng)格列是通過跨越指定的 12 個(gè)列來創(chuàng)建。 例如,設(shè)置三個(gè)相等的列,需要使用用三個(gè).col-sm-4 來設(shè)置。
Bootstrap 3 和 Bootstrap 4 最大的區(qū)別在于 Bootstrap 4 現(xiàn)在使用 flexbox(彈性盒子) 而不是浮動(dòng)。 Flexbox 的一大優(yōu)勢是,沒有指定寬度的網(wǎng)格列將自動(dòng)設(shè)置為等寬與等高列 。 如果您想了解有關(guān)Flexbox的更多信息,可以閱讀我們的CSS Flexbox教程。
下表總結(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 |
以下各個(gè)類可以一起使用,從而創(chuàng)建更靈活的頁面布局。
以下代碼為 Bootstrap 4 網(wǎng)格的基本結(jié)構(gòu):
<!-- 第一個(gè)實(shí)例:控制列的寬度及在不同的設(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è)實(shí)例:或讓 Bootstrap 者自動(dòng)處理布局 --> <div class="row"> <div class="col"></div> <div class="col"></div> <div class="col"></div> </div>
第一個(gè)實(shí)例:創(chuàng)建一行(<div class="row">)。然后, 添加是需要的列(.col-*-* 類中設(shè)置)。 第一個(gè)星號 (*) 表示響應(yīng)的設(shè)備: sm, md, lg 或 xl, 第二個(gè)星號 (*) 表示一個(gè)數(shù)字, 同一行的數(shù)字相加為 12。
第二個(gè)實(shí)例: 不在每個(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ī)則。
接下來我們可以看看示例。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <h1>Hello World!</h1> <p>創(chuàng)建三個(gè)相等寬度的列! 嘗試在 class="row" 的 div 中添加新的 class="col" div,會顯示四個(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>測試看看 ?/?
以下示例演示了如何在平板及更大屏幕上創(chuàng)建等寬度的響應(yīng)式列。 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會上下堆疊排版:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <h1>Hello World!</h1> <p>重置瀏覽器大小查效果。</p> <p> 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會上下堆疊排版。</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>測試看看 ?/?
以下示例演示了在平板及更大屏幕上創(chuàng)建不等寬度的響應(yīng)式列。 在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),兩個(gè)列將會上下堆疊排版:
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <h1>Hello World!</h1> <p>重置瀏覽器大小查效果。</p> <p>在移動(dòng)設(shè)備上,即屏幕寬度小于 576px 時(shí),四個(gè)列將會上下堆疊排版。</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>測試看看 ?/?
以下示例演示了在桌面設(shè)備的顯示器上兩個(gè)列的寬度各占 50%,如果在平板端則左邊的寬度為 25%,右邊的寬度為 75%, 在移動(dòng)手機(jī)等小型設(shè)備上會堆疊顯示。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <h1>平板與桌面的網(wǎng)格布局</h1> <p>以下示例演示了在桌面設(shè)備的顯示器上兩個(gè)列的寬度各占 50%,如果在平板端則左邊的寬度為 25%,右邊的寬度為 75%, 在移動(dòng)手機(jī)等小型設(shè)備上會堆疊顯示。 </p> <p>重置瀏覽器窗口大小,查看效果。</p> <div class="container-fluid"> <div class="row"> <div class="col-sm-3 col-md-6 bg-success"> nhooo </div> <div class="col-sm-9 col-md-6 bg-warning"> 菜鳥教程 </div> </div> </div> </div> </body> </html>測試看看 ?/?
以下示例在平板、桌面、大桌面顯示器、超大桌面顯示器的寬度比例為分別為:25%/75%、50%/50%、33.33%/66.67%、16.67/83.33%, 在移動(dòng)手機(jī)等小型設(shè)備上會堆疊顯示。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> </head> <body> <div class="container-fluid"> <h1>平板、桌面、大桌面顯示器、超大桌面顯示器</h1> <p>以下示例在平板、桌面、大桌面顯示器、超大桌面顯示器的寬度比例為分別為:25%/75%、50%/50%、33.33%/66.67%、16.67/83.33%, 在移動(dòng)手機(jī)等小型設(shè)備上會堆疊顯示。</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"> nhooo </div> <div class="col-sm-9 col-md-6 col-lg-8 col-xl-10 bg-warning"> 菜鳥教程 </div> </div> </div> </div> </body> </html>測試看看 ?/?
偏移列通過 offset-*-* 類來設(shè)置。第一個(gè)星號( * )可以是 sm、md、lg、xl,表示屏幕設(shè)備類型,第二個(gè)星號( * )可以是 1 到 11 的數(shù)字。
為了在大屏幕顯示器上使用偏移,請使用 .offset-md-* 類。這些類會把一個(gè)列的左外邊距(margin)增加 * 列,其中 * 范圍是從 1 到 11。
例如:.offset-md-4 是把.col-md-4 往右移了四列格。
<!DOCTYPE html> <html> <head> <title>Bootstrap 示例</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></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>測試看看 ?/?
我們希望您已經(jīng)了解了新的Bootstrap 4網(wǎng)格系統(tǒng)的基礎(chǔ)知識。在接下來的幾章中,您將學(xué)習(xí)如何使用此flexbox網(wǎng)格系統(tǒng)創(chuàng)建基本的網(wǎng)頁布局。