BootStrap24

花溪月影發表於2024-06-12

一、概念

 一個前端開發的框架,Bootstrap,來自 Twitter,是目前很受歡迎的前端框架。Bootstrap 是基於 HTML、CSS、JavaScript 的,它簡潔靈活,使得 Web 開發更加快捷。

  • 框架:一個半成品軟體,開發人員可以在框架基礎上,在進行開發,簡化編碼。
  • 好處:

    1. 定義了很多的css樣式和js外掛。我們開發人員直接可以使用這些樣式和外掛得到豐富的頁面效果。
    2. 響應式佈局。
        同一套頁面可以相容不同解析度的裝置。

二、快速入門

 1. 下載Bootstrap
 2. 在專案中將這三個資料夾複製

  

  min-壓縮版

  複製資料中的jquery-3.2.1.min.js到js目錄下

 3. 建立html頁面,引入必要的資原始檔

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! -->
        <title>Bootstrap HelloWorld</title>
    
        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <!-- jQuery (Bootstrap 的所有 JavaScript 外掛都依賴 jQuery,所以必須放在前邊) -->
        <script src="js/jquery-3.2.1.min.js"></script>
        <!-- 載入 Bootstrap 的所有 JavaScript 外掛。你也可以根據需要只載入單個外掛。 -->
        <script src="js/bootstrap.min.js"></script>
    </head>
    <body>
    <h1>你好,世界!</h1>
    
    </body>
    </html>

三、響應式佈局

 同一套頁面可以相容不同解析度的裝置。

 1、實現:依賴於柵格系統:將一行平均分成12個格子,可以指定元素佔幾個格子

  

 2、步驟:
  1. 定義容器。相當於之前的table
    容器分類:
        1. container:兩邊留白
        2. container-fluid:每一種裝置都是100%寬度
  2. 定義行。相當於之前的tr。樣式:row
  3. 定義元素。指定該元素在不同的裝置上,所佔的格子數目。樣式:col-裝置代號-格子數目
    裝置代號:
        1. xs:超小螢幕 手機 (<768px):col-xs-12
        2. sm:小螢幕 平板 (≥768px)
        3. md:中等螢幕 桌面顯示器 (≥992px)
        4. lg:大螢幕 大桌面顯示器 (≥1200px)

  注意:
    1. 一行中如果格子數目超過12,則超出部分自動換行。
    2. 柵格類屬性可以向上相容。柵格類適用於與螢幕寬度大於或等於分界點大小的裝置。
    3. 如果真實裝置寬度小於了設定柵格類屬性的裝置程式碼的最小值,會一個元素佔滿一整行。

