- Bootstrap 教程
- Bootstrap - 首页
- Bootstrap - 概述
- Bootstrap - 环境设置
- Bootstrap - RTL(从右到左)
- Bootstrap - CSS 变量
- Bootstrap - 颜色模式
- Bootstrap 布局
- Bootstrap - 断点
- Bootstrap - 容器
- Bootstrap - 网格系统
- Bootstrap - 列
- Bootstrap - 间距
- Bootstrap - 实用工具类
- Bootstrap - CSS 网格
- Bootstrap 组件
- Bootstrap - 手风琴
- Bootstrap - 警报
- Bootstrap - 徽章
- Bootstrap - 面包屑
- Bootstrap - 按钮
- Bootstrap - 按钮组
- Bootstrap - 卡片
- Bootstrap - 走马灯
- Bootstrap - 关闭按钮
- Bootstrap - 折叠
- Bootstrap - 下拉菜单
- Bootstrap - 列表组
- Bootstrap - 模态框
- Bootstrap - 导航栏
- Bootstrap - 导航 & 标签页
- Bootstrap - 侧边栏
- Bootstrap - 分页
- Bootstrap - 占位符
- Bootstrap - 气泡提示
- Bootstrap - 进度条
- Bootstrap - 滚动侦听
- Bootstrap - 加载动画
- Bootstrap - 提示框
- Bootstrap - 工具提示
- Bootstrap 表单
- Bootstrap - 表单
- Bootstrap - 表单控件
- Bootstrap - 选择框
- Bootstrap - 复选框 & 单选按钮
- Bootstrap - 范围滑块
- Bootstrap - 输入组
- Bootstrap - 浮动标签
- Bootstrap - 布局
- Bootstrap - 表单验证
- Bootstrap 辅助类
- Bootstrap - 清除浮动
- Bootstrap - 颜色 & 背景
- Bootstrap - 彩色链接
- Bootstrap - 焦点环
- Bootstrap - 图标链接
- Bootstrap - 位置
- Bootstrap - 比例
- Bootstrap - 堆叠
- Bootstrap - 拉伸链接
- Bootstrap - 文本截断
- Bootstrap - 垂直分割线
- Bootstrap - 视觉隐藏
- Bootstrap 实用工具类
- Bootstrap - 背景
- Bootstrap - 边框
- Bootstrap - 颜色
- Bootstrap - 显示
- Bootstrap - Flex 布局
- Bootstrap - 浮动
- Bootstrap - 交互
- Bootstrap - 链接
- Bootstrap - 对象适应
- Bootstrap - 不透明度
- Bootstrap - 溢出
- Bootstrap - 位置
- Bootstrap - 阴影
- Bootstrap - 尺寸
- Bootstrap - 间距
- Bootstrap - 文本
- Bootstrap - 垂直对齐
- Bootstrap - 可见性
- Bootstrap 演示
- Bootstrap - 网格演示
- Bootstrap - 按钮演示
- Bootstrap - 导航演示
- Bootstrap - 博客演示
- Bootstrap - 滑块演示
- Bootstrap - 走马灯演示
- Bootstrap - 页眉演示
- Bootstrap - 页脚演示
- Bootstrap - 英雄图演示
- Bootstrap - 特色演示
- Bootstrap - 侧边栏演示
- Bootstrap - 下拉菜单演示
- Bootstrap - 列表组演示
- Bootstrap - 模态框演示
- Bootstrap - 徽章演示
- Bootstrap - 面包屑演示
- Bootstrap - Jumbotron 演示
- Bootstrap - 粘性页脚演示
- Bootstrap - 相册演示
- Bootstrap - 登录演示
- Bootstrap - 定价演示
- Bootstrap - 结账演示
- Bootstrap - 产品演示
- Bootstrap - 封面演示
- Bootstrap - 仪表盘演示
- Bootstrap - 粘性页脚导航栏演示
- Bootstrap - 砌体布局演示
- Bootstrap - 启动模板演示
- Bootstrap - RTL 相册演示
- Bootstrap - RTL 结账演示
- Bootstrap - RTL 走马灯演示
- Bootstrap - RTL 博客演示
- Bootstrap - RTL 仪表盘演示
- Bootstrap 有用资源
- Bootstrap - 常见问题解答
- Bootstrap - 快速指南
- Bootstrap - 有用资源
- Bootstrap - 讨论
Bootstrap - 导航栏
本章将讨论 Bootstrap 导航栏。Bootstrap 的导航栏是一个功能强大且响应式的导航页眉。导航栏位于页面顶部。
工作原理
基本的导航栏使用.navbar类构建。对于响应式折叠,请使用.navbar-expand{-sm|-md|-lg|-xl|-xxl}类包裹.navbar类(在小型、中型、大型、特大型或超大型屏幕上垂直排列导航栏)。
使用aria-current="page"指示当前项目,或使用aria-current="true"指示集合中的当前项目。
请使用<nav>元素确保可用性,或者如果使用更通用的元素(如<div>),请在每个导航栏中包含role="navigation",以便明确地将其标识为辅助技术用户的关注点。
v5.2.0 新增功能:导航栏主题可以使用 CSS 变量进行设置,这些变量仅限于.navbar基类。.navbar-light已被弃用,.navbar-dark已被重写为使用 CSS 变量而不是包含额外的样式。
基本导航栏
支持的内容
使用导航栏时,内置类非常方便,如下所示
使用.navbar-brand类显示公司、产品或项目名称。
使用.navbar-nav类创建全高、轻量级的导航(包括对下拉菜单的支持)。
结合折叠插件使用.navbar-toggler类实现导航切换功能。
使用.navbar-text类添加垂直居中的文本。
使用.collapse.navbar-collapse类对组进行分组,并通过父断点折叠导航栏内容。
使用.navbar-scroll类设置最大高度和滚动导航栏的展开内容。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Tutorialspoint</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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 me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Blog </a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Blog 1</a></li> <li><a class="dropdown-item" href="#">Blog 2</a></li> </ul> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search Here" aria-label="Search"> <button class="btn btn-outline-warning" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
垂直导航栏
要创建一个始终垂直的导航栏,请移除.navbar-expand-*类。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-light"> <div class="container-fluid"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="#"> Item 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Item 2</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Item 3</a> </li> </ul> </div> </nav> <div class="container-fluid mt-3"> <h3>Vertical Navbar</h3> </div> </body> </html>
居中导航栏
添加.justify-content-center类以显示居中的导航栏
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-sm bg-light justify-content-center"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="#">Item 1</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Item 2</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Item 3</a> </li> </ul> </nav> <div class="container-fluid mt-3"> <h3>Centered Navbar</h3> </div> </body> </html>
品牌
添加.navbar-brand类用于显示项目名称的品牌。
文本
通过添加.navbar-brand类,在元素内添加文本。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-primary mt-2"> <div class="container-fluid"> <a class="navbar-brand" href="#">Tutorialspoint</a> </div> </nav> <nav class="navbar bg-light mt-2"> <div class="container-fluid"> <span class="navbar-brand mb-0 h1">Tutorialspoint</span> </div> </nav> </body> </html>
图片
用<img>标签替换.navbar-brand内的文本。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <div class="container"> <a class="navbar-brand" href="#"> <img src="\bootstrap\images\tutimg.png" alt="Bootstrap" width="100" height="65"> </a> </div> </nav> </body> </html>
图片和文本
您还可以使用.d-inline-block和.align-text-top类(在<img>元素上)添加一些额外的实用工具类,以同时添加图片和文本。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#"> <img src="\bootstrap\images\tutimg.png" alt="Logo" width="100" height="75" class="d-inock alline-blign-text-mid"> Tutorialspoint </a> </div> </nav> </body> </html>
导航
导航栏导航链接基于.nav选项及其自身的修饰符类构建,并使用切换器类进行响应式样式设置。
导航栏导航还将扩展以占据尽可能多的水平空间,以安全地对齐导航栏内容。
在.nav-link中添加.active类以显示当前页面。
请注意,还应将aria-current属性添加到活动的.nav链接。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contanct us</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> </div> </div> </nav> </body> </html>
无列表的导航栏
由于导航栏使用类进行导航,因此可以完全避免基于列表的方法,如下面的示例所示。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Tutorialspoint</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-link active" aria-current="page" href="#">Home</a> <a class="nav-link" href="#">About us</a> <a class="nav-link" href="#">Services</a> <a class="nav-link disabled">Contanct us</a> </div> </div> </div> </nav> </body> </html>
带有下拉菜单的导航栏
要在导航栏中使用下拉菜单,请添加.nav-item和.nav-link类,如下例所示。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Tutorialspoint</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavDropdown"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About Us</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Blog </a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Blog 1</a></li> <li><a class="dropdown-item" href="#">Blog 2</a></li> </ul> </li> </ul> </div> </div> </nav> </body> </html>
表单
通过添加.form-control类,在导航栏中放置各种表单控件。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-primary"> <div class="container-fluid"> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search Here.." aria-label="Search"> <button class="btn btn-outline-warning" type="submit">Search</button> </form> </div> </nav> </body> </html>
.navbar的子元素现在默认使用 Flex 布局和justify-content: space-between。根据需要使用其他Flex 实用工具类来自定义此行为。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand">Navbar</a> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search Here..." aria-label="Search"> <button class="btn btn-outline-info" type="submit">Search</button> </form> </div> </nav> </body> </html>
如果您的导航栏是一个完整的表单或几乎是表单,则使用<form>元素作为容器。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <form class="container-fluid"> <div class="input-group"> <span class="input-group-text" id="basic-addon1">#</span> <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1"> </div> </form> </nav> </body> </html>
导航栏表单支持各种按钮。您可以使用垂直对齐实用工具类来对齐不同大小的元素。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <form class="container-fluid justify-content-start"> <button class="btn btn-outline-primary me-2" type="button">Large Button</button> <button class="btn btn-sm btn-outline-secondary" type="button">Small Button</button> </form> </nav> </body> </html>
文本
添加.navbar-text类以调整文本字符串的垂直对齐和水平间距。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <div class="container-fluid"> <span class="navbar-text"> Welcome to the Tutorialspoint! </span> </div> </nav> </body> </html>
根据需要使用其他组件和实用工具类进行自定义,如下所示。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar w/ text</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarText"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contanct us</a> </li> </ul> <span class="navbar-text"> Welcome to Tutorialspoint! </span> </div> </div> </nav> </body> </html>
配色方案
v5.3.0 中新的深色导航栏——已弃用.navbar-dark,改用新的data-bs-theme="dark"。在您的.navbar中包含data-bs-theme="dark"以启用组件特定的颜色模式。
v5.2.0 新增功能 - 导航栏主题现在由 CSS 变量提供支持,并且.navbar-light已弃用。一个 CSS 变量应用于.navbar以使其默认具有“浅色”外观,并且可以在.navbar-dark中被覆盖。
默认情况下,导航栏是“浅色导航栏”,用于浅色背景颜色。
对父.navbar类使用data-bs-theme="dark"以获得深色背景颜色。
最后,使用.bg-*实用工具类进行自定义。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-dark" data-bs-theme="dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contanct us</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> </div> </div> </nav> <nav class="navbar bg-primary" data-bs-theme="dark"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contanct us</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> </div> </div> </nav> <nav class="navbar" style="background-color: #e3f2fd;"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Contanct us</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> </div> </div> </nav> </body> </html>
容器
您可以选择将导航栏包裹在一个.container中,并将其居中在页面上。但是,请注意,仍然需要内部容器。
在.navbar内添加一个容器以居中您的固定或静态顶部导航栏内容。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container"> <nav class="navbar navbar-expand-lg bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="###">Tutorialspoint</a> </div> </nav> </div> </body> </html>
要修改导航栏中内容的宽度显示方式,请使用任何响应式容器。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-md"> <a class="navbar-brand" href="#">Tutorialspoint</a> </div> </nav> </body> </html>
位置
使用位置辅助类将导航栏定位在非静态位置。可以选择固定到顶部、固定到底部、固定到顶部(滚动页面到顶部并停留在那里)或粘性到底部(滚动直到页面到达底部并停留在那里)。
固定导航栏使用Position: Fixed。这意味着它从 DOM 的正常流程中分离出来,可能需要自定义 CSS(例如,在 上使用 padding-top)以避免与其他元素重叠。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Default Navbar</a> </div> </nav> </body> </html>
固定顶部
这是使用.fixed-top类实现的。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar fixed-top bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar Fixed at Top</a> </div> </nav> </body> </html>
固定底部
这是使用.fixed-bottom类实现的。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container-fluid"></div> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <nav class="navbar fixed-bottom bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar Fixed at Bottom</a> </div> </nav> </div> </body> </html>
粘性顶部
这是使用.sticky-top类实现的。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar sticky-top bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar Stick at Top</a> </div> </nav> </body> </html>
粘性底部
这是使用.sticky-bottom类实现的。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Card</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container-fluid"> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <p>Twitter Bootstrap is the most popular front end framework in the recent time. It is sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript. This tutorial will teach you the basics of Bootstrap Framework using which you can create web projects with ease. The tutorial is divided into sections such as Bootstrap Basic Structure, Bootstrap CSS, Bootstrap Layout Components and Bootstrap Plugins. Each of these sections contain related topics with simple and useful examples.</p> <nav class="navbar sticky-bottom bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Sticky bottom</a> </div> </nav> </div> </body> </html>
滚动
要在折叠导航栏的可切换内容内允许垂直滚动,请向.navbar-nav添加.navbar-nav-scroll类。
默认情况下,滚动高度为 75vh(视口高度的 75%)。您可以使用 CSS 属性--bs-navbar-height来覆盖高度。
使用.navbar-nav-scroll和style="--bs-scroll-height: 100px;"的导航栏,以及一些额外的边距实用工具类以实现最佳间距。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Tutorialspoint</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarScroll" aria-controls="navbarScroll" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarScroll"> <ul class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll" style="--bs-scroll-height: 100px;"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Blog </a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Blog 1</a></li> <li><a class="dropdown-item" href="#">Blog 2</a></li> </ul> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search here..." aria-label="Search"> <button class="btn btn-outline-warning" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
响应式行为
导航栏使用.navbar-toggler、.navbar-collapse和.navbar-expand{-sm|-md|-lg|-xl|-xxl}类来确定其内容在按钮后方折叠。
对于从不折叠的导航栏,请在导航栏上添加.navbar-expand类。对于始终折叠的导航栏,请不要添加.navbar-expand类。
切换器
默认情况下,导航栏切换器左对齐,但如果它们位于像.navbar-brand这样的兄弟元素之后,它将自动右对齐。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-light"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarTogglerDemo01"> <a class="navbar-expand" href="#">Tutorialspoint</a> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-outline-warning" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
品牌名称在左侧,切换器在右侧
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-fluid"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarTogglerDemo02"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Register here</a> </li> <li class="nav-item"> <a class="nav-link disabled">About us</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
品牌名称在右侧,切换器在左侧。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="#">Navbar</a> <div class="collapse navbar-collapse" id="navbarTogglerDemo03"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About us</a> </li> <li class="nav-item"> <a class="nav-link disabled">Contanct us</a> </li> </ul> <form class="d-flex" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
外部内容
你需要使用折叠插件来触发容器元素的内容,这些内容在结构上位于.navbar之外。这很容易,因为我们的插件适用于id和data-bs-target的目标匹配。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="collapse" id="navbarToggleExternalContent"> <div class="bg-primary p-4"> <h5 class="text-white h4">Collapsed content</h5> <span class="text-body-light">Toggleable via the navbar brand</span> </div> </div> <nav class="navbar navbar-dark bg-primary"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> </div> </nav> </body> </html>
侧边栏 (Offcanvas)
将展开和折叠的导航栏转换为侧边栏抽屉,无需 offcanvas 组件。要扩展 offcanvas 默认样式,请添加类.navbar-expand-*,这将创建一个动态且灵活的导航侧边栏。
创建一个在所有断点处都折叠的侧边栏导航栏,并完全跳过.navbar-expand-*类。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar bg-body-tertiary fixed-top"> <div class="container-fluid"> <a class="navbar-brand" href="#">Offcanvas navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasNavbar" aria-labelledby="offcanvasNavbarLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <ul class="navbar-nav justify-content-end flex-grow-1 pe-3"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About us</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Sign in</a></li> <li><a class="dropdown-item" href="#">Sign out</a></li> <li> <hr class="dropdown-divider"> </li> <li><a class="dropdown-item" href="#">Others</a></li> </ul> </li> </ul> <form class="d-flex mt-3" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </div> </nav> </body> </html>
要构建一个在精确断点处扩展为普通导航栏的侧边栏导航栏,请使用.navbar-expand-lg。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-body-tertiary fixed-top"> <a class="navbar-brand" href="#">Offcanvas navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#navbarOffcanvasLg" aria-controls="navbarOffcanvasLg" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="offcanvas offcanvas-end" tabindex="-1" id="navbarOffcanvasLg" aria-labelledby="navbarOffcanvasLgLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Offcanvas</h5> <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <ul class="navbar-nav justify-content-end flex-grow-1 pe-3"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About us</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a> <ul class="dropdown-menu"> <li><a class="dropdown-item" href="#">Sign in</a></li> <li><a class="dropdown-item" href="#">Sign out</a></li> <li> <hr class="dropdown-divider"> </li> <li><a class="dropdown-item" href="#">Others</a></li> </ul> </li> </ul> <form class="d-flex mt-3" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-outline-success" type="submit">Search</button> </form> </div> </div> </nav> </body> </html>
为避免深色导航栏中的文本难以阅读,请在侧边栏内容中使用深色背景。在以下示例中
向.navbar添加了类.navbar-dark和.bg-dark。
向.offcanvas添加了类.text-bg-dark。
向.dropdown-menu添加了类.dropdown-menu-dark。
并向.btn-close添加了类.btn-close-white。
您可以使用编辑并运行选项编辑并运行此代码。
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap - Navbar</title> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net.cn/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <nav class="navbar navbar-dark bg-dark fixed-top"> <div class="container-fluid"> <a class="navbar-brand" href="#">Offcanvas dark navbar</a> <button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasDarkNavbar" aria-controls="offcanvasDarkNavbar" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="offcanvas offcanvas-end text-bg-dark" tabindex="-1" id="offcanvasDarkNavbar" aria-labelledby="offcanvasDarkNavbarLabel"> <div class="offcanvas-header"> <h5 class="offcanvas-title" id="offcanvasDarkNavbarLabel">Dark offcanvas</h5> <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close"></button> </div> <div class="offcanvas-body"> <ul class="navbar-nav justify-content-end flex-grow-1 pe-3"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#">About us</a> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown </a> <ul class="dropdown-menu dropdown-menu-dark"> <li><a class="dropdown-item" href="#">Sign in</a></li> <li><a class="dropdown-item" href="#">Sign out</a></li> <li> <hr class="dropdown-divider"> </li> <li><a class="dropdown-item" href="#">Other's</a></li> </ul> </li> </ul> <form class="d-flex mt-3" role="search"> <input class="form-control me-2" type="search" placeholder="Search here" aria-label="Search"> <button class="btn btn-success" type="submit">Search</button> </form> </div> </div> </div> </nav> </body> </html>