《Bootstrap5零基礎到精通》第20節 Bootstrap5 Button group按鈕組用法

這是我參與更文挑戰的第20天,活動詳情查看: 更文挑戰css

《Bootstrap5零基礎到精通》 俺老劉原創,爭取天天更新一節。html

20.1 簡單例子

將一系列按鈕組合在一行上,或將它們堆疊在一個垂直的列中,用.btn-group包裝,就組成了一個按鈕組。bootstrap

<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
複製代碼

1.1.png

這些類也能夠添加到連接組中,做爲.nav導航組件的替代。markdown

<div class="btn-group">
<a href="#" class="btn btn-primary active" aria-current="page">Active link</a>
<a href="#" class="btn btn-primary">Link</a>
<a href="#" class="btn btn-primary">Link</a>
</div>
複製代碼

1.2.png

20.2 不一樣形式的按鈕組

20.2.1 混合顏色樣式

幾種不一樣樣色的按鈕放在一塊兒工具

<div class="btn-group" role="group" aria-label="Basic mixed styles example">
<button type="button" class="btn btn-danger">Left</button>
<button type="button" class="btn btn-warning">Middle</button>
<button type="button" class="btn btn-success">Right</button>
</div>
複製代碼

2.1.png

20.2.2 輪廓樣式

<div class="btn-group" role="group" aria-label="Basic outlined example">
<button type="button" class="btn btn-outline-primary">Left</button>
<button type="button" class="btn btn-outline-primary">Middle</button>
<button type="button" class="btn btn-outline-primary">Right</button>
</div>
複製代碼

2.2.png

20.2.3 複選框

複選框在表單部分也很介紹,此處介紹的是按鈕形態,隱藏表單,只顯示標籤,且標籤具備選中和未選兩種狀態。post

<div class="btn-group" role="group" aria-label="Basic checkbox toggle button group">
<input type="checkbox" class="btn-check" id="btncheck1" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="btncheck1">Checkbox 1</label>

<input type="checkbox" class="btn-check" id="btncheck2" autocomplete="off">
<label class="btn btn-outline-primary" for="btncheck2">Checkbox 2</label>

<input type="checkbox" class="btn-check" id="btncheck3" autocomplete="off">
<label class="btn btn-outline-primary" for="btncheck3">Checkbox 3</label>
</div>
複製代碼

2.3.png

20.2.4 單選按鈕組

同複選框學習

<div class="btn-group" role="group" aria-label="Basic radio toggle button group">
<input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked>
<label class="btn btn-outline-primary" for="btnradio1">Radio 1</label>

<input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio2">Radio 2</label>

<input type="radio" class="btn-check" name="btnradio" id="btnradio3" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio3">Radio 3</label>
</div>
複製代碼

2.4.png

20.2.5 按鈕工具條

將整組的按鈕羣組加入到按鈕工具欄中,以創建更復雜的組件。依照需求,使用通用類將羣組、按鈕等間隔開來。spa

<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group me-2" role="group" aria-label="First group"> <button type="button" class="btn btn-primary">1</button> <button type="button" class="btn btn-primary">2</button> <button type="button" class="btn btn-primary">3</button> <button type="button" class="btn btn-primary">4</button> </div>
<div class="btn-group me-2" role="group" aria-label="Second group"> <button type="button" class="btn btn-secondary">5</button> <button type="button" class="btn btn-secondary">6</button> <button type="button" class="btn btn-secondary">7</button> </div>
<div class="btn-group" role="group" aria-label="Third group"> <button type="button" class="btn btn-info">8</button> </div>
</div>
複製代碼

2.5.png

任意將input羣組與工具欄中的按鈕羣組進行混合。與上面的示例相似,您須要一些通用類別以適當的將這些的內容間隔開。此處知道能夠這樣用便可,在學完表單部分會有更清晰的認識。3d

