主頁 > 企業開發 > Bootstrap 滾動監聽+小工具+Flex(彈性)布局+多媒體物件

Bootstrap 滾動監聽+小工具+Flex(彈性)布局+多媒體物件

2020-09-15 10:03:52 企業開發

Bootstrap 滾動監聽

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        body{
            margin:10px;
            padding:10px;
        }
        .navbar-item{flex:1;}
    </style>
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="50" style="position: relative;">
    <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
        <ul class="navbar-nav" style="width:100%;">
            <li class="nav-item">
                <a href="#item1" class="nav-link">item1</a>
            </li>
            <li class="nav-item">
                <a href="#item2" class="nav-link">item2</a>
            </li>
            <li class="nav-item">
                <a href="#item3" class="nav-link">item3</a>
            </li>
            <li class="nav-item">
                <a href="#item4" class="nav-link">item4</a>
            </li>
        </ul>
    </nav>

    <div id="item1" class="container-fluid bg-success" style="padding:70px 0">
        <h1>Section 1</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item2" class="container-fluid bg-warning" style="padding:70px 0">
        <h1>Section 2</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item3" class="container-fluid bg-danger" style="padding:70px 0">
        <h1>Section 3</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item4" class="container-fluid bg-info" style="padding:70px 0">
        <h1>Section 4</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>


</body>
</html>

 

 

 

把滾動監聽加在body標簽上,是正常的

當我嘗試加在某個div上時,發現滾動監聽似乎無效了

各位有知道原因的么

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        body{
            margin:10px;
            padding:10px;
        }
        .navbar-item{flex:1;}
    </style>
</head>
<body>

<div data-spy="scroll" data-target=".navbar" data-offset="50" style="width:300px;margin:0 auto;position: relative;">
    <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top" style="width:300px;margin:0 auto;">
        <ul class="navbar-nav" style="width:100%;">
            <li class="nav-item">
                <a href="#item1" class="nav-link">item1</a>
            </li>
            <li class="nav-item">
                <a href="#item2" class="nav-link">item2</a>
            </li>
            <li class="nav-item">
                <a href="#item3" class="nav-link">item3</a>
            </li>
            <li class="nav-item">
                <a href="#item4" class="nav-link">item4</a>
            </li>
        </ul>
    </nav>

    <div id="item1" class="container-fluid bg-success" style="padding:70px 0">
        <h1>Section 1</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item2" class="container-fluid bg-warning" style="padding:70px 0">
        <h1>Section 2</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item3" class="container-fluid bg-danger" style="padding:70px 0">
        <h1>Section 3</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>
    <div id="item4" class="container-fluid bg-info" style="padding:70px 0">
        <h1>Section 4</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
    </div>

</div>


</body>
</html>

 

 

 

垂直滾動監聽:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        .nav-pills{
            position:fixed;
            top:20px;
        }
        .col-8 div{
            height:800px;
        }
    </style>
</head>
<body data-spy="scroll" data-target="#navbar" data-offset="1" style="position: relative;">
    <div class="container-fluid">
        <div class="row">
            <nav class="col-4" id="navbar">
                <ul class="nav nav-pills flex-column">
                    <li class="nav-item">
                        <a href="#item1" class="nav-link">item1</a>
                    </li>
                    <li class="nav-item">
                        <a href="#item2" class="nav-link">item2</a>
                    </li>
                    <li class="nav-item">
                        <a href="#item3" class="nav-link">item3</a>
                    </li>
                    <li class="nav-item">
                        <a href="#item4" class="nav-link">item4</a>
                    </li>
                </ul>
            </nav>

            <div class="col-8">
                <div id="item1" class="container-fluid bg-success" style="padding:70px 0">
                    <h1>Section 1</h1>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                </div>
                <div id="item2" class="container-fluid bg-warning" style="padding:70px 0">
                    <h1>Section 2</h1>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                </div>
                <div id="item3" class="container-fluid bg-danger" style="padding:70px 0">
                    <h1>Section 3</h1>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                </div>
                <div id="item4" class="container-fluid bg-info" style="padding:70px 0">
                    <h1>Section 4</h1>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                    <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

 

 

 

 

