Navbar
Dokumentazzjoni u eżempji għall-header tan-navigazzjoni b'saħħtu u reattiv ta' Bootstrap, in-navbar. Jinkludi appoġġ għal branding, navigazzjoni, u aktar, inkluż appoġġ għall-plugin tal-kollass tagħna.
Kif taħdem
Hawn dak li għandek bżonn tkun taf qabel tibda bin-navbar:
- In-Navbars jeħtieġu tgeżwir
.navbar
bi għal klassijiet ta' skema ta' skema ta'.navbar-expand{-sm|-md|-lg|-xl|-xxl}
kollass u ta' kuluri li jirrispondu. - In-Navbars u l-kontenut tagħhom huma fluwidi awtomatikament. Ibdel il- kontenitur biex tillimita l-wisa 'orizzontali tagħhom b'modi differenti.
- Uża l-klassijiet tal-utilità tal- ispazjar u l- flex tagħna biex tikkontrolla l-ispazjar u l-allinjament fi ħdan in-navbars.
- In-Navbars jirrispondu awtomatikament, iżda tista 'faċilment timmodifikahom biex tbiddel dan. L-imġieba reattiva tiddependi fuq il-plugin JavaScript tagħna Kollass.
- Żgura l-aċċessibbiltà billi tuża
<nav>
element jew, jekk tuża element aktar ġeneriku bħal<div>
, żid arole="navigation"
ma' kull navbar biex tidentifikaha b'mod espliċitu bħala reġjun importanti għall-utenti ta' teknoloġiji ta' assistenza. - Indika l-oġġett kurrenti billi tuża
aria-current="page"
għall-paġna kurrenti jewaria-current="true"
għall-oġġett kurrenti f'sett. - Ġdid f'v5.2.0: Navbars jistgħu jkunu temati b'varjabbli CSS li huma skoperti għall-
.navbar
klassi bażi..navbar-light
ġie deprecated u.navbar-dark
ġie miktub mill-ġdid biex jissupera l-varjabbli CSS minflok ma żżid stili addizzjonali.
prefers-reduced-motion
-mistoqsija tal-midja. Ara t-
taqsima tal-mozzjoni mnaqqsa tad-dokumentazzjoni tagħna dwar l-aċċessibbiltà .
Kontenut appoġġjat
Navbars jiġu b'appoġġ integrat għal numru żgħir ta 'sottokomponenti. Agħżel minn dawn li ġejjin kif meħtieġ:
.navbar-brand
għall-kumpanija, prodott, jew isem tal-proġett tiegħek..navbar-nav
għal navigazzjoni ta' għoli sħiħ u ħafifa (inkluż appoġġ għal dropdowns)..navbar-toggler
għall-użu mal-plugin kollass tagħna u imgieba oħra ta 'toggling tan-navigazzjoni .- Flex u utilitajiet ta 'spazjar għal kwalunkwe kontrolli u azzjonijiet tal-forma.
.navbar-text
għaż-żieda ta' kordi ta' test ċċentrata vertikalment..collapse.navbar-collapse
għall-raggruppament u l-ħabi tal-kontenut tan-navbar minn breakpoint ġenitur.- Żid fakultattiv
.navbar-scroll
biex tissettjamax-height
u tiskrollja kontenut ta' navbar estiż .
Hawn eżempju tas-sottokomponenti kollha inklużi f'navbar b'tema ħafifa li tirrispondi li awtomatikament tikkollassa fil- lg
punt ta 'waqfien (kbir).
<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>
Dan l-eżempju juża klassijiet ta' utilità fl- isfond ( bg-light
) u spazjar ( me-auto
, mb-2
, mb-lg-0
, ).me-2
Brand
Jista ' .navbar-brand
jiġi applikat għal ħafna elementi, iżda ankra taħdem l-aħjar, peress li xi elementi jistgħu jeħtieġu klassijiet ta' utilità jew stili tad-dwana.
Test
Żid it-test tiegħek f'element mal- .navbar-brand
klassi.
<!-- 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>
Immaġni
Tista' tissostitwixxi t-test fi ħdan il- .navbar-brand
b' <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>
Immaġini u test
Tista 'wkoll tagħmel użu minn xi utilitajiet addizzjonali biex iżżid immaġni u test fl-istess ħin. Innota ż-żieda ta' .d-inline-block
u .align-text-top
fuq il- <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
Ir-rabtiet tan-navigazzjoni tan-Navbar jibnu fuq l- .nav
għażliet tagħna bil-klassi modifikatur tagħhom stess u jeħtieġu l-użu ta ' klassijiet toggler għal stil reattiv xieraq. In-navigazzjoni fin-navbars se tikber ukoll biex tokkupa spazju orizzontali kemm jista' jkun biex iżżomm il-kontenut tan-navbar tiegħek allinjat b'mod sikur.
Żid il- .active
klassi fuq .nav-link
biex tindika l-paġna attwali.
Jekk jogħġbok innota li għandek iżżid ukoll l- aria-current
attribut fuq l-attiv .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>
U minħabba li nużaw klassijiet għan-navs tagħna, tista' tevita kompletament l-approċċ ibbażat fuq il-lista jekk tixtieq.
<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>
Tista 'wkoll tuża dropdowns fin-navbar tiegħek. Il-menus dropdown jeħtieġu element tat-tgeżwir għall-ippożizzjonar, għalhekk kun żgur li tuża elementi separati u nested għal .nav-item
u .nav-link
kif muri hawn taħt.
<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>
Formoli
Poġġi diversi kontrolli tal-forma u komponenti fi ħdan 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>
Elementi tfal immedjati ta ' .navbar
użu flex layout u default għal justify-content: space-between
. Uża utilitajiet flex addizzjonali kif meħtieġ biex taġġusta din l-imġieba.
<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>
Il-gruppi tal-input jaħdmu wkoll. Jekk in-navbar tiegħek hija formola sħiħa, jew l-aktar forma, tista 'tuża l- <form>
element bħala l-kontenitur u tiffranka xi 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>
Diversi buttuni huma appoġġjati wkoll bħala parti minn dawn il-formoli tan-navbar. Din hija wkoll tfakkira kbira li l-utilitajiet ta 'allinjament vertikali jistgħu jintużaw biex jallinjaw elementi ta' daqs differenti.
<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>
Test
Navbars jista 'jkun fihom bits ta' test bl-għajnuna ta ' .navbar-text
. Din il-klassi taġġusta l-allinjament vertikali u l-ispazjar orizzontali għall-kordi tat-test.
<nav class="navbar bg-light">
<div class="container-fluid">
<span class="navbar-text">
Navbar text with an inline element
</span>
</div>
</nav>
Ħallat u qabbel ma 'komponenti u utilitajiet oħra kif meħtieġ.
<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>
Skemi tal-kulur
.navbar-light
ġiet deprecata. Il-varjabbli tas-CSS huma applikati għal
.navbar
, b'kontumaċja għad-dehra "ħafifa", u jistgħu jiġu sostitwiti b'
.navbar-dark
.
Temi Navbar huma aktar faċli minn qatt qabel grazzi għall-kombinazzjoni ta 'Bootstrap ta' varjabbli Sass u CSS. Id-default huwa "light navbar" tagħna għall-użu ma 'kuluri ta' sfond ċar, iżda tista 'wkoll tapplika .navbar-dark
għal kuluri ta' sfond skur. Imbagħad, ippersonalizza bl- .bg-*
utilitajiet.
<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>
Kontenituri
Għalkemm mhux meħtieġ, tista' tgeżwer navbar f'biċ .container
biex tiċċentraha fuq paġna, għalkemm innota li kontenitur ta' ġewwa għadu meħtieġ. Jew tista 'żżid kontenitur ġewwa l- biċ .navbar
-ċentru biss tal-kontenut ta' navbar ta 'fuq fiss jew statiku .
<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>
Uża xi wieħed mill-kontenituri li jirrispondu biex tibdel kemm hu ppreżentat il-kontenut fin-navbar tiegħek.
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-md">
<a class="navbar-brand" href="#">Navbar</a>
</div>
</nav>
Pjazzament
Uża l- utilitajiet tal-pożizzjoni tagħna biex tpoġġi navbars f'pożizzjonijiet mhux statiċi. Agħżel minn imwaħħal mal-quċċata, imwaħħal mal-qiegħ, imwaħħal mal-wiċċ (jiskrollja mal-paġna sakemm tasal fil-quċċata, imbagħad tibqa' hemm), jew imwaħħal mal-qiegħ (jiskrollja mal-paġna sakemm tasal fil-qiegħ, imbagħad tibqa') hemm).
Navbars fissi jużaw position: fixed
, li jfisser li qed jinġibdu mill-fluss normali tad-DOM u jistgħu jeħtieġu CSS personalizzati (eż., padding-top
fuq il- <body>
) biex jipprevjenu koinċidenza ma 'elementi oħra.
<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>
Skrolljar
Żid .navbar-nav-scroll
ma' .navbar-nav
(jew sub-komponent ieħor tan-navbar) biex tippermetti scrolling vertikali fi ħdan il-kontenuti li jistgħu jiġu togglebbli ta' navbar imġarraf. B'mod awtomatiku, is-scrolling jibda fi 75vh
(jew 75% tal-għoli tal-vetrina), iżda tista' tegħleb dak bil-proprjetà lokali tad-dwana tas-CSS --bs-navbar-height
jew stili tad-dwana. F'viewports akbar meta n-navbar tiġi estiża, il-kontenut jidher kif jidher f'navbar default.
Jekk jogħġbok innota li din l-imġieba tiġi ma 'żvantaġġ potenzjali ta' overflow
—meta l-issettjar overflow-y: auto
(meħtieġ biex tiskrollja l-kontenut hawn), overflow-x
hija l-ekwivalenti ta ' auto
, li se taqsam xi kontenut orizzontali.
Hawn eżempju ta 'navbar li juża .navbar-nav-scroll
ma' style="--bs-scroll-height: 100px;"
, b'xi utilitajiet ta 'marġni żejda għall-ispazjar ottimali.
<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>
Imgieba reattivi
In-Navbars jistgħu jużaw .navbar-toggler
, .navbar-collapse
, u .navbar-expand{-sm|-md|-lg|-xl|-xxl}
klassijiet biex jiddeterminaw meta l-kontenut tagħhom jiġġarraf wara buttuna. Flimkien ma 'utilitajiet oħra, tista' faċilment tagħżel meta turi jew taħbi elementi partikolari.
Għal navbars li qatt ma jikkollassaw, żid il- .navbar-expand
klassi fuq in-navbar. Għal navbars li dejjem jikkrollaw, iżżid l-ebda .navbar-expand
klassi.
Toggler
It-togglers tan-Navbar huma allinjati max-xellug b'mod awtomatiku, iżda jekk isegwu element aħwa bħal .navbar-brand
, huma awtomatikament jiġu allinjati mal-lemin estrem. It-treġġigħ lura tal-markup tiegħek ser ireġġa' lura t-tqegħid tal-toggler. Hawn taħt hemm eżempji ta 'stili ta' toggle differenti.
Bla ebda .navbar-brand
muri fl-iżgħar punt ta' waqfien:
<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>
B'isem tad-ditta muri fuq ix-xellug u toggler fuq il-lemin:
<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>
Bi toggler fuq ix-xellug u isem tad-ditta fuq il-lemin:
<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>
Kontenut estern
Xi drabi trid tuża l-plugin tal-kollass biex tiskatta element tal-kontenitur għall-kontenut li strutturalment joqgħod barra mill- .navbar
. Minħabba li l-plugin tagħna jaħdem fuq id
u t- data-bs-target
tqabbil, dan isir faċilment!
<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>
Meta tagħmel dan, nirrakkomandaw li tinkludi JavaScript addizzjonali biex tmexxi l-fokus b'mod programmatiku lejn il-kontenitur meta jinfetaħ. Inkella, l-utenti tat-tastiera u l-utenti ta' teknoloġiji ta' assistenza x'aktarx ikollhom diffiċli biex isibu l-kontenut li għadu kif ġie żvelat - partikolarment jekk il-kontenitur li nfetaħ jiġi qabel it-toggler fl-istruttura tad-dokument. Nirrakkomandaw ukoll li niżguraw li t-toggler għandu l- aria-controls
attribut, li jindika l- id
tal-kontenitur tal-kontenut. Fit-teorija, dan jippermetti lill-utenti tat-teknoloġija assistiva li jaqbżu direttament mill-toggler għall-kontenitur li jikkontrolla – iżda l-appoġġ għal dan bħalissa huwa pjuttost irregolari.
Offcanvas
Ittrasforma n-navbar tiegħek li qed tespandi u li qed tiġġarraf f'kexxun offcanvas bil- komponent offcanvas . Aħna jestendu kemm l-istili default offcanvas u nużaw il- .navbar-expand-*
klassijiet tagħna biex noħolqu sidebar tan-navigazzjoni dinamika u flessibbli.
Fl-eżempju hawn taħt, biex tinħoloq navbar offcanvas li hija dejjem ikkollassata fil-punti ta' waqfien kollha, ħalli barra l- .navbar-expand-*
klassi għal kollox.
<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>
Biex toħloq navbar offcanvas li tespandi f'navbar normali f'punt ta' waqfien speċifiku bħal lg
, uża .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>
Meta tuża offcanvas f'navbar skur, kun konxju li jista' jkollok bżonn li jkollok sfond skur fuq il-kontenut offcanvas biex tevita li t-test isir illeġibbli. Fl-eżempju hawn taħt, inżidu .navbar-dark
u .bg-dark
mal- .navbar
, .text-bg-dark
mal- .offcanvas
, .dropdown-menu-dark
mal- .dropdown-menu
, u .btn-close-white
għall .btn-close
-grafika xierqa b'kanvas skur.
<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
Varjabbli
Miżjud fi v5.2.0Bħala parti mill-approċċ tal-varjabbli tas-CSS li qed jevolvu ta' Bootstrap, in-navbars issa jużaw varjabbli tas-CSS lokali .navbar
għall-adattament imtejjeb f'ħin reali. Il-valuri għall-varjabbli CSS huma stabbiliti permezz ta 'Sass, għalhekk il-personalizzazzjoni ta' Sass għadha appoġġjata wkoll.
--#{$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};
Xi varjabbli CSS addizzjonali huma preżenti wkoll fuq .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);
L-adattament permezz ta' varjabbli CSS jista' jidher fuq il- .navbar-dark
klassi fejn nissuperaw valuri speċifiċi mingħajr ma nżidu seletturi CSS duplikati.
--#{$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 varjabbli
Varjabbli għall-navbars kollha:
$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;
Varjabbli għan- navbar skur :
$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
Il-klassijiet ta' navbar li jirrispondu jespandu/kollass (eż., .navbar-expand-lg
) huma kkombinati mal- $breakpoints
mappa u ġġenerati permezz ta' loop f' 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;
}
}
}
}
}
}