Navbar
Sgrìobhainnean agus eisimpleirean airson bann-cinn seòlaidh cumhachdach, freagairteach Bootstrap, an navbar. A’ toirt a-steach taic airson branndadh, seòladh, agus barrachd, a’ toirt a-steach taic don plugan tuiteam againn.
Mar a tha e ag obair
Seo na dh’ fheumas tu a bhith eòlach mus tòisich thu leis a’ bhàr nav:
- Feumaidh navbars còmhdach
.navbar
le.navbar-expand{-sm|-md|-lg|-xl|-xxl}
airson tuiteam freagairteach agus clasaichean sgeama dathan . - Tha navbars agus an susbaint siùbhlach gu bunaiteach. Atharraich an soitheach gus an leud còmhnard aca a chuingealachadh ann an diofar dhòighean.
- Cleachd na clasaichean farsaingeachd agus goireas sùbailte againn airson smachd a chumail air farsaingeachd agus co-thaobhadh taobh a-staigh na bàraichean seòlaidh.
- Tha navbars freagairteach gu bunaiteach, ach is urrainn dhut an atharrachadh gu furasta gus sin atharrachadh. Tha giùlan ciallach an urra ris a’ plugan Collapse JavaScript againn.
- Dèan cinnteach gum bi ruigsinneachd ann le bhith a’ cleachdadh
<nav>
eileamaid no, ma tha thu a’ cleachdadh eileamaid nas gnèitheach leithid<div>
, cuir a risrole="navigation"
a h-uile bàr seòlaidh gus a chomharrachadh gu soilleir mar sgìre shònraichte airson luchd-cleachdaidh teicneòlasan cuideachaidh. - Sònraich an rud làithreach le bhith a’ cleachdadh
aria-current="page"
airson na duilleige làithreach noaria-current="true"
airson an rud làithreach ann an seata. - Ùr ann an v5.2.0: Faodar navbars a bhith air an cuspair le caochladairean CSS a tha air an cuairteachadh don
.navbar
chlas bhunaiteach..navbar-light
air a bhith gun mholadh agus.navbar-dark
chaidh ath-sgrìobhadh gus a dhol thairis air caochladairean CSS an àite stoidhlichean a bharrachd a chur ris.
prefers-reduced-motion
ceist nam meadhanan. Faic an
earrann gluasad lùghdaichte de na sgrìobhainnean ruigsinneachd againn .
Susbaint le taic
Bidh Navbars a’ tighinn le taic togte airson dòrlach de fo-phàirtean. Tagh bho na leanas mar a dh 'fheumar:
.navbar-brand
airson do chompanaidh, toradh no ainm pròiseict..navbar-nav
airson seòladh làn-àirde agus aotrom (a’ toirt a-steach taic airson dropdowns)..navbar-toggler
airson a chleachdadh leis a’ plugan tuiteam againn agus giùlan toggling seòlaidh eile.- Goireasan sùbailte agus farsaingeachd airson smachdan agus gnìomhan cruth sam bith.
.navbar-text
airson sreathan teacsa le fòcas dìreach a chur ris..collapse.navbar-collapse
airson a bhith a’ cruinneachadh agus a’ falach susbaint navbar le briseadh pàrant.- Cuir roghainn a-steach
.navbar-scroll
gus susbaint bàr seòlaidh leudaichte a shuidheachadhmax-height
agus scrollaich .
Seo eisimpleir de na fo-phàirtean gu lèir a tha air an toirt a-steach do bhàr seòlaidh freagairteach le cuspair solais a thuiteas gu fèin-ghluasadach aig an lg
àite brisidh (mòr).
<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>
Bidh an eisimpleir seo a’ cleachdadh cùl -fhiosrachadh ( bg-light
) agus farsaingeachd ( me-auto
, mb-2
, mb-lg-0
, me-2
) clasaichean goireis.
Brand
Faodar .navbar-brand
an cur an sàs anns a’ mhòr-chuid de na h-eileamaidean, ach tha acair ag obair as fheàrr, oir is dòcha gu feum cuid de na h-eileamaidean clasaichean goireis no stoidhlichean àbhaisteach.
Teacs
Cuir do theacsa taobh a-staigh eileamaid leis a’ .navbar-brand
chlas.
<!-- 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>
Dealbh
Faodaidh tu teacsa a chuir na àite .navbar-brand
le faidhle <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>
Ìomhaigh agus teacsa
Faodaidh tu cuideachd cuid de ghoireasan a bharrachd a chleachdadh gus ìomhaigh agus teacsa a chuir ris aig an aon àm. Thoir fa-near cur-ris .d-inline-block
agus .align-text-top
air an <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
Bidh ceanglaichean seòlaidh Navbar a’ togail air na .nav
roghainnean againn leis a’ chlas atharrachaidh aca fhèin agus feumaidh iad clasaichean toggler a chleachdadh airson stoidhle freagairteach ceart. Bidh seòladh ann am bàraichean seòlaidh cuideachd a’ fàs gus a bhith a’ gabhail thairis na h-uimhir de dh’ àite còmhnard ’s as urrainn gus susbaint do bhàr-seòlaidh a chumail ceangailte gu tèarainte.
Cuir ris a’ .active
chlas .nav-link
gus an duilleag làithreach a chomharrachadh.
Thoir an aire gum bu chòir dhut cuideachd am aria-current
feart a chuir ris an fhaidhle gnìomhach .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>
Agus leis gu bheil sinn a’ cleachdadh chlasaichean airson ar seòladairean, faodaidh tu an dòigh-obrach stèidhichte air liosta a sheachnadh gu tur ma thogras tu.
<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>
Faodaidh tu cuideachd dropdowns a chleachdadh anns a’ bhàr seòlaidh agad. Feumaidh clàir tuiteam-sìos eileamaid còmhdaich airson a shuidheachadh, mar sin bi cinnteach gun cleachd thu eileamaidean fa leth agus neadachaidh airson .nav-item
agus .nav-link
mar a chithear gu h-ìosal.
<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>
Foirmean
Cuir diofar smachdan cruth agus co-phàirtean taobh a-staigh bàr nav:
<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>
Bidh clann sa bhad a’ .navbar
cleachdadh cruth sùbailte agus bidh iad bunaiteach gu justify-content: space-between
. Cleachd goireasan sùbailte a bharrachd mar a dh’ fheumar gus an giùlan seo atharrachadh.
<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>
Bidh buidhnean cuir a-steach ag obair cuideachd. Mas e foirm slàn a th’ anns a’ bhàr seòlaidh agad, no foirm sa mhòr-chuid, faodaidh tu an <form>
eileamaid a chleachdadh mar shoitheach agus sàbhail beagan 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>
Tha grunn phutanan a’ faighinn taic mar phàirt de na foirmean navbar sin cuideachd. Tha seo cuideachd na dheagh chuimhneachan gum faodar goireasan co-thaobhadh dìreach a chleachdadh gus eileamaidean de dhiofar mheudan a cho-thaobhadh.
<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>
Teacs
Faodaidh pìosan teacsa a bhith ann an Navbars le cuideachadh bho .navbar-text
. Bidh an clas seo ag atharrachadh co-thaobhadh dìreach agus farsaingeachd chòmhnard airson sreathan teacsa.
<nav class="navbar bg-light">
<div class="container-fluid">
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</nav>
Measgaich agus maids le co-phàirtean agus goireasan eile mar a dh’ fheumar.
<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>
Sgeama dathan
.navbar-light
chan eil e air a mholadh. Bithear a’ cur caochladairean CSS an sàs ann an
.navbar
, a’ dol a-steach don choltas “aotrom”, agus faodar an toirt thairis le
.navbar-dark
.
Tha cuspairean Navbar nas fhasa na bha e a-riamh le taing dha cothlamadh Bootstrap de chaochladairean Sass agus CSS. Is e am “bàr-seòlaidh aotrom” an roghainn àbhaisteach againn airson a chleachdadh le dathan cùl-raoin aotrom, ach faodaidh tu cuideachd tagradh a dhèanamh .navbar-dark
airson dathan cùl-raoin dorcha. An uairsin, gnàthaich le .bg-*
goireasan.
<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>
Gabhadairean
Ged nach eil feum air, faodaidh tu bàr-seòlaidh a phasgadh ann an .container
duilleag gus a mheadhanachadh - ged a thoir an aire gu bheil feum air soitheach a-staigh fhathast. No faodaidh tu soitheach a chuir a-steach .navbar
don ionad gus susbaint bàr-seòlaidh mullach stèidhichte no statach a-mhàin .
<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>
Cleachd gin de na soithichean freagairteach gus atharrachadh dè cho farsaing ‘s a tha an susbaint anns a’ bhàr seòlaidh agad air a thaisbeanadh.
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-md">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
Suidheachadh
Cleachd na goireasan suidheachaidh againn gus navbars a chuir ann an suidheachaidhean neo-statach. Tagh bho stèidhichte gu mullach, suidhichte aig a ’bhonn, steigte chun mhullach (scrollaich leis an duilleag gus an ruig e am mullach, an uairsin fuirich ann), no steigte ris a’ bhonn (scrollaich leis an duilleag gus an ruig e am bonn, an uairsin fuirich ann).
Bidh navbars stèidhichte a’ cleachdadh position: fixed
, a’ ciallachadh gu bheil iad air an tarraing bho shruth àbhaisteach an DOM agus is dòcha gu feum iad CSS àbhaisteach (me, padding-top
air an <body>
) gus casg a chuir air tar-tharraing le eileamaidean eile.
<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>
Sgrolaich
Cuir .navbar-nav-scroll
ri .navbar-nav
(no fo-phàirt navbar eile) gus scrollaidh dìreach a chomasachadh taobh a-staigh susbaint toggle ann am bàr-seòlaidh a thuit. Gu gnàthach, bidh scrollaidh a’ tòiseachadh aig 75vh
(no 75% de dh’ àirde an t-seallaidh), ach faodaidh tu a dhol thairis air sin le seilbh àbhaisteach CSS ionadail --bs-navbar-height
no stoidhlichean àbhaisteach. Aig puirt-seallaidh nas motha nuair a thèid am bàr seòlaidh a leudachadh, nochdaidh susbaint mar a tha e ann am bàr seòlaidh bunaiteach.
Thoir an aire gu bheil an giùlan seo a’ tighinn le tarraing air ais a dh’ fhaodadh a bhith ann - nuair a tha overflow
suidheachadh overflow-y: auto
(feumar an susbaint a sgrùdadh an seo), overflow-x
co-ionann ri auto
, a bheir bàrr air susbaint chòmhnard.
Seo eisimpleir navbar a’ cleachdadh .navbar-nav-scroll
le style="--bs-scroll-height: 100px;"
, le cuid de ghoireasan iomaill a bharrachd airson am farsaingeachd as fheàrr.
<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>
Giùlan freagairteach
Faodaidh Navbars .navbar-toggler
, .navbar-collapse
, agus .navbar-expand{-sm|-md|-lg|-xl|-xxl}
clasaichean a chleachdadh gus faighinn a-mach cuin a thuiteas an susbaint aca air cùl putan. Còmhla ri goireasan eile, faodaidh tu gu furasta taghadh cuin a sheallas tu no a chuireas tu am falach eileamaidean sònraichte.
Airson navbars nach tuit gu bràth, cuir a’ .navbar-expand
chlas air a’ bhàr nav. Airson navbars a bhios an-còmhnaidh a 'tuiteam, na cuir .navbar-expand
clas sam bith ris.
Togbhail
Tha toglaichean Navbar air an co-thaobhadh ris an taobh chlì a ghnàth, ach ma leanas iad eileamaid sibling mar a .navbar-brand
, bidh iad gu fèin-obrachail air an co-thaobhadh ris an fhìor làimh dheis. Ma chuireas tu do chomharradh air ais cuiridh sin cùl ri suidheachadh an toggler. Gu h-ìosal tha eisimpleirean de dhiofar stoidhlichean toggle.
Gun a bhith .navbar-brand
air a shealltainn aig an ìre briseadh as lugha:
<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>
Le ainm branda air an taobh chlì agus toggler air an làimh dheis:
<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>
Le toggler air an taobh chlì agus ainm branda air an làimh dheis:
<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>
Susbaint bhon taobh a-muigh
Aig amannan bidh thu airson am plugan tuiteam a chleachdadh gus eileamaid soithich a bhrosnachadh airson susbaint a tha gu structarail na shuidhe taobh a-muigh an fhaidhle .navbar
. Leis gu bheil am plugan againn ag obair air id
agus a’ data-bs-target
maidseadh, tha sin furasta a dhèanamh!
<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>
Nuair a nì thu seo, tha sinn a’ moladh JavaScript a bharrachd a thoirt a-steach gus am fòcas a ghluasad gu prògramach chun ghobhar nuair a thèid fhosgladh. Rud eile, tha coltas ann gum bi ùine chruaidh aig luchd-cleachdaidh meur-chlàr agus luchd-cleachdaidh theicneòlasan cuideachaidh an t-susbaint ùr fhoillseachadh - gu sònraichte ma thig an soitheach a chaidh fhosgladh ron toggler ann an structar na sgrìobhainn. Tha sinn cuideachd a’ moladh dèanamh cinnteach gu bheil aria-controls
buadhan aig an toggler, a’ comharrachadh meud an id
t-soithich susbaint. Ann an teòiridh, leigidh seo le luchd-cleachdaidh teicneòlas cuideachaidh leum gu dìreach bhon toggler chun inneal a tha e a’ cumail smachd - ach tha taic airson seo gu math neo-chunbhalach an-dràsta.
Offcanvas
Atharraich am bàr seòlaidh agad a tha a’ leudachadh is a’ tuiteam gu drathair far canabhas leis a’ cho-phàirt offcanvas . Bidh sinn a’ leudachadh an dà chuid na stoidhlichean bunaiteach offcanvas agus a’ cleachdadh ar .navbar-expand-*
clasaichean gus bàr-taoibh seòlaidh fiùghantach is sùbailte a chruthachadh.
Anns an eisimpleir gu h-ìosal, gus bàr-seòlaidh offcanvas a chruthachadh a bhios an-còmhnaidh air tuiteam thairis air a h-uile briseadh, fàg a-mach an .navbar-expand-*
clas gu tur.
<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>
Gus bàr-seòlaidh offcanvas a chruthachadh a leudaicheas gu bàr seòlaidh àbhaisteach aig àite brisidh sònraichte leithid lg
, cleachd .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>
Nuair a bhios tu a’ cleachdadh offcanvas ann am bàr seòlaidh dorcha, thoir an aire gur dòcha gum feum thu cùl-raon dorcha air an t-susbaint far a’ chanabhas gus nach bi an teacsa so-leughaidh. Anns an eisimpleir gu h-ìosal, bidh sinn a 'cur ris .navbar-dark
agus .bg-dark
ris an .navbar
, .text-bg-dark
ris an .offcanvas
, .dropdown-menu-dark
gu .dropdown-menu
, agus .btn-close-white
airson .btn-close
stoidhle ceart le canabhas dorcha.
<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
Caochlaidhean
Air a chur ris ann an v5.2.0Mar phàirt de dhòigh-obrach caochladairean CSS Bootstrap a tha ag atharrachadh, bidh navbars a-nis a’ cleachdadh caochladairean CSS ionadail air adhart .navbar
airson gnàthachadh fìor-ùine leasaichte. Tha luachan airson caochladairean CSS air an suidheachadh tro Sass, agus mar sin tha gnàthachadh Sass fhathast a’ faighinn taic, cuideachd.
--#{$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};
Tha cuid de chaochladairean CSS a bharrachd an làthair cuideachd air .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);
Chithear gnàthachadh tro chaochladairean CSS air a’ .navbar-dark
chlas far am bi sinn a’ dol thairis air luachan sònraichte gun a bhith a’ cur luchd-taghaidh CSS dùblaichte ris.
--#{$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)};
caochladairean Sass
Caochlaidhean airson a h-uile 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($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;
Caochlaidhean airson a’ bhàr seòlaidh dorcha :
$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;
lùb sass
Bithear a’ cur chlasaichean leudachaidh/tuiteam (me, .navbar-expand-lg
) còmhla ris a’ $breakpoints
mhapa agus air an gineadh tro lùb ann an 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;
}
}
}
}
}
}