Bootstrap4 小工具

使用 border 類可以添加或移除邊框:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        div{
            display: inline-block;
            width:100px;
            height:100px;
            margin:20px;
        }
    </style>
</head>
<body>
    <div class="border"></div>
    <div class="border-0"></div>
    <div class="border border-top-0"></div>
    <div class="border border-right-0"></div>
    <div class="border border-bottom-0"></div>
    <div class="border border-left-0"></div>

</body>
</html>

 

 

 

Bootstrap4 提供了一些類來設定邊框顏色:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        div{
            display: inline-block;
            width:100px;
            height:100px;
            margin:20px;
        }
    </style>
</head>
<body>
    <div class="border border-primary">primary</div>
    <div class="border border-secondary">secondary</div>
    <div class="border border-info">info</div>
    <div class="border border-warning">warning</div>
    <div class="border border-danger">danger</div>
    <div class="border border-success">success</div>
    <div class="border border-light">light</div>
    <div class="border border-dark">dark</div>
    <div class="border border-white">white</div>

</body>
</html>

 

 

 

使用rounded 類可以添加圓角邊框:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        div{
            display: inline-block;
            width:100px;
            height:100px;
            margin:20px;
        }
    </style>
</head>
<body>
    <div class="rounded border">rounded</div>
    <div class="rounded-top border">rounded-top</div>
    <div class="rounded-bottom border">rounded-bottom</div>
    <div class="rounded-left border">rounded-left</div>
    <div class="rounded-right border">rounded-right</div>
    <div class="rounded-circle border">rounded-circle</div>
    <div class="rouded-0 border">rouded-0</div>

</body>
</html>

 

 

 

.float-right 類用于設定元素右浮動, .float-left 設定元素左浮動, .clearfix 類用于清除浮動:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        .box{
            width:100px;
            height:100px;
        }
    </style>
</head>
<body>
    <div class="clearfix">
        <div class="box border float-left">float-left</div>
        <div class="box border float-right">float-right</div>
    </div>


</body>
</html>

 

 

設定浮動 (.float-*-left|right - * 為 sm, md, lg 或 xl)的方向依賴于螢屏的大小:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        .box{
            width:100px;
            height:100px;
        }
    </style>
</head>
<body>
    <!-- 設定回應式浮動 -->
    <div class="clearfix">
        <div class="box border float-md-right">在大于中等螢屏尺寸上右浮動</div>
    </div>

</body>
</html>

 

使用 .mx-auto 類來設定居中對齊:

<div class="box mx-auto bg-warning">div</div>

 

 

 

使用 w-* 類 (.w-25, .w-50, .w-75, .w-100, .mw-100) 來設定寬度:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        div{
            margin-bottom:10px;
        }
    </style>
</head>
<body>
    <div class="w-25 bg-warning">25%</div>
    <div class="w-50 bg-warning">50%</div>
    <div class="w-100 bg-warning">100%</div>
    <div class="mw-80 bg-warning">最大80%</div>
</body>
</html>

最大寬度沒生效,是這么用的么??

 