<div class="btn-toolbar mb-3" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group me-2" role="group" aria-label="First group"> <button type="button" class="btn btn-outline-secondary">1</button> <button type="button" class="btn btn-outline-secondary">2</button> <button type="button" class="btn btn-outline-secondary">3</button> <button type="button" class="btn btn-outline-secondary">4</button> </div>
<div class="input-group"> <div class="input-group-text" id="btnGroupAddon">@</div> <input type="text" class="form-control" placeholder="Input group example" aria-label="Input group example" aria-describedby="btnGroupAddon"> </div> </div>

<div class="btn-toolbar justify-content-between" role="toolbar" aria-label="Toolbar with button groups"> <div class="btn-group" role="group" aria-label="First group"> <button type="button" class="btn btn-outline-secondary">1</button> <button type="button" class="btn btn-outline-secondary">2</button> <button type="button" class="btn btn-outline-secondary">3</button> <button type="button" class="btn btn-outline-secondary">4</button> </div> <div class="input-group"> <div class="input-group-text" id="btnGroupAddon2">@</div> <input type="text" class="form-control" placeholder="Input group example" aria-label="Input group example" aria-describedby="btnGroupAddon2"> </div> </div> 複製代碼

2.6.png

20.3 大小

沒必要將按鈕尺寸調整類別套用在羣組內的每個按鈕上,只須要在每一個帶有btn-group的元件加上btn-group-*便可。code

<div class="btn-group btn-group-lg" role="group" aria-label="...">...</div>
<div class="btn-group" role="group" aria-label="...">...</div>
<div class="btn-group btn-group-sm" role="group" aria-label="...">...</div>
複製代碼

3.png

20.4 嵌套

當您想要下拉式功能表與按鈕羣組混合時,只須要將.btn-group放在另外一個.btn-group中便可。

<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
<button type="button" class="btn btn-primary">1</button>
<button type="button" class="btn btn-primary">2</button>

<div class="btn-group" role="group"> <button id="btnGroupDrop1" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </button> <ul class="dropdown-menu" aria-labelledby="btnGroupDrop1"> <li><a class="dropdown-item" href="#">Dropdown link</a></li> <li><a class="dropdown-item" href="#">Dropdown link</a></li> </ul> </div>
</div>
複製代碼

20.5 垂直變化

將一組按鈕垂直堆疊而不是水平呈現,你只須要把上面例子的按鈕組標籤btn-group替換爲btn-group-vertical(注意此處是直接用btn-group-vertical作容器,而不是在btn-group附加btn-group-vertical類)。此處支持嵌套,不支持分割下拉式功能表。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>按鈕組</title>
  </head>
  <body>
    <div class="container">
      <br><br><br>
      <div class="btn-group-vertical" role="group">
        <input type="radio" class="btn-check" name="vbtn-radio" id="vbtn-radio1" autocomplete="off" checked="">
        <label class="btn btn-outline-danger" for="vbtn-radio1">Radio 1</label>
        <input type="radio" class="btn-check" name="vbtn-radio" id="vbtn-radio2" autocomplete="off">
        <label class="btn btn-outline-danger" for="vbtn-radio2">Radio 2</label>
        <input type="radio" class="btn-check" name="vbtn-radio" id="vbtn-radio3" autocomplete="off">
        <label class="btn btn-outline-danger" for="vbtn-radio3">Radio 3</label>
      </div>

      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
  </body>
</html>
...
</div>
複製代碼

5.png

嵌套很簡單,把2.4的例子改一個標籤而已

<div class="btn-group-vertical" role="group">
        <button type="button" class="btn btn-primary">1</button>
        <button type="button" class="btn btn-primary">2</button>
        
        <div class="btn-group" role="group"> <button id="btnGroupDrop1" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </button> <ul class="dropdown-menu" aria-labelledby="btnGroupDrop1"> <li><a class="dropdown-item" href="#">Dropdown link</a></li> <li><a class="dropdown-item" href="#">Dropdown link</a></li> </ul> </div>
        </div>
複製代碼

6.png

今天的課程就到這裏,請關注我,及時學習 俺老劉原創的《Bootstrap5零基礎到精通》第21節 Bootstrap5 卡片用法,卡片功能也是很是強大而有用,不要錯過啊。

若是這篇文章對你有幫助,記得隨手點贊哦!

相關文章
相關標籤/搜索