06 前端之Bootstrap框架

Panda_Xin發表於2022-02-16

前端之Bootstrap框架

一、簡介

Bootstrap 是最受歡迎的 HTML、CSS 和 JS 框架,用於開發響應式佈局、移動裝置優先的 WEB 專案。
該框架是很多前端框架的鼻祖 學會它幾乎可以熟悉很多框架的原理

二、引入方式

地址:https://v3.bootcss.com/
bootstrap需要依賴於jQuery才能正常執行(動態效果)
PS: 第一次使用該框架的時候最好採用本地匯入的方式,讓pycharm記住bootstrap的關鍵字。

本地引入(最完整的)

1.引入jQuery
2.引入bootstrap的css檔案
3.引入bootstrap的js檔案

將下載完的資料夾拖到pycharm程式設計專案中即可。(推薦3.4版本非常穩定)
image

CDN引入

1.引入jQuery CDN
2.引入bootstrap css的 CDN
3.引入bootstrap js的 CDN
匯入到HTML檔案中的<head>即可,如下圖

image

三、佈局容器

下面進入第一個重要知識點,學會後就可以自己搭建頁面了。
首先了解下多數瀏覽頁面的左右兩側都有空白區域————稱謂左右留白

container    左右留白
container-fluid   左右不留白

image
實際效果圖:
image

四、柵格系統

Bootstrap 提供了一套響應式、移動裝置優先的流式柵格系統,隨著螢幕或視口(viewport)尺寸的增加,系統會自動分為最多12列,即響應式佈局
該系統能夠自動監測瀏覽器視窗大小,從而根據大小來進行變化。
image
引數及使用:

row      行  # 一個row就是一行 一行是固定的12份
col-md-1    佔幾份
col-sm-1    佔幾份
col-xs-1    佔幾份
col-lg-1    佔幾份

實測:
程式碼:

點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <style>
      .c1{
          background-color: red;
          height: 200px;
          border: 5px solid black;
      }
    </style>


</head>
<body>
    <div class="container">
      <div class="row"></div>
        <div class="col-lg-6 c1"></div>
        <div class="col-lg-6 c1"></div>
    </div>
</body>
</html>

實際效果圖:
image
這樣只是在網頁端是分散式佈局,但是切換到移動端就變了,所以需要增加程式碼。

<div class="col-lg-6 c1 col-sm-4"></div>
<div class="col-lg-6 c1 col-sm-8"></div>
我這裡將移動端頁面改為4、8分成。

image
以下就是柵格系統需要掌握的程式碼引數
image
PS:對於小白來說,掌握這些就足夠了!

五、列偏移

列偏移:就是將任意份數的一行,進行向左或向右移動的操作。

col-md-offset-3 向右移動3份

image

六、表格與表單

6.1表格

編寫一個表格,如下摺疊程式碼。

點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            // 向右移動3份,居中
            <div class="col-md-6 col-md-offset-3">
                // 文字直接text-center即可。
                <h2 class="text-center">使用者資料表單</h2>
                // 新增表格的樣式,用著三個即可。
                <table class="table table-hover table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Age</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>1</td>
                            <td>張三</td>
                            <td>18</td>
                        </tr>
                    </tbody>
                    <tbody>
                        <tr>
                            <td>2</td>
                            <td>李四</td>
                            <td>18</td>
                        </tr>
                    </tbody>
                    <tbody>
                        <tr>
                            <td>3</td>
                            <td>王五</td>
                            <td>18</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>
使用框架調整頁面樣式一般都是操作標籤的class屬性。
即table中的class,新增了多種樣式。
class = "table"
table-hover  懸浮
table-bordered  增加邊框
table-striped  增加背景顏色

實際效果圖如下:
image
增加顏色後的body程式碼如下:

body程式碼
<body>
    <div class="container">
        <div class="row">
<!--            向右移動3份,居中-->
            <div class="col-md-6 col-md-offset-3">
<!--                文字直接text-center即可。-->
                <h2 class="text-center">使用者資料表單</h2>
<!--                新增表格的樣式,用著三個即可。-->
                <table class="table table-hover table-bordered table-striped">
                    <thead>
                        <tr class="active">
                            <th>ID</th>
                            <th>Name</th>
                            <th>class="active"</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="success">
                            <td>1</td>
                            <td>張三</td>
                            <td>class="success"</td>
                        </tr>
                    </tbody>
                    <tbody>
                        <tr class="info">
                            <td>2</td>
                            <td>李四</td>
                            <td>class="info"</td>
                        </tr>
                    </tbody>
                    <tbody>
                        <tr class="warning">
                            <td>3</td>
                            <td>王五</td>
                            <td>class="warning"</td>
                        </tr>
                    </tbody>
                    <tbody>
                        <tr class="danger">
                            <td>4</td>
                            <td>趙六</td>
                            <td>class="danger"</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>

實際效果圖:
image

6.2表單form

在form表單的優化頁面中,只需記住下面一行程式碼即可。