使用 h-* 類 (.h-25, .h-50, .h-75, .h-100, .mh-100) 來設定高度:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        .container{
            height:300px;
        }
        div{
            margin-right:10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="float-left h-25 bg-warning">25%</div>
        <div class="float-left h-50 bg-warning">50%</div>
        <div class="float-left h-100 bg-warning">100%</div>
        <div class="float-left mh-80 bg-warning" style="height:300px;">最大80%</div>
    </div>
</body>
</html>

 

 

 

不是很懂這個最大寬度和最大高度的原理……

 

Bootstrap 4 Flex(彈性)布局

注意:IE9 及其以下版本不支持彈性盒子,所以如果你需要兼容 IE8-9,請使用 Bootstrap 3

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning">bg-warning</div>
        <div class="p-2 bg-info">bg-info</div>
        <div class="p-2 bg-danger">bg-danger</div>
    </div>

 

 

  

創建顯示在同一行上的彈性盒子容器可以使用 d-inline-flex 類:

    <div class="d-inline-flex p-3 bg-dark">
        <div class="p-2 bg-warning">bg-warning</div>
        <div class="p-2 bg-info">bg-info</div>
        <div class="p-2 bg-danger">bg-danger</div>
    </div>

 

 

 

.flex-row 可以設定彈性子元素水平顯示,這是默認的,

使用 .flex-row-reverse 類用于設定右對齊顯示,即與 .flex-row 方向相反,

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-row">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-row-reverse">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

 

 

 

 

.flex-column 類用于設定彈性子元素垂直方向顯示, .flex-column-reverse 用于翻轉子元素:

    <div class="d-flex p-3 bg-dark flex-column">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-column-reverse">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

 

 

 

.justify-content-* 類用于修改彈性子元素的排列方式,* 號允許的值有:start (默認), end, center, between 或 around:

    <div class="d-flex p-3 bg-dark justify-content-start">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark justify-content-end">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark justify-content-center">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark justify-content-between">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark justify-content-around">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box1</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

 

 

 

.flex-fill 類強制設定各個彈性子元素的寬度是一樣的:(填滿剩余空間)

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning flex-fill">flex box1</div>
        <div class="p-2 bg-info flex-fill">flex box2</div>
        <div class="p-2 bg-danger flex-fill">flex box3</div>
    </div>

 

 

 

.flex-grow-1 用于設定子元素使用剩下的空間,以下實體中兩個子元素只設定了它們所需要的空間,中間一個獲取剩余空間,

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info flex-grow-1">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

 

 

 

使用 .flex-shrink-1 用于設定子元素的收縮規則,

 

.order 類可以設定彈性子元素的排序,從 .order-1 到 .order-12,數字越低權重越高( .order-1 排在 .order-2 之前) :

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning order-2">flex box1</div>
        <div class="p-2 bg-info order-3">flex box2</div>
        <div class="p-2 bg-danger order-1">flex box3</div>
    </div>

 

 

.mr-auto 類可以設定子元素右外邊距為 auto,即 margin-right: auto!important;,.ml-auto 類可以設定子元素左外邊距為 auto,即 margin-left: auto!important;:

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 mr-auto bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
    </div>

    <div class="d-flex p-3 bg-dark">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 ml-auto bg-danger">flex box3</div>
    </div>

 

 

彈性容器中包裹子元素可以使用以下三個類: .flex-nowrap (默認), .flex-wrap 或 .flex-wrap-reverse,設定 flex 容器是單行或者多行

    <div class="d-flex p-3 bg-dark flex-wrap">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-wrap-reverse">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-nowrap">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

 

 

使用 .align-content-* 來控制在垂直方向上如何去堆疊子元素,包含的值有:.align-content-start (默認), .align-content-end, .align-content-center, .align-content-between, .align-content-around 和 .align-content-stretch,

這些類在只有一行的彈性子元素中是無效的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        .d-flex{height:200px;}
    </style>
</head>
<body>

    <div class="d-flex p-3 bg-dark flex-wrap align-content-start border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-wrap align-content-end border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-wrap align-content-center border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-wrap align-content-around border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark flex-wrap align-content-stretch border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>




</body>
</html>

 

 

 

如果要設定單行的子元素對齊可以使用 .align-items-* 類來控制,包含的值有:.align-items-start, .align-items-end, .align-items-center, .align-items-baseline, 和 .align-items-stretch (默認)

    <div class="d-flex p-3 bg-dark align-items-start border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark align-items-end border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark align-items-center border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark align-items-around border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark align-items-stretch border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

 

 

如果要設定指定子元素對齊對齊可以使用 .align-self-* 類來控制,包含的值有:.align-self-start, .align-self-end, .align-self-center, .align-self-baseline, 和 .align-self-stretch (默認),

    <div class="d-flex p-3 bg-dark border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger align-self-start">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger align-self-end">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger align-self-center">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger align-self-around">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

    <div class="d-flex p-3 bg-dark border border-white">
        <div class="p-2 bg-warning">flex box1</div>
        <div class="p-2 bg-info">flex box2</div>
        <div class="p-2 bg-danger align-self-stretch">flex box3</div>
        <div class="p-2 bg-warning">flex box4</div>
        <div class="p-2 bg-info">flex box5</div>
        <div class="p-2 bg-danger">flex box6</div>
    </div>

 

 

回應式flex類:

可以根據不同的設備,設定 flex 類,從而實作頁面回應式布局,以下表格中的 * 號可以的值有:sm, md, lg 或 xl, 對應的是小型設備、中型設備,大型設備,超大型設備,

https://www.runoob.com/bootstrap4/bootstrap4-flex.html  菜鳥筆記最下面寫的很清楚,要用了就去翻翻

 

Bootstrap 4 多媒體物件

要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果:

    <div class="media border p-3">
        <img src="img/img_avatar.png" class="mr-3 mt-3 rounded-circle" style="width:50px;">
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
        </div>
    </div>

 

 

多媒體物件可以多個嵌套(一個多媒體物件中包含另外一個多媒體物件)

要嵌套多媒體物件,可以把新的 .media 容器放到 .media-body 容器中:

    <div class="media border p-3">
        <img src="img/img_avatar.png" class="mr-3 mt-3 rounded-circle" style="width:50px;">
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
            <div class="media border p-3">
                <img src="img/img_avatar.png" class="mr-3 mt-3 rounded-circle" style="width:50px;">
                <div class="media-body">
                    <h4>基礎多媒體物件</h4>
                    <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
                </div>
            </div>
        </div>
    </div>

 

 

如果你想將頭像圖片顯示在右側,可以在 .media-body 容器后添加圖片:

    <div class="media border p-3">        
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
        </div>
        <img src="img/img_avatar.png" class="mr-3 mt-3 rounded-circle" style="width:50px;">
    </div>

 

 

 

 

可以使用 align-self-* 相關類來設定多媒體物件的圖片顯示位置:

(垂直方向)

    <div class="media border p-3">
        <img src="img/img_avatar.png" class="align-self-start mr-3 rounded-circle" style="width:50px;">
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
        </div>
    </div>

    <div class="media border p-3">
        <img src="img/img_avatar.png" class="align-self-end mr-3 rounded-circle" style="width:50px;">
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
        </div>
    </div>

    <div class="media border p-3">
        <img src="img/img_avatar.png" class="align-self-center mr-3 rounded-circle" style="width:50px;">
        <div class="media-body">
            <h4>基礎多媒體物件</h4>
            <p>要創建一個多媒體物件,可以在容器元素上添加 .media 類,然后將多媒體內容放到子容器上,子容器需要添加 .media-body 類,然后添加外邊距,內邊距等效果,</p>
        </div>
    </div>

 

 

Bootstrap4 創建一個網頁

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo1</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>
    <style>
        
    </style>
</head>
<body>

    <div class="jumbotron text-center" style="margin-bottom:0;">
        <h1>我的第一個 Bootstrap 4 頁面</h1>
          <p>重置瀏覽器視窗大小查看效果!</p> 
    </div>

    <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
      <a class="navbar-brand" href="#">導航</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="collapsibleNavbar">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link" href="#">鏈接</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">鏈接</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">鏈接</a>
          </li>    
        </ul>
      </div>  
    </nav>

    <div class="container">
        <div class="row">
            <div class="col-sm-4">
                about me
                <ul class="nav nav-pills flex-column">
                    <li class="nav-item">
                      <a class="nav-link active" href="#">激活狀態</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">鏈接</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">鏈接</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link disabled" href="#">禁用</a>
                    </li>
                 </ul>
                 <hr class="d-sm-none">
            </div>
            <div class="col-sm-8">
                ...
            </div>
        </div>
    </div>

    <div class="jumbotron text-center" style="margin-bottom:0">
      <p>footer</p>
    </div>

</body>
</html>

 

 好丑……

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/45565.html

標籤:Html/Css

上一篇:CSS的3種使用方法

下一篇:flex兼容性寫法

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • IEEE1588PTP在數字化變電站時鐘同步方面的應用

    IEEE1588ptp在數字化變電站時鐘同步方面的應用 京準電子科技官微——ahjzsz 一、電力系統時間同步基本概況 隨著對IEC 61850標準研究的不斷深入,國內外學者提出基于IEC61850通信標準體系建設數字化變電站的發展思路。數字化變電站與常規變電站的顯著區別在于程序層傳統的電流/電壓互 ......

    uj5u.com 2020-09-10 03:51:52 more
  • HTTP request smuggling CL.TE

    CL.TE 簡介 前端通過Content-Length處理請求,通過反向代理或者負載均衡將請求轉發到后端,后端Transfer-Encoding優先級較高,以TE處理請求造成安全問題。 檢測 發送如下資料包 POST / HTTP/1.1 Host: ac391f7e1e9af821806e890 ......

    uj5u.com 2020-09-10 03:52:11 more
  • 網路滲透資料大全單——漏洞庫篇

    網路滲透資料大全單——漏洞庫篇漏洞庫 NVD ——美國國家漏洞庫 →http://nvd.nist.gov/。 CERT ——美國國家應急回應中心 →https://www.us-cert.gov/ OSVDB ——開源漏洞庫 →http://osvdb.org Bugtraq ——賽門鐵克 →ht ......

    uj5u.com 2020-09-10 03:52:15 more
  • 京準講述NTP時鐘服務器應用及原理

    京準講述NTP時鐘服務器應用及原理京準講述NTP時鐘服務器應用及原理 安徽京準電子科技官微——ahjzsz 北斗授時原理 授時是指接識訓通過某種方式獲得本地時間與北斗標準時間的鐘差,然后調整本地時鐘使時差控制在一定的精度范圍內。 衛星導航系統通常由三部分組成:導航授時衛星、地面檢測校正維護系統和用戶 ......

    uj5u.com 2020-09-10 03:52:25 more
  • 利用北斗衛星系統設計NTP網路時間服務器

    利用北斗衛星系統設計NTP網路時間服務器 利用北斗衛星系統設計NTP網路時間服務器 安徽京準電子科技官微——ahjzsz 概述 NTP網路時間服務器是一款支持NTP和SNTP網路時間同步協議,高精度、大容量、高品質的高科技時鐘產品。 NTP網路時間服務器設備采用冗余架構設計,高精度時鐘直接來源于北斗 ......

    uj5u.com 2020-09-10 03:52:35 more
  • 詳細解讀電力系統各種對時方式

    詳細解讀電力系統各種對時方式 詳細解讀電力系統各種對時方式 安徽京準電子科技官微——ahjzsz,更多資料請添加VX 衛星同步時鐘是我京準公司開發研制的應用衛星授時時技術的標準時間顯示和發送的裝置,該裝置以M國全球定位系統(GLOBAL POSITIONING SYSTEM,縮寫為GPS)或者我國北 ......

    uj5u.com 2020-09-10 03:52:45 more
  • 如何保證外包團隊接入企業內網安全

    不管企業規模的大小,只要企業想省錢,那么企業的某些服務就一定會采用外包的形式,然而看似美好又經濟的策略,其實也有不好的一面。下面我通過安全的角度來聊聊使用外包團的安全隱患問題。 先看看什么服務會使用外包的,最常見的就是話務/客服這種需要大量重復性、無技術性的服務,或者是一些銷售外包、特殊的職能外包等 ......

    uj5u.com 2020-09-10 03:52:57 more
  • PHP漏洞之【整型數字型SQL注入】

    0x01 什么是SQL注入 SQL是一種注入攻擊,通過前端帶入后端資料庫進行惡意的SQL陳述句查詢。 0x02 SQL整型注入原理 SQL注入一般發生在動態網站URL地址里,當然也會發生在其它地發,如登錄框等等也會存在注入,只要是和資料庫打交道的地方都有可能存在。 如這里http://192.168. ......

    uj5u.com 2020-09-10 03:55:40 more
  • [GXYCTF2019]禁止套娃

    git泄露獲取原始碼 使用GET傳參,引數為exp 經過三層過濾執行 第一層過濾偽協議,第二層過濾帶引數的函式,第三層過濾一些函式 preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'] (?R)參考當前正則運算式,相當于匹配函式里的引數 因此傳遞 ......

    uj5u.com 2020-09-10 03:56:07 more
  • 等保2.0實施流程

    流程 結論 ......

    uj5u.com 2020-09-10 03:56:16 more
最新发布
  • 使用Django Rest framework搭建Blog

    在前面的Blog例子中我們使用的是GraphQL, 雖然GraphQL的使用處于上升趨勢,但是Rest API還是使用的更廣泛一些. 所以還是決定回到傳統的rest api framework上來, Django rest framework的官網上給了一個很好用的QuickStart, 我參考Qu ......

    uj5u.com 2023-04-20 08:17:54 more
  • 記錄-new Date() 我忍你很久了!

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 大家平時在開發的時候有沒被new Date()折磨過?就是它的諸多怪異的設定讓你每每用的時候,都可能不小心踩坑。造成程式意外出錯,卻一下子找不到問題出處,那叫一個煩透了…… 下面,我就列舉它的“四宗罪”及應用思考 可惡的四宗罪 1. Sa ......

    uj5u.com 2023-04-20 08:17:47 more
  • 使用Vue.js實作文字跑馬燈效果

    實作文字跑馬燈效果,首先用到 substring()截取 和 setInterval計時器 clearInterval()清除計時器 效果如下: 實作代碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta ......

    uj5u.com 2023-04-20 08:12:31 more
  • JavaScript 運算子

    JavaScript 運算子/運算子 在 JavaScript 中,有一些運算子可以使代碼更簡潔、易讀和高效。以下是一些常見的運算子: 1、可選鏈運算子(optional chaining operator) ?.是可選鏈運算子(optional chaining operator)。?. 可選鏈操 ......

    uj5u.com 2023-04-20 08:02:25 more
  • CSS—相對單位rem

    一、概述 rem是一個相對長度單位,它的單位長度取決于根標簽html的字體尺寸。rem即root em的意思,中文翻譯為根em。瀏覽器的文本尺寸一般默認為16px,即默認情況下: 1rem = 16px rem布局原理:根據CSS媒體查詢功能,更改根標簽的字體尺寸,實作rem單位隨螢屏尺寸的變化,如 ......

    uj5u.com 2023-04-20 08:02:21 more
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 08:01:50 more
  • 如何在 vue3 中使用 jsx/tsx?

    我們都知道,通常情況下我們使用 vue 大多都是用的 SFC(Signle File Component)單檔案組件模式,即一個組件就是一個檔案,但其實 Vue 也是支持使用 JSX 來撰寫組件的。這里不討論 SFC 和 JSX 的好壞,這個仁者見仁智者見智。本篇文章旨在帶領大家快速了解和使用 Vu ......

    uj5u.com 2023-04-20 08:01:37 more
  • 【Vue2.x原始碼系列06】計算屬性computed原理

    本章目標:計算屬性是如何實作的?計算屬性快取原理以及洋蔥模型的應用?在初始化Vue實體時,我們會給每個計算屬性都創建一個對應watcher,我們稱之為計算屬性watcher ......

    uj5u.com 2023-04-20 08:01:31 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:01:10 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:00:32 more