Navbar · Bootstrap v4 中文文档 v4.6

您所在的位置:网站首页 mr中文意思翻译 Navbar · Bootstrap v4 中文文档 v4.6

Navbar · Bootstrap v4 中文文档 v4.6

2023-11-18 00:59| 来源: 网络整理| 查看: 265

View on GitHub Navbar

Documentation and examples for Bootstrap’s powerful, responsive navigation header, the navbar. Includes support for branding, navigation, collapse plugin, and more.

How it works

Here’s what you need to know before getting started with the navbar:

Navbars require a wrapping .navbar with .navbar-expand{-sm|-md|-lg|-xl} for responsive collapsing and color scheme classes. Navbars and their contents are fluid by default. Use optional containers to limit their horizontal width. Use our spacing and flex utility classes for controlling spacing and alignment within navbars. Navbars are responsive by default, but you can easily modify them to change that. Responsive behavior depends on our Collapse JavaScript plugin. Navbars are hidden by default when printing. Force them to be printed by adding .d-print to the .navbar. See the display utility class. Ensure accessibility by using a element or, if using a more generic element such as a , add a role="navigation" to every navbar to explicitly identify it as a landmark region for users of assistive technologies. The animation effect of this component is dependent on the 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 for your company, product, or project name. .navbar-nav for a full-height and lightweight navigation (including support for dropdowns). .navbar-toggler for use with our collapse plugin and other navigation toggling behaviors. .form-inline for any form controls and actions. .navbar-text for adding vertically centered strings of text. .collapse.navbar-collapse for grouping and hiding navbar contents by a parent breakpoint.

Here’s an example of all the sub-components included in a responsive light-themed navbar that automatically collapses at the lg (large) breakpoint.

Navbar Home (current) Link Dropdown Action Another action Something else here Disabled Search

This example uses color (bg-light) and spacing (my-2, my-lg-0, mr-sm-0, my-sm-0) utility classes.

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.

Navbar Navbar

Adding images to the .navbar-brand will likely always require custom styles or utilities to properly size. Here are some examples to demonstrate.

Bootstrap 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.

Active states—with .active—to indicate the current page can be applied directly to .nav-links or their immediate parent .nav-items.

Navbar Home (current) Features Pricing Disabled

And because we use classes for our navs, you can avoid the list-based approach entirely if you like.

Navbar Home (current) Features Pricing Disabled

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.

Navbar Home (current) Features Pricing Dropdown link Action Another action Something else here Forms

Place various form controls and components within a navbar with .form-inline.

Search

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.

Navbar Search

Input groups work, too:

@

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.

Main button Smaller button 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.

Navbar text with an inline element

Mix and match with other components and utilities as needed.

Navbar w/ text Home (current) Features Pricing Navbar text with an inline element Color schemes

Theming the navbar has never been easier thanks to the combination of theming classes and background-color utilities. Choose from .navbar-light for use with light background colors, or .navbar-dark for dark background colors. Then, customize with .bg-* utilities.

Containers

Although it’s not required, you can wrap a navbar in a .container to center it on a page. Or you can add a container inside the .navbar to only center the contents of a fixed or static top navbar.

Navbar

When the container is within your navbar, its horizontal padding is removed at breakpoints lower than your specified .navbar-expand{-sm|-md|-lg|-xl} class. This ensures we’re not doubling up on padding unnecessarily on lower viewports when your navbar is collapsed.

Navbar Placement

Use our position utilities to place navbars in non-static positions. Choose from fixed to the top, fixed to the bottom, or stickied to the top (scrolls with the page until it reaches the top, then stays there). 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 ) to prevent overlap with other elements.

Also note that .sticky-top uses position: sticky, which isn’t fully supported in every browser.

Default Fixed top Fixed bottom Sticky top Scrolling

Add .navbar-nav-scroll to a .navbar-collapse (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 inline or custom styles. At larger viewports when the navbar is expanded, content will appear as it does in a default navbar.

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.

Here’s an example navbar using .navbar-nav-scroll with style="max-height: 100px;", with some extra margin utilities for optimum spacing.

Navbar scroll Home (current) Link Link Action Another action Something else here Link Search Responsive behaviors

Navbars can use .navbar-toggler, .navbar-collapse, and .navbar-expand{-sm|-md|-lg|-xl} 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.

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 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.

With no .navbar-brand shown at the smallest breakpoint:

Hidden brand Home (current) Link Disabled Search

With a brand name shown on the left and toggler on the right:

Navbar Home (current) Link Disabled Search

With a toggler on the left and brand name on the right:

Navbar Home (current) Link Disabled Search External content

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-target matching, that’s easily done!

Collapsed content Toggleable via the navbar brand. Collapsed content Toggleable via the navbar brand.

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.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3