<input type="text" class="form-control"  disabled>
form-control 優化頁面
disbaled 禁止其與使用者有任何互動

input輸入框內陰影提示:

點選檢視程式碼
<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputEmail3">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail3" placeholder="Email">
  </div>
  <div class="form-group">
    <label class="sr-only" for="exampleInputPassword3">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword3" placeholder="Password">
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Remember me
    </label>
  </div>
  <button type="submit" class="btn btn-default">Sign in</button>
</form>

實際效果圖如下:
image

七、按鈕

為 a、button 或 input 元素新增按鈕類(button class)即可使用 Bootstrap 提供的樣式。
想讓誰變成按鈕,就在他標籤內新增class = "btn" 即可。

預定義樣式

預定義樣式程式碼
<!-- 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>

實際效果圖如下:
image

八、元件

image

如何使用

複製圖示下面的程式碼

不要和其他元件混合使用
圖示類不能和其它元件直接聯合使用。它們不能在同一個元素上與其他類共同存在。應該建立一個巢狀的 <span> 標籤,並將圖示類應用到這個 <span> 標籤上。

只對內容為空的元素起作用
圖示類只能應用在不包含任何文字內容或子元素的元素上。

綜上以及學習bootstrap官網內容後,娛樂的程式碼如下:

點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="bootstrap-3.4.1-dist/css/bootstrap-theme.min.css">
    <script src="bootstrap-3.4.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
  <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>
<div class="panel panel-primary">
  <div class="panel-heading">
    <h3 class="panel-title">標題</h3>
  </div>
  <div class="panel-body">
    <div class="jumbotron">
  <h1>Hello, world!</h1>
  <p>...</p>
  <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
  </div>
</div>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>












    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="row">
                  <div class="col-xs-6 col-md-3">
                    <a href="#" class="thumbnail">
                      <img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
                    </a>
                  </div>
                  <div class="col-xs-6 col-md-3">
                    <a href="#" class="thumbnail">
                      <img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
                    </a>
                  </div>
                  <div class="col-xs-6 col-md-3">
                    <a href="#" class="thumbnail">
                      <img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
                    </a>
                  </div>
                  <div class="col-xs-6 col-md-3">
                    <a href="#" class="thumbnail">
                      <img src="https://img1.baidu.com/it/u=1875739781,4152007440&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=576" alt="...">
                    </a>
                  </div>
                </div>

                <div class="progress">
                  <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 45%">
                    <span class="sr-only">45% Complete</span>
                  </div>
                </div>
            <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="..." alt="...">
      <div class="carousel-caption">
        ...
      </div>
    </div>
    <div class="item">
      <img src="..." 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 class="row">
                  <div class="col-sm-6 col-md-4">
                    <div class="thumbnail">
                      <img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                    <div class="col-sm-6 col-md-4">
                    <div class="thumbnail">
                      <img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                    <div class="col-sm-6 col-md-4">
                    <div class="thumbnail">
                      <img src="https://img0.baidu.com/it/u=3112218522,2073304928&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1026" alt="...">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                </div>



                <a href="" class="btn btn-primary">點我</a>
                <input type="reset" class="btn btn-info">
                <input type="reset" class="btn btn-info btn-sm">
                <input type="reset" class="btn btn-info btn-lg">
                <input type="reset" class="btn btn-info btn-block">

                
                <h2 class="text-center">使用者資料 <span class="glyphicon glyphicon-heart"></span></h2>
                <table class="table table-hover table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>NAME</th>
                            <th>PWD</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="success">
                            <td>1</td>
                            <td>jason</td>
                            <td>123</td>
                        </tr>
                    <tr class="danger">
                            <td>2</td>
                            <td>kevin</td>
                            <td>321</td>
                        </tr>
                    <tr class="warning">
                            <td>3</td>
                            <td>tony</td>
                            <td>222</td>
                        </tr>
                    </tbody>
                    <tbody></tbody>
                </table>
                <h2 class="text-center">使用者註冊 <i class="fa fa-bath" aria-hidden="true" style="color: red"></i></h2>
                <form action="">
                    <p>username:
                        <input type="text" class="form-control" value="jason" disabled>
                    </p>
                    <p>password:
                        <input type="text" class="form-control">
                    </p>
                    <p>
                        <input type="radio">男
                        <input type="radio">女
                        <input type="radio">其他
                    </p>
                    <p>
                        <select name="" id="" class="form-control">
                            <option value="">111</option>
                            <option value="">222</option>
                            <option value="">333</option>
                        </select>

                    </p>
                    <input type="submit"  class="btn btn-danger btn-block">
                </form>

                <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>

                <i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>

                <i class="fa fa-refresh fa-spin fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>

                <i class="fa fa-cog fa-spin fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>

                <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
                <span class="sr-only">Loading...</span>

                <img src="..." alt="..." class="img-rounded">
                <img src="..." alt="..." class="img-circle">
                <img src="..." alt="..." class="img-thumbnail">
            </div>
            
        </div>
    </div>

</body>
</html>

相關文章