Bootstrap 5 常用語法


Posted by pei_______ on 2022-05-23

目錄:

01. [導入Bootstrap]
02. [導覽列 Navbar]
03. [表格 Grid system]
04. [容器 Containers]
05. [按鈕 Buttons]
06. [輪播 Carousel]
07. [卡片 Cards]
08. [z-index]



01. 導入Bootstrap

  • 注意導入時必須放在css或其他連結的前方,否則可能覆寫後來的設計
<!-- CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<!-- JS -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.min.js" integrity="sha384-lpyLfhYuitXl2zRZ5Bn2fqnhNAKOAaM/0Kr9laMspuaMiZfGmfwRNFh8HlMy49eQ" crossorigin="anonymous"></script>

02. 導覽列 Navbar

<nav class="navbar bg-dark navbar-expand-lg navbar-dark">

    <a class="navbar-brand" href="">Tindog</a>

     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ms-auto">
        <li class="nav-item">
            <a class="nav-link" href="#Contact">Contact</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#Pricing">Pricing</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#Download">Download</a>
        </li>
    </ul>
 </div>
</nav>

03. 表格 Grid system

  • 可以col-lg, col-md, col-sm設定隨視窗改變而改變欄位數目的表格
<!--大螢幕-4欄, 中螢幕-3欄, 小螢幕-2欄-->
<div class="row">
    <div class="col-lg-3 col-md-4 col-sm-6" style="background-color:yellow; border: 1px solid;">
        col-lg-3 col-md-4 col-sm-6
    </div>
    <div class="col-lg-3 col-md-4 col-sm-6" style="background-color:yellow; border: 1px solid;">
        col-lg-3 col-md-4 col-sm-6
    </div>
    <div class="col-lg-3 col-md-4 col-sm-6" style="background-color:yellow; border: 1px solid;">
        col-lg-3 col-md-4 col-sm-6
    </div>
    <div class="col-lg-3 col-md-4 col-sm-6" style="background-color:yellow; border: 1px solid;">
        col-lg-3 col-md-4 col-sm-6
    </div>
</div>

04. 容器 Containers

  • class="container" => 寬度的比例會隨視窗大小改變
  • class="container-fluid" => 無論視窗大小為何,都會保持寬度在100%
<!-- 寬度保持100%-->
<div class="container-fluid" style="background-color: yellow;">
    Bootstrap’s grid system uses a series of containers, rows, and columns to layout and align content. It’s built with flexbox and is fully responsive. Below is an example and an in-depth look at how the grid comes together.
</div>

05. 按鈕 Buttons

06. [輪播 Carousel]
(https://bootstrap5.hexschool.com/docs/5.0/components/carousel/)

  • 第一個頁面須加上active才會顯示
<div id="carouselExampleControls" class="carousel slide" data-bs-ride="false" ;>
    <div class="carousel-inner">
        <div class="carousel-item active" style="background-color: red;">
            <img src="..." class="d-block w-100" alt="1">
        </div>
        <div class="carousel-item" style="background-color: blue;">
            <img src="..." class="d-block w-100" alt="2">
        </div>
        <div class="carousel-item" style="background-color: yellow;">
            <img src="..." class="d-block w-100" alt="3">
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleControls" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
        </button>
    </div>
</div>

07. 卡片 Cards

  • 可用card-group組成堆疊
<div class="card">

    <div class="card-header">
        <h3>Chihuahua</h3>
    </div>

    <div class="card-body">
        <h2 class="price-text">Free</h2>
        <p>5 Matches Per Day</p>
    </div>
</div>

08. [z-index]https://bootstrap5.hexschool.com/docs/5.0/layout/z-index/

  • 要有position才能使用(relative/absolute/fixed)

#Bootstrap #web系列







Related Posts

[Note] Runtime CSS-in-JS vs Zero-runtime CSS-in-JS

[Note] Runtime CSS-in-JS vs Zero-runtime CSS-in-JS

[11] 型別 - undefined、undeclared、依存注入

[11] 型別 - undefined、undeclared、依存注入

[演算法] Bubble Sort 泡沫排序法

[演算法] Bubble Sort 泡沫排序法


Comments