<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap.min.css">

    <!-- HTML5 shim 和 Respond.js 是為了讓 IE8 支援 HTML5 元素和媒體查詢(media queries)功能 -->
    <!-- 警告:透過 file:// 協議(就是直接將 html 頁面拖拽到瀏覽器中)訪問頁面時 Respond.js 不起作用 -->
    <!--[if lt IE 9]-->
    <!--<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>-->
    <!--<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>-->
    <!--[endif]-->
    <!-- jQuery (Bootstrap 的所有 JavaScript 外掛都依賴 jQuery,所以必須放在前邊) -->
    <script src="js/jquery-3.2.1.min.js"></script>
    <!-- 載入 Bootstrap 的所有 JavaScript 外掛。你也可以根據需要只載入單個外掛。 -->
    <script src="js/bootstrap.min.js"></script>
    <style>
        .inner{
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <!--1.定義容器-->
    <div class="container">
        <!--2.定義行-->
        <div class="row">
            <!--3.定義元素
                        在大顯示器一行12個格子
                        在pad上一行6個各自
            -->
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <div class="col-lg-1 col-sm-2 inner">柵格</div>
            <!--<div class="col-md-4 inner">柵格</div>
            <div class="col-md-4 inner">柵格</div>
            <div class="col-md-4 inner">柵格</div>-->
        </div>
    </div>


</body>
</html>

  

  

  

四、CSS樣式和JS外掛

 1、全域性CSS樣式:

  • 按鈕:class="btn btn-default"
<a class="btn btn-default" href="#" role="button">Link</a>
<button class="btn btn-default" type="submit">Button</button>
<input class="btn btn-default" type="button" value="Input">
<input class="btn btn-default" type="submit" value="Submit">

  

<!-- Standard button -->
<button type="button" class="btn btn-default">(預設樣式)Default</button>

<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="button" class="btn btn-primary">(首選項)Primary</button>

<!-- Indicates a successful or positive action -->
<button type="button" class="btn btn-success">(成功)Success</button>

<!-- Contextual button for informational alert messages -->
<button type="button" class="btn btn-info">(一般資訊)Info</button>

<!-- Indicates caution should be taken with this action -->
<button type="button" class="btn btn-warning">(警告)Warning</button>

<!-- Indicates a dangerous or potentially negative action -->
<button type="button" class="btn btn-danger">(危險)Danger</button>

<!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
<button type="button" class="btn btn-link">(連結)Link</button>

  

  • 圖片:

    class="img-responsive":圖片在任意尺寸寬度都佔100%

<img src="..." class="img-responsive">
  • 圖片形狀
<img src="..." alt="..." class="img-rounded">
<img src="..." alt="..." class="img-circle">
<img src="..." alt="..." class="img-thumbnail">

  

  • 表格

    table
    table-bordered
    table-hover

<table class="table table-bordered table-hover">
  ...
</table>
  • 表單

    給表單項新增:class="form-control"

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <p class="help-block">Example block-level help text here.</p>
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Check me out
    </label>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

  

<form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Sign in</button>
    </div>
  </div>
</form>

  

 2、元件:

  • 導航條
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <link rel="stylesheet" href="css/bootstrap.min.css">

    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Brand</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">One more separated link</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
</body>
</html>

  

  

<nav class="navbar navbar-inverse">
  ...
</nav>

  

  • 分頁條
<nav aria-label="Page navigation">
  <ul class="pagination">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

  

 3、外掛:

  • 輪播圖

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example-generic" data-slide-to="1"></li>
        <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <img src="img/banner_1.jpg" alt="...">
            <div class="carousel-caption">
            </div>
        </div>
        <div class="item">
            <img src="img/banner_2.jpg" alt="...">
            <div class="carousel-caption">
            </div>
        </div>
        <div class="item">
            <img src="img/banner_3.jpg" alt="...">
            <div class="carousel-caption">
            </div>
        </div>
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

五、案例

 

    

<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <link rel="stylesheet" href="css/bootstrap.min.css">

    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
        .paddtop{
            padding-top: 10px;
        }
        .search-input{
            float: left;
            border: 2px solid #ffc900;
            width: 400px;
            height: 35px;
            padding-left: 5px;
            margin-top: 15px;

        }
        .search-btn{
            float: left;
            border: 2px solid #ffc900;
            width: 50px;
            height: 35px;
            background-color: #ffc900;
            text-align: center;
            line-height: 35px;
            margin-top: 15px;
        }
        .jx{
            border-bottom: 2px solid #ffc900;
            padding: 5px;
        }
        .company{
            height: 40px;
            background-color: #ffc900;
            text-align: center;
            line-height:40px ;
            font-size: 8px;
        }
    </style>
</head>
<body>

    <!--1.頁首部分-->
    <header class="container-fluid">
        <div class="row">
            <img src="img/top_banner.jpg" class="img-responsive">
        </div>
        <div class="row paddtop">
            <div class="col-md-3">
                <img src="img/logo.jpg" class="img-responsive">
            </div>
            <div class="col-md-5">
                <input class="search-input" placeholder="請輸入線路名稱">
                <a href="#" class="search-btn">搜尋</a>
            </div>
            <div class="col-md-4">
                <img src="img/hotel_tel.png" class="img-responsive">
            </div>
        </div>
        <!--導航條-->
        <div class="row">
            <nav class="navbar navbar-default">
                <div class="container-fluid">
                    <!-- Brand and toggle get grouped for better mobile display -->
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="#">傳智播客</a>
                    </div>

                    <!-- Collect the nav links, forms, and other content for toggling -->
                    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                        <ul class="nav navbar-nav">
                            <li class="active"><a href="#">JavaEE <span class="sr-only">(current)</span></a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                            <li><a href="#">Android</a></li>
                        </ul>
                    </div><!-- /.navbar-collapse -->
                </div><!-- /.container-fluid -->
            </nav>
        </div>
        <!--輪播圖-->
        <div class="row">
            <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                <!-- Indicators -->
                <ol class="carousel-indicators">
                    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
                </ol>

                <!-- Wrapper for slides -->
                <div class="carousel-inner" role="listbox">
                    <div class="item active">
                        <img src="img/banner_1.jpg" alt="...">
                        <div class="carousel-caption">
                        </div>
                    </div>
                    <div class="item">
                        <img src="img/banner_2.jpg" alt="...">
                        <div class="carousel-caption">
                        </div>
                    </div>
                    <div class="item">
                        <img src="img/banner_3.jpg" alt="...">
                        <div class="carousel-caption">
                        </div>
                    </div>
                </div>

                <!-- Controls -->
                <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
    </header>

    <!--2.主體部分-->
    <div class="container">
        <div class="row jx">
            <img src="img/icon_5.jpg">
            <span>黑馬精選</span>
        </div>
        <div class="row paddtop">
            <div class="col-md-3">
                <div class="thumbnail">
                    <img src="img/jiangxuan_3.jpg">
                    <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                    <font color="red">&yen; 699</font>
                </div>
            </div>
            <div class="col-md-3">
                <div class="thumbnail">
                    <img src="img/jiangxuan_3.jpg">
                    <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                    <font color="red">&yen; 699</font>
                </div>
            </div>
            <div class="col-md-3">
                <div class="thumbnail">
                    <img src="img/jiangxuan_3.jpg">
                    <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                    <font color="red">&yen; 699</font>
                </div>
            </div>
            <div class="col-md-3">
                <div class="thumbnail">
                    <img src="img/jiangxuan_3.jpg">
                    <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                    <font color="red">&yen; 699</font>
                </div>
            </div>
        </div>
        <div class="row jx">
            <img src="img/icon_6.jpg">
            <span>國內遊</span>
        </div>
        <div class="row paddtop">
            <div class="col-md-4">
                <img src="img/guonei_1.jpg">
            </div>
            <div class="col-md-8">
                <div class="row">
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="thumbnail">
                            <img src="img/jiangxuan_3.jpg">
                            <p>上海直飛三亞5天4晚自由行(春節預售+親子/蜜月/休閒遊首選+豪華酒店任選+接送機)</p>
                            <font color="red">&yen; 699</font>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!--3.頁尾部分-->
    <footer class="container-fluid">
        <div class="row">
            <img src="img/footer_service.png" class="img-responsive">
        </div>
        <div class="row company">
            江蘇傳智播客教育科技股份有限公司 版權所有Copyright 2006-2018, All Rights Reserved 蘇ICP備16007882
        </div>
    </footer>
</body>
</html>