怎么运行的
¥How it works
在开始使用导航栏之前,你需要了解以下内容:
¥Here’s what you need to know before getting started with the navbar:
-
导航栏需要用
.navbar-expand{-sm|-md|-lg|-xl|-xxl}
封装.navbar
来实现响应式折叠和 配色方案 类。¥Navbars require a wrapping
.navbar
with.navbar-expand{-sm|-md|-lg|-xl|-xxl}
for responsive collapsing and color scheme classes. -
默认情况下,导航栏及其内容是流动的。更改 container 以通过不同方式限制其水平宽度。
¥Navbars and their contents are fluid by default. Change the container to limit their horizontal width in different ways.
-
使用我们的 spacing 和 flex 实用程序类来控制导航栏中的间距和对齐方式。
¥Use our spacing and flex utility classes for controlling spacing and alignment within navbars.
-
导航栏默认情况下是响应式的,但你可以轻松修改它们来更改它。响应行为取决于我们的 Collapse JavaScript 插件。
¥Navbars are responsive by default, but you can easily modify them to change that. Responsive behavior depends on our Collapse JavaScript plugin.
-
通过使用
<nav>
元素确保可访问性,或者,如果使用更通用的元素(例如<div>
),请向每个导航栏添加role="navigation"
,以明确将其标识为辅助技术用户的标志性区域。¥Ensure accessibility by using a
<nav>
element or, if using a more generic element such as a<div>
, add arole="navigation"
to every navbar to explicitly identify it as a landmark region for users of assistive technologies. -
使用
aria-current="page"
表示当前页或使用aria-current="true"
表示集合中的当前项目来指示当前项目。¥Indicate the current item by using
aria-current="page"
for the current page oraria-current="true"
for the current item in a set. -
v5.2.0 中的新功能:导航栏可以使用范围为
.navbar
基类的 CSS 变量进行主题化。.navbar-light
已被弃用,.navbar-dark
已被重写以覆盖 CSS 变量,而不是添加其他样式。¥New in v5.2.0: Navbars can be themed with CSS variables that are scoped to the
.navbar
base class..navbar-light
has been deprecated and.navbar-dark
has been rewritten to override CSS variables instead of adding additional styles.
prefers-reduced-motion
media query. See the reduced motion section of our accessibility documentation.
支持的内容
¥Supported content
导航栏内置了对少数子组件的支持。根据需要从以下选项中进行选择:
¥Navbars come with built-in support for a handful of sub-components. Choose from the following as needed:
-
.navbar-brand
代表你的公司、产品或项目名称。¥
.navbar-brand
for your company, product, or project name. -
.navbar-nav
用于全高和轻量级导航(包括对下拉菜单的支持)。¥
.navbar-nav
for a full-height and lightweight navigation (including support for dropdowns). -
.navbar-toggler
与我们的折叠插件和其他 导航切换 行为一起使用。¥
.navbar-toggler
for use with our collapse plugin and other navigation toggling behaviors. -
适用于任何表单控件和操作的 Flex 和间距工具。
¥Flex and spacing utilities for any form controls and actions.
-
.navbar-text
用于添加垂直居中的文本字符串。¥
.navbar-text
for adding vertically centered strings of text. -
.collapse.navbar-collapse
用于通过父断点分组和隐藏导航栏内容。¥
.collapse.navbar-collapse
for grouping and hiding navbar contents by a parent breakpoint. -
添加可选的
.navbar-scroll
以设置max-height
和 滚动展开的导航栏内容。¥Add an optional
.navbar-scroll
to set amax-height
and scroll expanded navbar content.
以下是响应式浅色主题导航栏中包含的所有子组件的示例,该导航栏在 lg
(大)断点处自动折叠。
¥Here’s an example of all the sub-components included in a responsive light-themed navbar that automatically collapses at the lg
(large) breakpoint.
<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="#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" href="#">Link</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="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
此示例使用 background (bg-body-tertiary
) 和 spacing (me-auto
, mb-2
, mb-lg-0
, me-2
) 实用程序类。
¥This example uses background (bg-body-tertiary
) and spacing (me-auto
, mb-2
, mb-lg-0
, me-2
) utility classes.
品牌
¥Brand
.navbar-brand
可以应用于大多数元素,但锚点效果最好,因为某些元素可能需要工具类或自定义样式。
¥The .navbar-brand
can be applied to most elements, but an anchor works best, as some elements might require utility classes or custom styles.
文本
¥Text
在具有 .navbar-brand
类的元素中添加文本。
¥Add your text within an element with the .navbar-brand
class.
<!-- As a link -->
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
<!-- As a heading -->
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Navbar</span>
</div>
</nav>
图片
¥Image
你可以将 .navbar-brand
中的文本替换为 <img>
。
¥You can replace the text within the .navbar-brand
with an <img>
.
<nav class="navbar bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="#">
<img src="/docs/5.3/assets/brand/bootstrap-logo.svg" alt="Bootstrap" width="30" height="24">
</a>
</div>
</nav>
图片和文本
¥Image and text
你还可以使用一些附加工具同时添加图片和文本。请注意 <img>
上添加了 .d-inline-block
和 .align-text-top
。
¥You can also make use of some additional utilities to add an image and text at the same time. Note the addition of .d-inline-block
and .align-text-top
on the <img>
.
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="/docs/5.3/assets/brand/bootstrap-logo.svg" alt="Logo" width="30" height="24" class="d-inline-block align-text-top">
Bootstrap
</a>
</div>
</nav>
导航
¥Nav
导航栏导航链接基于我们的 .nav
选项构建,具有自己的修饰符类,并且需要使用 切换器类 来实现正确的响应式样式。导航栏中的导航也将占据尽可能多的水平空间,以保持导航栏内容安全对齐。
¥Navbar navigation links build on our .nav
options with their own modifier class and require the use of toggler classes for proper responsive styling. Navigation in navbars will also grow to occupy as much horizontal space as possible to keep your navbar contents securely aligned.
在 .nav-link
上添加 .active
类以指示当前页面。
¥Add the .active
class on .nav-link
to indicate the current page.
请注意,你还应该在活动的 .nav-link
上添加 aria-current
属性。
¥Please note that you should also add the aria-current
attribute on the active .nav-link
.
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
因为我们使用类作为导航,所以如果你愿意,你可以完全避免基于列表的方法。
¥And because we use classes for our navs, you can avoid the list-based approach entirely if you like.
<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="#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="#">Features</a>
<a class="nav-link" href="#">Pricing</a>
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</div>
</div>
</div>
</nav>
你还可以在导航栏中使用下拉菜单。下拉菜单需要一个环绕元素进行定位,因此请务必对 .nav-item
和 .nav-link
使用单独的嵌套元素,如下所示。
¥You can also use dropdowns in your navbar. Dropdown menus require a wrapping element for positioning, so be sure to use separate and nested elements for .nav-item
and .nav-link
as shown below.
<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="#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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
表单
¥Forms
将各种表单控件和组件放置在导航栏中:
¥Place various form controls and components within a navbar:
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
.navbar
的直接子元素使用 Flex 布局,并且默认为 justify-content: space-between
。根据需要使用其他 flex 实用程序 来调整此行为。
¥Immediate child elements of .navbar
use flex layout and will default to justify-content: space-between
. Use additional flex utilities as needed to adjust this behavior.
<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" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
输入组也可以工作。如果你的导航栏是整个表单,或者主要是一个表单,你可以使用 <form>
元素作为容器并保存一些 HTML。
¥Input groups work, too. If your navbar is an entire form, or mostly a form, you can use the <form>
element as the container and save some HTML.
<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>
这些导航栏表单也支持各种按钮。这也是一个很好的提醒,垂直对齐工具可用于对齐不同大小的元素。
¥Various buttons are supported as part of these navbar forms, too. This is also a great reminder that vertical alignment utilities can be used to align different sized elements.
<nav class="navbar bg-body-tertiary">
<form class="container-fluid justify-content-start">
<button class="btn btn-outline-success me-2" type="button">Main button</button>
<button class="btn btn-sm btn-outline-secondary" type="button">Smaller button</button>
</form>
</nav>
文本
¥Text
在 .navbar-text
的帮助下,导航栏可能包含一些文本。此类调整文本字符串的垂直对齐方式和水平间距。
¥Navbars may contain bits of text with the help of .navbar-text
. This class adjusts vertical alignment and horizontal spacing for strings of text.
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</nav>
根据需要与其他组件和工具混合搭配。
¥Mix and match with other components and utilities as needed.
<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="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</div>
</nav>
配色方案
¥Color schemes
New dark navbars in v5.3.0 — We’ve deprecated .navbar-dark
in favor of the new data-bs-theme="dark"
. Add data-bs-theme="dark"
to the .navbar
to enable a component-specific color mode. Learn more about our color modes.
New in v5.2.0 — Navbar theming is now powered by CSS variables and .navbar-light
has been deprecated. CSS variables are applied to .navbar
, defaulting to the “light” appearance, and can be overridden with .navbar-dark
.
由于 Bootstrap 结合了 Sass 和 CSS 变量,导航栏主题比以往任何时候都更容易。默认值是我们的 “浅色导航栏”,用于浅色背景颜色,但你也可以将 data-bs-theme="dark"
应用于深色背景颜色的 .navbar
父级。然后,使用 .bg-*
和其他工具进行自定义。
¥Navbar themes are easier than ever thanks to Bootstrap’s combination of Sass and CSS variables. The default is our “light navbar” for use with light background colors, but you can also apply data-bs-theme="dark"
to the .navbar
parent for dark background colors. Then, customize with .bg-*
and additional utilities.
<nav class="navbar bg-dark border-bottom border-body" data-bs-theme="dark">
<!-- Navbar content -->
</nav>
<nav class="navbar bg-primary" data-bs-theme="dark">
<!-- Navbar content -->
</nav>
<nav class="navbar" style="background-color: #e3f2fd;" data-bs-theme="light">
<!-- Navbar content -->
</nav>
容器
¥Containers
虽然这不是必需的,但你可以将导航栏封装在 .container
中,使其在页面上居中 - 但请注意,仍然需要内部容器。或者你可以在 .navbar
内添加一个容器,仅将 固定或静态顶部导航栏 的内容居中。
¥Although it’s not required, you can wrap a navbar in a .container
to center it on a page–though note that an inner container is still required. Or you can add a container inside the .navbar
to only center the contents of a fixed or static top navbar.
<div class="container">
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
</div>
使用任何响应式容器来更改导航栏中内容的显示宽度。
¥Use any of the responsive containers to change how wide the content in your navbar is presented.
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-md">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
放置
¥Placement
使用我们的位置实用程序 将导航栏放置在非静态位置。选择固定到顶部、固定到底部、粘到顶部(随页面滚动直到到达顶部,然后停留在那里)或粘到底部(随页面滚动直到到达底部,然后停留在那里)。
¥Use our position utilities to place navbars in non-static positions. Choose from fixed to the top, fixed to the bottom, stickied to the top (scrolls with the page until it reaches the top, then stays there), or stickied to the bottom (scrolls with the page until it reaches the bottom, then stays there).
固定导航栏使用 position: fixed
,这意味着它们是从 DOM 的正常流程中提取的,并且可能需要自定义 CSS(例如 <body>
上的 padding-top
)以防止与其他元素重叠。
¥Fixed navbars use position: fixed
, meaning they’re pulled from the normal flow of the DOM and may require custom CSS (e.g., padding-top
on the <body>
) to prevent overlap with other elements.
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Default</a>
</div>
</nav>
<nav class="navbar fixed-top bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed top</a>
</div>
</nav>
<nav class="navbar fixed-bottom bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed bottom</a>
</div>
</nav>
<nav class="navbar sticky-top bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Sticky top</a>
</div>
</nav>
<nav class="navbar sticky-bottom bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Sticky bottom</a>
</div>
</nav>
滚动
¥Scrolling
将 .navbar-nav-scroll
添加到 .navbar-nav
(或其他导航栏子组件),以在折叠导航栏的可切换内容中启用垂直滚动。默认情况下,滚动在 75vh
(或视口高度的 75%)处开始,但你可以使用本地 CSS 自定义属性 --bs-navbar-height
或自定义样式覆盖它。在较大的视口中,当导航栏展开时,内容将像在默认导航栏中一样显示。
¥Add .navbar-nav-scroll
to a .navbar-nav
(or other navbar sub-component) to enable vertical scrolling within the toggleable contents of a collapsed navbar. By default, scrolling kicks in at 75vh
(or 75% of the viewport height), but you can override that with the local CSS custom property --bs-navbar-height
or custom styles. At larger viewports when the navbar is expanded, content will appear as it does in a default navbar.
请注意,此行为带有 overflow
的潜在缺点 - 当设置 overflow-y: auto
(需要滚动此处的内容)时,overflow-x
相当于 auto
,这会裁剪一些水平内容。
¥Please note that this behavior comes with a potential drawback of overflow
—when setting overflow-y: auto
(required to scroll the content here), overflow-x
is the equivalent of auto
, which will crop some horizontal content.
下面是一个使用 .navbar-nav-scroll
和 style="--bs-scroll-height: 100px;"
的导航栏示例,并使用一些额外的边距工具来实现最佳间距。
¥Here’s an example navbar using .navbar-nav-scroll
with style="--bs-scroll-height: 100px;"
, with some extra margin utilities for optimum spacing.
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar scroll</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="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Link
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Link</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
响应式行为
¥Responsive behaviors
导航栏可以使用 .navbar-toggler
、.navbar-collapse
和 .navbar-expand{-sm|-md|-lg|-xl|-xxl}
类来确定其内容何时折叠在按钮后面。与其他工具结合使用,你可以轻松选择何时显示或隐藏特定元素。
¥Navbars can use .navbar-toggler
, .navbar-collapse
, and .navbar-expand{-sm|-md|-lg|-xl|-xxl}
classes to determine when their content collapses behind a button. In combination with other utilities, you can easily choose when to show or hide particular elements.
对于永不折叠的导航栏,请在导航栏上添加 .navbar-expand
类。对于总是折叠的导航栏,不要添加任何 .navbar-expand
类。
¥For navbars that never collapse, add the .navbar-expand
class on the navbar. For navbars that always collapse, don’t add any .navbar-expand
class.
切换器
¥Toggler
导航栏切换器默认情况下左对齐,但如果它们遵循 .navbar-brand
等同级元素,它们将自动对齐到最右侧。反转标记将反转切换器的位置。以下是不同切换样式的示例。
¥Navbar togglers are left-aligned by default, but should they follow a sibling element like a .navbar-brand
, they’ll automatically be aligned to the far right. Reversing your markup will reverse the placement of the toggler. Below are examples of different toggle styles.
最小断点处没有显示 .navbar-brand
:
¥With no .navbar-brand
shown at the smallest breakpoint:
<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="#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-brand" href="#">Hidden brand</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="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
品牌名称显示在左侧,切换器显示在右侧:
¥With a brand name shown on the left and toggler on the right:
<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="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
左侧有一个切换器,右侧有品牌名称:
¥With a toggler on the left and brand name on the right:
<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="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" aria-disabled="true">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
外部内容
¥External content
有时你想使用折叠插件来触发结构上位于 .navbar
之外的内容的容器元素。 因为我们的插件适用于 id
和 data-bs-target
匹配,所以这很容易完成!
¥Sometimes you want to use the collapse plugin to trigger a container element for content that structurally sits outside of the .navbar
. Because our plugin works on the id
and data-bs-target
matching, that’s easily done!
<div class="collapse" id="navbarToggleExternalContent" data-bs-theme="dark">
<div class="bg-dark p-4">
<h5 class="text-body-emphasis h4">Collapsed content</h5>
<span class="text-body-secondary">Toggleable via the navbar brand.</span>
</div>
</div>
<nav class="navbar navbar-dark bg-dark">
<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>
当你执行此操作时,我们建议添加额外的 JavaScript,以便在打开容器时以编程方式将焦点移动到容器。否则,键盘用户和辅助技术用户可能会很难找到新显示的内容 - 特别是如果打开的容器位于文档结构中的切换器之前。我们还建议确保切换器具有 aria-controls
属性,指向内容容器的 id
。理论上,这允许辅助技术用户直接从切换器跳转到它控制的容器,但目前对此的支持还很不完善。
¥When you do this, we recommend including additional JavaScript to move the focus programmatically to the container when it is opened. Otherwise, keyboard users and users of assistive technologies will likely have a hard time finding the newly revealed content - particularly if the container that was opened comes before the toggler in the document’s structure. We also recommend making sure that the toggler has the aria-controls
attribute, pointing to the id
of the content container. In theory, this allows assistive technology users to jump directly from the toggler to the container it controls–but support for this is currently quite patchy.
Offcanvas
使用 offcanvas 组件 将展开和折叠的导航栏转换为 offcanvas 抽屉。我们扩展了 offcanvas 默认样式并使用 .navbar-expand-*
类来创建动态且灵活的导航侧边栏。
¥Transform your expanding and collapsing navbar into an offcanvas drawer with the offcanvas component. We extend both the offcanvas default styles and use our .navbar-expand-*
classes to create a dynamic and flexible navigation sidebar.
在下面的示例中,要创建一个始终在所有断点上折叠的 offcanvas 导航栏,请完全省略 .navbar-expand-*
类。
¥In the example below, to create an offcanvas navbar that is always collapsed across all breakpoints, omit the .navbar-expand-*
class entirely.
<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="#">Link</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="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form class="d-flex mt-3" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</div>
</nav>
要创建一个在特定断点(如 lg
)处扩展为普通导航栏的画布外导航栏,请使用 .navbar-expand-lg
。
¥To create an offcanvas navbar that expands into a normal navbar at a specific breakpoint like lg
, use .navbar-expand-lg
.
<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>
</nav>
在深色导航栏中使用 Offcanvas 时,请注意,你可能需要在 Offcanvas 内容上使用深色背景,以避免文本变得难以辨认。在下面的示例中,我们将 .navbar-dark
和 .bg-dark
添加到 .navbar
,将 .text-bg-dark
添加到 .offcanvas
,将 .dropdown-menu-dark
添加到 .dropdown-menu
,将 .btn-close-white
添加到 .btn-close
,以便使用深色画布获得正确的样式。
¥When using offcanvas in a dark navbar, be aware that you may need to have a dark background on the offcanvas content to avoid the text becoming illegible. In the example below, we add .navbar-dark
and .bg-dark
to the .navbar
, .text-bg-dark
to the .offcanvas
, .dropdown-menu-dark
to .dropdown-menu
, and .btn-close-white
to .btn-close
for proper styling with a dark offcanvas.
<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="#">Link</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="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
<form class="d-flex mt-3" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-success" type="submit">Search</button>
</form>
</div>
</div>
</div>
</nav>
CSS
变量
¥Variables
Added in v5.2.0作为 Bootstrap 不断发展的 CSS 变量方法的一部分,导航栏现在使用 .navbar
上的本地 CSS 变量来增强实时自定义。CSS 变量的值是通过 Sass 设置的,因此仍然支持 Sass 自定义。
¥As part of Bootstrap’s evolving CSS variables approach, navbars now use local CSS variables on .navbar
for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
--#{$prefix}navbar-padding-x: #{if($navbar-padding-x == null, 0, $navbar-padding-x)};
--#{$prefix}navbar-padding-y: #{$navbar-padding-y};
--#{$prefix}navbar-color: #{$navbar-light-color};
--#{$prefix}navbar-hover-color: #{$navbar-light-hover-color};
--#{$prefix}navbar-disabled-color: #{$navbar-light-disabled-color};
--#{$prefix}navbar-active-color: #{$navbar-light-active-color};
--#{$prefix}navbar-brand-padding-y: #{$navbar-brand-padding-y};
--#{$prefix}navbar-brand-margin-end: #{$navbar-brand-margin-end};
--#{$prefix}navbar-brand-font-size: #{$navbar-brand-font-size};
--#{$prefix}navbar-brand-color: #{$navbar-light-brand-color};
--#{$prefix}navbar-brand-hover-color: #{$navbar-light-brand-hover-color};
--#{$prefix}navbar-nav-link-padding-x: #{$navbar-nav-link-padding-x};
--#{$prefix}navbar-toggler-padding-y: #{$navbar-toggler-padding-y};
--#{$prefix}navbar-toggler-padding-x: #{$navbar-toggler-padding-x};
--#{$prefix}navbar-toggler-font-size: #{$navbar-toggler-font-size};
--#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-light-toggler-icon-bg)};
--#{$prefix}navbar-toggler-border-color: #{$navbar-light-toggler-border-color};
--#{$prefix}navbar-toggler-border-radius: #{$navbar-toggler-border-radius};
--#{$prefix}navbar-toggler-focus-width: #{$navbar-toggler-focus-width};
--#{$prefix}navbar-toggler-transition: #{$navbar-toggler-transition};
.navbar-nav
上还存在一些额外的 CSS 变量:
¥Some additional CSS variables are also present on .navbar-nav
:
--#{$prefix}nav-link-padding-x: 0;
--#{$prefix}nav-link-padding-y: #{$nav-link-padding-y};
@include rfs($nav-link-font-size, --#{$prefix}nav-link-font-size);
--#{$prefix}nav-link-font-weight: #{$nav-link-font-weight};
--#{$prefix}nav-link-color: var(--#{$prefix}navbar-color);
--#{$prefix}nav-link-hover-color: var(--#{$prefix}navbar-hover-color);
--#{$prefix}nav-link-disabled-color: var(--#{$prefix}navbar-disabled-color);
通过 CSS 变量进行的自定义可以在 .navbar-dark
类中看到,我们在不添加重复的 CSS 选择器的情况下覆盖特定值。
¥Customization through CSS variables can be seen on the .navbar-dark
class where we override specific values without adding duplicate CSS selectors.
--#{$prefix}navbar-color: #{$navbar-dark-color};
--#{$prefix}navbar-hover-color: #{$navbar-dark-hover-color};
--#{$prefix}navbar-disabled-color: #{$navbar-dark-disabled-color};
--#{$prefix}navbar-active-color: #{$navbar-dark-active-color};
--#{$prefix}navbar-brand-color: #{$navbar-dark-brand-color};
--#{$prefix}navbar-brand-hover-color: #{$navbar-dark-brand-hover-color};
--#{$prefix}navbar-toggler-border-color: #{$navbar-dark-toggler-border-color};
--#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-dark-toggler-icon-bg)};
Sass 变量
¥Sass variables
所有导航栏的变量:
¥Variables for all navbars:
$navbar-padding-y: $spacer * .5;
$navbar-padding-x: null;
$navbar-nav-link-padding-x: .5rem;
$navbar-brand-font-size: $font-size-lg;
// Compute the navbar-brand padding-y so the navbar-brand will have the same height as navbar-text and nav-link
$nav-link-height: $font-size-base * $line-height-base + $nav-link-padding-y * 2;
$navbar-brand-height: $navbar-brand-font-size * $line-height-base;
$navbar-brand-padding-y: ($nav-link-height - $navbar-brand-height) * .5;
$navbar-brand-margin-end: 1rem;
$navbar-toggler-padding-y: .25rem;
$navbar-toggler-padding-x: .75rem;
$navbar-toggler-font-size: $font-size-lg;
$navbar-toggler-border-radius: $btn-border-radius;
$navbar-toggler-focus-width: $btn-focus-width;
$navbar-toggler-transition: box-shadow .15s ease-in-out;
$navbar-light-color: rgba(var(--#{$prefix}emphasis-color-rgb), .65);
$navbar-light-hover-color: rgba(var(--#{$prefix}emphasis-color-rgb), .8);
$navbar-light-active-color: rgba(var(--#{$prefix}emphasis-color-rgb), 1);
$navbar-light-disabled-color: rgba(var(--#{$prefix}emphasis-color-rgb), .3);
$navbar-light-icon-color: rgba($body-color, .75);
$navbar-light-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-light-icon-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
$navbar-light-toggler-border-color: rgba(var(--#{$prefix}emphasis-color-rgb), .15);
$navbar-light-brand-color: $navbar-light-active-color;
$navbar-light-brand-hover-color: $navbar-light-active-color;
深色导航栏 的变量:
¥Variables for the dark navbar:
$navbar-dark-color: rgba($white, .55);
$navbar-dark-hover-color: rgba($white, .75);
$navbar-dark-active-color: $white;
$navbar-dark-disabled-color: rgba($white, .25);
$navbar-dark-icon-color: $navbar-dark-color;
$navbar-dark-toggler-icon-bg: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'><path stroke='#{$navbar-dark-icon-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
$navbar-dark-toggler-border-color: rgba($white, .1);
$navbar-dark-brand-color: $navbar-dark-active-color;
$navbar-dark-brand-hover-color: $navbar-dark-active-color;
Sass 循环
¥Sass loops
响应式导航栏展开/折叠类(例如,.navbar-expand-lg
)与 $breakpoints
图组合并通过 scss/_navbar.scss
中的循环生成。
¥Responsive navbar expand/collapse classes (e.g., .navbar-expand-lg
) are combined with the $breakpoints
map and generated through a loop in scss/_navbar.scss
.
// Generate series of `.navbar-expand-*` responsive classes for configuring
// where your navbar collapses.
.navbar-expand {
@each $breakpoint in map-keys($grid-breakpoints) {
$next: breakpoint-next($breakpoint, $grid-breakpoints);
$infix: breakpoint-infix($next, $grid-breakpoints);
// stylelint-disable-next-line scss/selector-no-union-class-name
&#{$infix} {
@include media-breakpoint-up($next) {
flex-wrap: nowrap;
justify-content: flex-start;
.navbar-nav {
flex-direction: row;
.dropdown-menu {
position: absolute;
}
.nav-link {
padding-right: var(--#{$prefix}navbar-nav-link-padding-x);
padding-left: var(--#{$prefix}navbar-nav-link-padding-x);
}
}
.navbar-nav-scroll {
overflow: visible;
}
.navbar-collapse {
display: flex !important; // stylelint-disable-line declaration-no-important
flex-basis: auto;
}
.navbar-toggler {
display: none;
}
.offcanvas {
// stylelint-disable declaration-no-important
position: static;
z-index: auto;
flex-grow: 1;
width: auto !important;
height: auto !important;
visibility: visible !important;
background-color: transparent !important;
border: 0 !important;
transform: none !important;
@include box-shadow(none);
@include transition(none);
// stylelint-enable declaration-no-important
.offcanvas-header {
display: none;
}
.offcanvas-body {
display: flex;
flex-grow: 0;
padding: 0;
overflow-y: visible;
}
}
}
}
}
}