Grid and spacing
BS04. 排版系統 Grid system
為了便利使用者設計版面,bootstrap通常以12等分作為切割等寬欄位的基準,並且讓使用者得以利用class來指定他要怎麼切割這12個等寬欄位。
首先要指定一個
<div class="row">
之後在其中再建立其他<div>
.col-sm-4
其中的sm
所指的是這個版面要給平板電腦用的,其他的代號包含xs
for phones、md
for desktops、andlg
for larger desktops。
將一列的版面切割為4:4:4三塊。
將一列的版面切割為4:8兩個不等的區塊。
刻意在左方來作為留白。
若希望在grid排版上能夠左邊空出4,然後只留下右邊8,一來可以用上面那種辦法,先做一個空的.col-sm-4的<div>,再做一個.col-sm-8的<div>。但事實上,如果你始終不會用到左邊那區塊的話,可以用 來製作間隔
<div id="about" class="container-fluid">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-9">
<h2>About us</h2>
<h4>hahaha</h4>
<p>hehehe</p>
</div>
</div>
</div>
參考資料 http://getbootstrap.com/css/
標準寫法:使用offset
<div id="intro" class="container-fluid">
<div class="row">
<div class="col-sm-9 offset-3">
<h2>About us</h2></div>
</div>
</div>
BS04.1 Grid system for RWD
See https://getbootstrap.com/docs/4.0/layout/grid/
Bootstrap在3.0與4.5版分別提供四到五個等級的「斷點」,為預設的不同瀏覽器大小,讓程式設計師可以用指派class,就設定好因應不同螢幕大小的排版。一共分為以下五階層。

// Extra small devices (portrait phones, less than 576px)
@media (max-width: 575px) { ... }
// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) and (max-width: 767px) { ... }
// Medium devices (tablets, 768px and up)
@media (min-width: 768px) and (max-width: 991px) { ... }
// Large devices (desktops, 992px and up)
@media (min-width: 992px) and (max-width: 1199px) { ... }
// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }
當程式碼如以下指定,代表我們希望這些元件在不同螢幕大小時有不一樣的表現
<!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="row">
<div class="col-12 col-md-8">.col-12 .col-md-8</div>
<div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
<div class="row">
<div class="col-6 col-md-4">.col-6 .col-md-4</div>
<div class="col-6 col-md-4">.col-6 .col-md-4</div>
<div class="col-6 col-md-4">.col-6 .col-md-4</div>
</div>
<!-- Columns are always 50% wide, on mobile and desktop -->
<div class="row">
<div class="col-6">.col-6</div>
<div class="col-6">.col-6</div>
</div>
以上面的code而言,那螢幕夠大的時候看起來應該是如下圖,目前是照三個.col-md-4來排版。
若是螢幕太小的話,那就會變成按照.col-6
來排版,那就是兩個col-6
,所以中間那列的元件會排成兩欄如下。
<Practice> #1. 用grid system完成剩下的division
<Practice> #2 Footer
About glyphicon, take a look at http://www.w3schools.com/bootstrap/bootstrap_ref_comp_glyphs.asp
<body id="myPage">
...
<footer class="container-fluid text-center">
<a href="#myPage" title="back to top">
<span class="glyphicon glyphicon-triangle-top"></span>
</a>
<p>Made By ji-lung</p>
</footer>
BS04.1 Layout:clearfix
https://getbootstrap.com/docs/4.0/utilities/clearfix/
主要是提供clear:both
功能的class。
<div class="bg-info clearfix">
<button type="button" class="btn btn-secondary float-left">Example Button floated left</button>
<button type="button" class="btn btn-secondary float-right">Example Button floated right</button>
</div>

BS05. Spacing: Margin and Padding
See https://getbootstrap.com/docs/4.0/utilities/spacing/
Examples
.mt-0 {
margin-top: 0 !important;
}
.ml-1 {
margin-left: ($spacer * .25) !important;
}
.px-2 {
padding-left: ($spacer * .5) !important;
padding-right: ($spacer * .5) !important;
}
.p-3 {
padding: $spacer !important;
}
https://getbootstrap.com/docs/4.0/utilities/spacing/
Where property is one of:
m
- for classes that setmargin
p
- for classes that setpadding
Where sides is one of:
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
orpadding-bottom
l
- for classes that setmargin-left
orpadding-left
r
- for classes that setmargin-right
orpadding-right
x
- for classes that set both*-left
and*-right
y
- for classes that set both*-top
and*-bottom
blank - for classes that set a
margin
orpadding
on all 4 sides of the element
Where size is one of:
0
- for classes that eliminate themargin
orpadding
by setting it to0
1
- (by default) for classes that set themargin
orpadding
to$spacer * .25
2
- (by default) for classes that set themargin
orpadding
to$spacer * .5
3
- (by default) for classes that set themargin
orpadding
to$spacer
4
- (by default) for classes that set themargin
orpadding
to$spacer * 1.5
5
- (by default) for classes that set themargin
orpadding
to$spacer * 3
auto
- for classes that set themargin
to auto
Last updated
Was this helpful?