Navbar
Nā palapala a me nā hiʻohiʻona no ke poʻomanaʻo hoʻokele ikaika a Bootstrap, ka navbar. Loaʻa i ke kākoʻo no ka hōʻailona ʻana, ka hoʻokele ʻana, a me nā mea hou aku, me ke kākoʻo no kā mākou plugin collapse.
Pehea e hana ai
Eia ka mea e pono ai ʻoe e ʻike ma mua o ka hoʻomaka ʻana me ka navbar:
- Pono nā Navbars i kahi kāʻei
.navbar
me.navbar-expand{-sm|-md|-lg|-xl|-xxl}
nā papa hāʻule pane a me nā papa kala . - ʻO nā Navbars a me kā lākou mau ʻike he wai ma ka paʻamau. E hoʻololi i ka ipu e kaupalena i ko lākou ākea ākea ma nā ʻano like ʻole.
- E hoʻohana i kā mākou papa hana hoʻokaʻawale a hoʻololi no ka hoʻomalu ʻana i ka spacing a me ke kaulike ʻana i loko o nā navbars .
- Hoʻololi nā Navbars ma ka maʻamau, akā hiki iā ʻoe ke hoʻololi maʻalahi iā lākou e hoʻololi i kēlā. ʻO ke ʻano pane e pili ana i kā mākou Collapse JavaScript plugin.
- E hōʻoia i ka loaʻa ʻana ma o ka hoʻohana ʻana i kahi
<nav>
mea a i ʻole, inā e hoʻohana ana i kahi mea maʻamau e like me ka<div>
, e hoʻohui i kahirole="navigation"
i kēlā me kēia navbar e hoʻomaopopo maopopo iā ia he ʻāina ʻāina no nā mea hoʻohana i nā ʻenehana kōkua. - E hōʻike i ka mea o kēia manawa ma ka hoʻohana ʻana
aria-current="page"
no ka ʻaoʻao o kēia manawa a i ʻolearia-current="true"
ka mea i kēia manawa i kahi set. - Mea hou ma v5.2.0: Hiki ke hoʻohālikelike ʻia nā Navbars me nā mea hoʻololi CSS i hoʻopili ʻia i ka
.navbar
papa kumu..navbar-light
ua hoʻopau ʻia a.navbar-dark
ua kākau hou ʻia e kāpae i nā mea hoʻololi CSS ma mua o ka hoʻohui ʻana i nā kaila hou.
prefers-reduced-motion
nīnau media. E ʻike
i ka ʻāpana hoʻohaʻahaʻa o kā mākou palapala hiki ke komo .
Maʻiʻo i kākoʻo ʻia
Hele mai nā Navbars me ke kākoʻo i kūkulu ʻia no ka liʻiliʻi o nā ʻāpana. E koho mai i ka mea e pono ai:
.navbar-brand
no kāu hui, huahana, a i ʻole inoa papahana..navbar-nav
no ka hoʻokele piha kiʻekiʻe a me ka māmā (me ke kākoʻo no nā hāʻule iho)..navbar-toggler
no ka hoʻohana ʻana me kā mākou plugin collapse a me nā ʻano hoʻokele hoʻokele ʻē aʻe .- Hoʻololi a me ka hoʻokaʻawale ʻana i nā pono no kēlā me kēia ʻano kaohi a me nā hana.
.navbar-text
no ka hoʻohui ʻana i nā kaula kikoʻī o ka kikokikona..collapse.navbar-collapse
no ka hui pū ʻana a me ka hūnā ʻana i nā maʻiʻo o ka navbar e kahi ʻāpana makua.- E hoʻohui i kahi koho
.navbar-scroll
e hoʻonoho i kahimax-height
maʻiʻo navbar i hoʻonui ʻia .
Eia kekahi laʻana o nā ʻāpana ʻāpana āpau i hoʻokomo ʻia i loko o kahi navbar hoʻonaʻauao māmā e hāʻule ʻakomi i ka wahi haʻi lg
(nui).
<nav class="navbar navbar-expand-lg bg-light">
<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">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>
Ke hoʻohana nei kēia hiʻohiʻona i nā papa hoʻohana hope ( bg-light
) a me ka spacing ( me-auto
, mb-2
, mb-lg-0
, me-2
).
Brand
Hiki .navbar-brand
ke hoʻohana ʻia i ka hapa nui o nā mea, akā ʻoi aku ka maikaʻi o kahi heleuma, no ka mea e koi ana kekahi mau mea i nā papa pono a i ʻole nā ʻano maʻamau.
kikokikona
Hoʻohui i kāu kikokikona i loko o kahi mea me ka .navbar-brand
papa.
<!-- As a link -->
<nav class="navbar bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
<!-- As a heading -->
<nav class="navbar bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Navbar</span>
</div>
</nav>
Kiʻi
Hiki iā ʻoe ke pani i ka kikokikona i loko o ka .navbar-brand
me kahi <img>
.
<nav class="navbar bg-light">
<div class="container">
<a class="navbar-brand" href="#">
<img src="/docs/5.2/assets/brand/bootstrap-logo.svg" alt="Bootstrap" width="30" height="24">
</a>
</div>
</nav>
Kiʻi a me ka kikokikona
Hiki iā ʻoe ke hoʻohana i kekahi mau mea pono e hoʻohui i kahi kiʻi a me kahi kikokikona i ka manawa like. E nānā i ka hoʻohui ʻana o .d-inline-block
a .align-text-top
ma ka <img>
.
<nav class="navbar bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="/docs/5.2/assets/brand/bootstrap-logo.svg" alt="Logo" width="30" height="24" class="d-inline-block align-text-top">
Bootstrap
</a>
</div>
</nav>
Nav
Hoʻokumu nā loulou hoʻokele Navbar i kā mākou mau .nav
koho me kā lākou papa hoʻololi ponoʻī a koi i ka hoʻohana ʻana i nā papa toggler no ke ʻano pane kūpono. E ulu pū ana ka hoʻokele ma nā navbar e noho i kahi ākea ākea e hiki ai ke hoʻopaʻa paʻa i kāu ʻike navbar.
Hoʻohui i ka .active
papa .nav-link
e hōʻike i ka ʻaoʻao o kēia manawa.
E ʻoluʻolu, pono ʻoe e hoʻohui i ke aria-current
ʻano ma ka active .nav-link
.
<nav class="navbar navbar-expand-lg bg-light">
<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">Disabled</a>
</li>
</ul>
</div>
</div>
</nav>
A no ka mea, hoʻohana mākou i nā papa no kā mākou navs, hiki iā ʻoe ke pale i ka papa inoa-papa inoa holoʻokoʻa inā makemake ʻoe.
<nav class="navbar navbar-expand-lg bg-light">
<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">Disabled</a>
</div>
</div>
</div>
</nav>
Hiki iā ʻoe ke hoʻohana i nā dropdown i kāu navbar. Pono nā menus dropdown i kahi mea hoʻopili no ka hoʻonohonoho ʻana, no laila e hoʻohana pono i nā mea ʻokoʻa a pūnana no .nav-item
a .nav-link
e like me ka mea i hōʻike ʻia ma lalo nei.
<nav class="navbar navbar-expand-lg bg-light">
<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>
Nā palapala
E kau i nā ʻano mana like ʻole a me nā ʻāpana i loko o kahi navbar:
<nav class="navbar bg-light">
<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>
E hoʻohana i nā ʻano keiki ponoʻī e .navbar
hoʻohana i ka hoʻonohonoho flex a e paʻa i ka justify-content: space-between
. E hoʻohana i nā pono hana maʻalahi e like me ka mea e pono ai e hoʻoponopono i kēia ʻano.
<nav class="navbar bg-light">
<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>
Hana pū kekahi pūʻulu hoʻokomo. Inā he ʻano holoʻokoʻa kāu navbar, a i ʻole ka hapa nui o ke ʻano, hiki iā ʻoe ke hoʻohana i ka <form>
mea e like me ka ipu a mālama i kekahi HTML.
<nav class="navbar bg-light">
<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>
Kākoʻo ʻia nā pihi like ʻole ma ke ʻano o kēia mau ʻano navbar. He mea hoʻomanaʻo maikaʻi hoʻi kēia e hiki ke hoʻohana ʻia nā pono hana alignment vertical e hoʻohālikelike i nā mea nui like ʻole.
<nav class="navbar bg-light">
<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>
kikokikona
Loaʻa paha i nā Navbars nā ʻāpana kikokikona me ke kōkua o .navbar-text
. Hoʻoponopono kēia papa i ka hoʻopololei kū pololei a me ka hakahaka ākea no nā kaula kikokikona.
<nav class="navbar bg-light">
<div class="container-fluid">
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</nav>
Hoʻohui a hoʻohālikelike me nā mea ʻē aʻe a me nā pono hana e like me ka mea e pono ai.
<nav class="navbar navbar-expand-lg bg-light">
<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>
Nā kala kala
.navbar-light
ua hoʻopau ʻia. Hoʻohana ʻia nā ʻano hoʻololi CSS i ka
.navbar
, ke kuhi hewa ʻana i ke ʻano "mālamalama", a hiki ke hoʻopau ʻia me
.navbar-dark
.
ʻOi aku ka maʻalahi o nā kumumanaʻo Navbar ma mua o ka hui pū ʻana o Bootstrap o Sass a me CSS. ʻO ka mea paʻamau ʻo kā mākou "navbar māmā" no ka hoʻohana ʻana me nā kala ʻulaʻula māmā, akā hiki iā ʻoe ke noi .navbar-dark
no nā kala ʻeleʻele. A laila, hoʻonohonoho me .bg-*
nā pono hana.
<nav class="navbar navbar-dark bg-dark">
<!-- Navbar content -->
</nav>
<nav class="navbar navbar-dark bg-primary">
<!-- Navbar content -->
</nav>
<nav class="navbar" style="background-color: #e3f2fd;">
<!-- Navbar content -->
</nav>
Nā pahu pahu
ʻOiai ʻaʻole koi ʻia, hiki iā ʻoe ke hoʻopaʻa i kahi navbar i loko o kahi .container
kikowaena ma kahi ʻaoʻao-ʻoiai e hoʻomaopopo ʻoe e koi ʻia kahi pahu i loko. A i ʻole hiki iā ʻoe ke hoʻohui i kahi pahu i loko o ka mea .navbar
e hoʻokomo wale i nā ʻike o kahi navbar luna paʻa a paʻa paha .
<div class="container">
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
</div>
E hoʻohana i kekahi o nā pahu pane e hoʻololi i ka laulā o ka ʻike ma kāu navbar i hōʻike ʻia.
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-md">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
Hoʻokomo
E hoʻohana i kā mākou mau pono kūlana e kau i nā navbars i nā kūlana non-static. E koho mai ka paʻa i luna, hoʻopaʻa ʻia i lalo, pipili i luna (owili me ka ʻaoʻao a hiki i luna, a laila noho ma laila), a i ʻole pipili i lalo (owili me ka ʻaoʻao a hiki i lalo, a laila noho. ma laila).
Hoʻohana ʻia nā navbars paʻa position: fixed
, ʻo ia hoʻi, ua huki ʻia lākou mai ke kahe maʻamau o ka DOM a makemake paha i ka CSS maʻamau (e laʻa, padding-top
ma ka <body>
) e pale i ka uhi ʻana me nā mea ʻē aʻe.
<nav class="navbar bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Default</a>
</div>
</nav>
<nav class="navbar fixed-top bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed top</a>
</div>
</nav>
<nav class="navbar fixed-bottom bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Fixed bottom</a>
</div>
</nav>
<nav class="navbar sticky-top bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Sticky top</a>
</div>
</nav>
<nav class="navbar sticky-bottom bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Sticky bottom</a>
</div>
</nav>
Owili
Hoʻohui .navbar-nav-scroll
i kahi .navbar-nav
(a i ʻole ʻāpana navbar ʻē aʻe) e hiki ai i ka ʻōwili kū pololei i loko o nā mea hiki ke hoʻololi ʻia o kahi navbar hāʻule. ʻO ka mea paʻamau, hoʻomaka ka ʻōwili ʻana ma 75vh
(a i ʻole 75% o ke kiʻekiʻe o ka viewport), akā hiki iā ʻoe ke hoʻopau i kēlā me ka waiwai maʻamau CSS kūloko --bs-navbar-height
a i ʻole nā ʻano maʻamau. Ma nā puka ʻike nui ke hoʻonui ʻia ka navbar, e ʻike ʻia ka ʻike e like me ia ma kahi navbar paʻamau.
E ʻoluʻolu e hele mai kēia ʻano me kahi drawback o overflow
—i ka hoʻonohonoho ʻana overflow-y: auto
(pono e ʻōwili i ka maʻiʻo ma aneʻi), overflow-x
ʻo ia ka mea like me auto
, e ʻoki i kekahi maʻiʻo ākea.
Eia kekahi laʻana navbar e hoʻohana ana .navbar-nav-scroll
me style="--bs-scroll-height: 100px;"
, me kekahi mau mea pono palena palena ʻē aʻe no ka spacing maikaʻi loa.
<nav class="navbar navbar-expand-lg bg-light">
<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">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>
Nā hana pane
Hiki i nā Navbars ke hoʻohana i ka .navbar-toggler
, .navbar-collapse
, a me .navbar-expand{-sm|-md|-lg|-xl|-xxl}
nā papa e hoʻoholo ai i ka wā e hāʻule ai kā lākou ʻike ma hope o kahi pihi. I ka hui pū ʻana me nā pono hana ʻē aʻe, hiki iā ʻoe ke koho maʻalahi i ka wā e hōʻike ai a hūnā i nā mea kikoʻī.
No nā navbar ʻaʻole e hāʻule, hoʻohui i ka .navbar-expand
papa ma ka navbar. No nā navbar e hāʻule mau ana, mai hoʻohui i kekahi .navbar-expand
papa.
Mea hoʻololi
Hoʻopaʻa ʻia nā mea hoʻololi Navbar ma ke ʻano paʻamau, akā inā e hahai lākou i kahi ʻāpana kaikaina e like me ka .navbar-brand
, e hoʻopili ʻia lākou i ka ʻaoʻao ʻākau loa. ʻO ka hoʻohuli ʻana i kāu markup e hoʻohuli i ke kau ʻana o ka toggler. Aia ma lalo nā laʻana o nā ʻano hoʻololi like ʻole.
Me ka .navbar-brand
hōʻike ʻole ʻia ma ka wahi haʻi liʻiliʻi loa:
<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-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">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>
Me ka inoa hōʻailona i hōʻike ʻia ma ka hema a me ka toggle ma ka ʻākau:
<nav class="navbar navbar-expand-lg bg-light">
<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">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>
Me ka mea hoʻololi ma ka hema a me ka inoa inoa ma ka ʻākau:
<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="#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">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>
Maʻiʻo waho
I kekahi manawa makemake ʻoe e hoʻohana i ka plugin collapse e hoʻomaka i kahi mea pahu no ka ʻike e noho ana ma waho o ka .navbar
. No ka mea e hana ana kā mākou plugin i ka id
a me ka data-bs-target
hoʻohālikelike ʻana, maʻalahi ia hana!
<div class="collapse" id="navbarToggleExternalContent">
<div class="bg-dark p-4">
<h5 class="text-white h4">Collapsed content</h5>
<span class="text-muted">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>
Ke hana ʻoe i kēia, manaʻo mākou e hoʻokomo i ka JavaScript hou e hoʻoneʻe i ka manaʻo programmatically i ka ipu ke wehe ʻia. A i ʻole, paʻakikī paha nā mea hoʻohana keyboard a me nā mea hoʻohana i nā ʻenehana kōkua i ka ʻimi ʻana i ka ʻike hou i hōʻike ʻia - ʻoiai inā ʻo ka ipu i wehe ʻia ma mua o ka mea hoʻololi i ke ʻano o ka palapala. Manaʻo pū mākou e hōʻoia i ka mea hoʻololi i ke ʻano aria-controls
, e kuhikuhi ana i ka id
pahu maʻiʻo. Ma ke kumumanaʻo, ʻae kēia i nā mea hoʻohana ʻenehana kōkua e lele pololei mai ka toggler a i ka ipu āna e hoʻomalu ai - akā paʻakikī ke kākoʻo no kēia.
Offcanvas
E hoʻololi i kāu navbar e hoʻonui ana a e hāʻule ana i loko o kahi pahu huki waho me ka ʻāpana offcanvas . Hoʻonui mākou i nā ʻano paʻamau offcanvas a hoʻohana i kā mākou .navbar-expand-*
papa e hana i kahi ʻaoʻao hoʻokele ikaika a maʻalahi.
Ma ka laʻana ma lalo nei, no ka hana ʻana i kahi navbar offcanvas i hāʻule mau ma nā wahi haʻi āpau, e haʻalele i ka .navbar-expand-*
papa.
<nav class="navbar bg-light 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">
<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" 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>
No ka hana ʻana i kahi navbar offcanvas e hoʻonui i kahi navbar maʻamau i kahi haʻi kikoʻī e like me lg
, hoʻohana .navbar-expand-lg
.
<nav class="navbar navbar-expand-lg bg-light 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">
<span class="navbar-toggler-icon"></span>
</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="navbarOffcanvasLg" aria-labelledby="navbarOffcanvasLgLabel">
...
</div>
</nav>
I ka hoʻohana ʻana i ka offcanvas ma kahi navbar ʻeleʻele, e makaʻala pono ʻoe e loaʻa i kahi ʻaoʻao ʻeleʻele ma luna o ka maʻiʻo offcanvas e pale aku i ka lilo ʻole o ka kikokikona. Ma ka laʻana ma lalo nei, hoʻohui mākou .navbar-dark
a .bg-dark
i ka .navbar
, .text-bg-dark
i ka .offcanvas
, .dropdown-menu-dark
i .dropdown-menu
, a .btn-close-white
i .btn-close
no ke kāhili pono ʻana me kahi kāwele ʻeleʻele.
<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">
<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" 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
Nā mea hoʻololi
Hoʻohui ʻia ma v5.2.0Ma ke ʻano o ka hoʻololi ʻana o nā ʻano hoʻololi CSS o Bootstrap, hoʻohana nā navbars i kēia manawa i nā ʻano CSS kūloko no ka hoʻomaikaʻi ʻana i ka manawa .navbar
maoli. Hoʻonohonoho ʻia nā waiwai no nā ʻano CSS ma o Sass, no laila ke kākoʻo mau ʻia nei ka hana maʻamau Sass.
--#{$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};
Aia kekahi mau mea hoʻololi CSS hou ma .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);
Hiki ke ʻike ʻia ka hana maʻamau ma o nā ʻano CSS ma ka .navbar-dark
papa kahi e hoʻopau ai mākou i nā waiwai kikoʻī me ka ʻole o ka hoʻohui ʻana i nā mea koho CSS kope.
--#{$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)};
Nā hoʻololi Sass
Nā hoʻololi no nā navbar a pau:
$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($black, .55);
$navbar-light-hover-color: rgba($black, .7);
$navbar-light-active-color: rgba($black, .9);
$navbar-light-disabled-color: rgba($black, .3);
$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-color}' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/></svg>");
$navbar-light-toggler-border-color: rgba($black, .1);
$navbar-light-brand-color: $navbar-light-active-color;
$navbar-light-brand-hover-color: $navbar-light-active-color;
Nā hoʻololi no ka navbar ʻeleʻele :
$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-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-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 loop
Hoʻohui ʻia nā papa hoʻonui/holo navbar pane (e laʻa, .navbar-expand-lg
) me ka $breakpoints
palapala ʻāina a hana ʻia ma o kahi loop i 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;
}
}
}
}
}
}