小码哥的IT人生

Bootstrap 5 导航

Bootstrap 2023-08-10 09:26:53小码哥的IT人生shichen

Bootstrap 5 导航

导航菜单

如果要创建简单的水平菜单,请将 .nav 类添加到 <ul> 元素,然后为每个 <li> 元素添加 .nav-item 并将 .nav-link 类添加到它们的链接:

示例代码:

<ul class="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>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">禁用</a>
  </li>
</ul>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>导航</h2>
  <p>基础的水平菜单:</p>
  <ul class="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>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">禁用</a>
    </li>
  </ul>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

对齐导航栏

添加 .justify-content-center 类使导航居中,添加 .justify-content-end 类使导航右对齐。

示例代码:

<!-- 居中的导航 -->
<ul class="nav justify-content-center">
<!-- 右对齐的导航 -->
<ul class="nav justify-content-end">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>导航</h2>
  <p>左对齐的导航(默认):</p>
  <ul class="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>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">禁用</a>
    </li>
  </ul>
  <p class="text-center">居中的导航:</p>
  <ul class="nav justify-content-center">
    <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>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">禁用</a>
    </li>
  </ul>
  <p class="text-end">右对齐的导航:</p>
  <ul class="nav justify-content-end">
    <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>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">禁用</a>
    </li>
  </ul>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

垂直导航栏

添加 .flex-column 类可创建垂直导航:

示例代码:

<ul class="nav flex-column">

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>垂直导航</h2>
  <p>使用 .flex-column 类创建垂直导航:</p>
  <ul class="nav flex-column">
    <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>
    <li class="nav-item">
      <a class="nav-link disabled" href="#">禁用</a>
    </li>
  </ul>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

选项卡

请使用 .nav-tabs 类将导航菜单转换为导航选项卡。并将 .active 类添加到活动/当前链接。如果您希望选项卡可切换,请参阅本页的最后一个例子。

示例代码:

<ul class="nav nav-tabs">
  <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>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>选项卡</h2>
  <p>使用 .nav nav-tabs 类将导航菜单转换为导航选项卡。将 active 类添加到活动的/当前链接。如果您希望选项卡可切换,请参阅教程页面上的最后一个例子。</p>
  <ul class="nav nav-tabs">
    <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>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

胶囊

使用 .nav-pills 类可将导航菜单设置为导航胶囊。如果您希望胶囊可切换,请参阅本页的最后一个例子。

示例代码:

<ul class="nav nav-pills">
  <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>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>导航胶囊</h2>
  <p>使用 .nav nav-pills 类将导航菜单变成导航胶囊:</p>
  <ul class="nav nav-pills">
    <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>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

等宽的选项卡/胶囊导航

请使用 .nav-justified 类(等宽)对齐标签/胶囊:

示例代码:

<ul class="nav nav-pills nav-justified">..</ul>
<ul class="nav nav-tabs nav-justified">..</ul>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>等宽的选项卡/胶囊导航</h2>
  <p>请使用 .nav-justified 类(等宽)对齐标签/胶囊:</p>
  <ul class="nav nav-pills nav-justified">
    <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><br>
  <p>Justified tabs:</p>
  <ul class="nav nav-tabs nav-justified">
    <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>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

胶囊下拉菜单

示例代码:

<ul class="nav nav-pills">
  <li class="nav-item">
    <a class="nav-link active" href="#">活动</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#">Dropdown</a>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">链接 1</a></li>
      <li><a class="dropdown-item" href="#">链接 2</a></li>
      <li><a class="dropdown-item" href="#">链接 3</a></li>
    </ul>
  </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>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>胶囊下拉菜单</h2>
  <ul class="nav nav-pills">
    <li class="nav-item">
      <a class="nav-link active" href="#">活动</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#">下拉列表</a>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">链接 1</a></li>
        <li><a class="dropdown-item" href="#">链接 2</a></li>
        <li><a class="dropdown-item" href="#">链接 3</a></li>
      </ul>
    </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>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

选项卡下拉菜单

示例代码:

<ul class="nav nav-tabs">
  <li class="nav-item">
    <a class="nav-link active" href="#">活动</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#">Dropdown</a>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">链接 1</a></li>
      <li><a class="dropdown-item" href="#">链接 2</a></li>
      <li><a class="dropdown-item" href="#">链接 3</a></li>
    </ul>
  </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>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>选项卡下拉菜单</h2>
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a class="nav-link active" href="#">活动</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" href="#">下拉列表</a>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">链接 1</a></li>
        <li><a class="dropdown-item" href="#">链接 2</a></li>
        <li><a class="dropdown-item" href="#">链接 3</a></li>
      </ul>
    </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>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

可切换的 / 动态选项卡

如需使选项卡可切换,请将 data-toggle="tab" 属性添加到每个链接。然后为每个选项卡添加有唯一 ID 的 .tab-pane 类,并将它们包装在带有 .tab-content 类的 <div> 元素中。

如果您希望选项卡在单击时能够淡入淡出,请将 .fade 类添加到 .tab-pane

示例代码:

<!-- Nav tabs -->
<ul class="nav nav-tabs">
  <li class="nav-item">
    <a class="nav-link active" data-bs-toggle="tab" href="#home">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="tab" href="#menu1">Menu 1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="tab" href="#menu2">Menu 2</a>
  </li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
  <div class="tab-pane container active" id="home">...</div>
  <div class="tab-pane container fade" id="menu1">...</div>
  <div class="tab-pane container fade" id="menu2">...</div>
</div>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>可切换的 / 动态选项卡</h2>
  <br>
  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li class="nav-item">
      <a class="nav-link active" data-bs-toggle="tab" href="#home">春日</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#menu1">初夏</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="tab" href="#menu2">山行</a>
    </li>
  </ul>
  <!-- Tab panes -->
  <div class="tab-content">
    <div id="home" class="container tab-pane active"><br>
      <h3>春日</h3>
      <p>胜日寻芳泗水滨,无边光景一时新。</p>
      <p>等闲识得东风面,万紫千红总是春。</p>
    </div>
    <div id="menu1" class="container tab-pane fade"><br>
      <h3>初夏绝句</h3>
      <p>纷纷红紫已成尘,布谷声中夏令新。</p>
      <p>夹路桑麻行不尽,始知身是太平人。</p>
    </div>
    <div id="menu2" class="container tab-pane fade"><br>
      <h3>山行</h3>
      <p>远上寒山石径斜,白云生处有人家。</p>
      <p>停车坐爱枫林晚,霜叶红于二月花。</p>
    </div>
  </div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

可切换的 / 动态胶囊导航

相同的代码适用于导航;仅将 data-toggle 属性更改为 data-toggle="pill" 即可:

示例代码:

<!--导航胶囊 -->
<ul class="nav nav-pills">
  <li class="nav-item">
    <a class="nav-link active" data-bs-toggle="pill" href="#home">春日</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="pill" href="#menu1">初夏</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="pill" href="#menu2">山行</a>
  </li>
</ul>
<!-- 选项卡窗格 -->
<div class="tab-content">
  <div class="tab-pane container active" id="home">...</div>
  <div class="tab-pane container fade" id="menu1">...</div>
  <div class="tab-pane container fade" id="menu2">...</div>
</div>

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 实例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
  <h2>可切换的 / 动态胶囊导航</h2>
  <br>
  <!-- Nav pills -->
  <ul class="nav nav-pills" role="tablist">
    <li class="nav-item">
      <a class="nav-link active" data-bs-toggle="pill" href="#home">春日</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="pill" href="#menu1">初夏</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-bs-toggle="pill" href="#menu2">山行</a>
    </li>
  </ul>
  <!-- Tab panes -->
  <div class="tab-content">
    <div id="home" class="container tab-pane active"><br>
      <h3>春日</h3>
      <p>胜日寻芳泗水滨,无边光景一时新。</p>
      <p>等闲识得东风面,万紫千红总是春。</p>
    </div>
    <div id="menu1" class="container tab-pane fade"><br>
      <h3>初夏绝句</h3>
      <p>纷纷红紫已成尘,布谷声中夏令新。</p>
      <p>夹路桑麻行不尽,始知身是太平人。</p>
    </div>
    <div id="menu2" class="container tab-pane fade"><br>
      <h3>山行</h3>
      <p>远上寒山石径斜,白云生处有人家。</p>
      <p>停车坐爱枫林晚,霜叶红于二月花。</p>
    </div>
  </div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024