Hla mus rau cov ntsiab lus tseem ceeb Hla mus rau docs navigation
Check
in English

Navbar

Cov ntaub ntawv thiab cov piv txwv rau Bootstrap lub zog, teb cov lus taw qhia header, navbar. Xws li kev txhawb nqa rau kev tsim khoom lag luam, kev taw qhia, thiab lwm yam, suav nrog kev txhawb nqa rau peb qhov kev sib tsoo plugin.

Nws ua haujlwm li cas

Nov yog yam koj yuav tsum paub ua ntej pib nrog navbar:

  • Navbars xav tau kev qhwv .navbarnrog .navbar-expand{-sm|-md|-lg|-xl|-xxl}rau kev sib tsoo thiab cov chav kawm xim xim .
  • Navbars thiab lawv cov ntsiab lus yog kua los ntawm lub neej ntawd. Hloov lub thawv kom txwv lawv txoj kab rov tav dav hauv ntau txoj kev.
  • Siv peb qhov sib nrug thiab cov chav kawm siv hluav taws xob yooj yim rau kev tswj qhov sib nrug thiab sib dhos hauv navbars .
  • Navbars teb los ntawm lub neej ntawd, tab sis koj tuaj yeem hloov kho tau yooj yim los hloov qhov ntawd. Tus cwj pwm teb nyob ntawm peb Collapse JavaScript plugin.
  • Xyuas kom nkag mus tau los ntawm kev siv lub <nav>ntsiab lus lossis, yog tias siv cov khoom siv ntau dua xws li a <div>, ntxiv role="navigation"rau txhua tus navbar kom qhia meej meej tias nws yog thaj chaw thaj chaw rau cov neeg siv cov cuab yeej pabcuam.
  • Qhia cov khoom tam sim no los ntawm kev siv aria-current="page"rau nplooj ntawv tam sim no lossis aria-current="true"rau cov khoom tam sim no hauv ib txheej.
  • Tshiab hauv v5.2.0: Navbars tuaj yeem raug them nrog CSS qhov sib txawv uas tau muab faib rau .navbarhauv chav kawm. .navbar-lighttau raug deprecated thiab .navbar-darktau sau dua tshiab rau override CSS variables es tsis txhob ntxiv cov style ntxiv.
Cov nyhuv animation ntawm cov khoom siv no yog nyob ntawm cov prefers-reduced-motionlus nug xov xwm. Saib qhov txo qis ntawm peb cov ntaub ntawv nkag mus tau .

Txhawb cov ntsiab lus

Navbars tuaj nrog kev txhawb nqa rau ib qho me me ntawm cov khoom sib txuas. Xaiv los ntawm cov hauv qab no raws li xav tau:

  • .navbar-brandrau koj lub tuam txhab, khoom, lossis lub npe qhov project.
  • .navbar-navrau qhov siab tag nrho thiab lub teeb yuag navigation (nrog rau kev txhawb nqa rau kev poob qis).
  • .navbar-togglerrau siv nrog peb lub cev qhuav dej plugin thiab lwm yam navigation toggling cwj pwm.
  • Flex thiab qhov chaw siv hluav taws xob rau txhua daim ntawv tswj thiab ua.
  • .navbar-textrau ntxiv vertically centered hlua ntawm cov ntawv nyeem.
  • .collapse.navbar-collapserau pab pawg thiab zais cov ntsiab lus navbar los ntawm niam txiv breakpoint.
  • Ntxiv ib qho kev xaiv .navbar-scrolllos teeb tsa max-heightthiab scroll nthuav dav navbar cov ntsiab lus .

Ntawm no yog ib qho piv txwv ntawm tag nrho cov sub-components muaj nyob rau hauv lub teb lub teeb-themed navbar uas cia li ntog ntawm lub lg(loj) breakpoint.

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

Qhov piv txwv no siv keeb kwm yav dhau ( bg-light) thiab qhov sib nrug ( me-auto, mb-2, mb-lg-0, me-2) cov chav kawm siv hluav taws xob.

Hom

Cov .navbar-brandtuaj yeem siv rau feem ntau cov ntsiab lus, tab sis lub thauj tog rau nkoj ua haujlwm zoo tshaj plaws, vim qee lub ntsiab lus yuav xav tau cov chav kawm siv hluav taws xob lossis cov qauv kev cai.

Ntawv nyeem

Ntxiv koj cov ntawv hauv ib lub ntsiab lus nrog rau .navbar-brandchav kawm.

html
<!-- 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>

Duab

Koj tuaj yeem hloov cov ntawv hauv qhov .navbar-brandnrog ib qho <img>.

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

Duab thiab ntawv

Koj tuaj yeem siv qee cov khoom siv ntxiv los ntxiv cov duab thiab cov ntawv nyob rau tib lub sijhawm. Nco ntsoov qhov sib ntxiv ntawm .d-inline-blockthiab .align-text-topntawm <img>.

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

Navbar navigation txuas tsim los ntawm peb cov .navkev xaiv nrog lawv tus kheej hloov kho chav kawm thiab xav tau kev siv cov chav kawm toggler rau kev ua haujlwm zoo. Kev taw qhia hauv navbars kuj tseem yuav loj hlob kom nyob hauv qhov chaw kab rov tav ntau li ntau tau kom koj cov ntsiab lus navbar ruaj ntseg zoo sib xws.

Ntxiv cov .activechav kawm .nav-linklos qhia rau nplooj ntawv tam sim no.

Thov nco ntsoov tias koj yuav tsum tau ntxiv tus aria-currentcwj pwm ntawm lub active .nav-link.

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

Thiab vim tias peb siv cov chav kawm rau peb cov navs, koj tuaj yeem zam cov npe raws li txoj hauv kev yog tias koj nyiam.

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

Koj tseem tuaj yeem siv dropdowns hauv koj lub navbar. Dropdown menus xav tau lub ntsiab lus qhwv rau qhov chaw, yog li nco ntsoov siv cov khoom sib cais thiab zes rau .nav-itemthiab .nav-linkraws li qhia hauv qab no.

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

Cov ntawv

Muab ntau daim ntawv tswj thiab cov khoom nyob rau hauv ib tug navbar:

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

Cov me nyuam tam sim ntawd cov ntsiab lus ntawm .navbarkev siv flex layout thiab yuav default rau justify-content: space-between. Siv cov khoom siv flex ntxiv raws li xav tau los kho tus cwj pwm no.

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

Cov tswv yim pab pawg ua haujlwm, ib yam nkaus. Yog tias koj tus navbar yog ib daim ntawv tag nrho, lossis feem ntau yog ib daim ntawv, koj tuaj yeem siv lub <form>caij ua lub thawv thiab txuag tau qee qhov HTML.

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>

Ntau cov nyees khawm raug txhawb los ua ib feem ntawm cov ntawv navbar no, ib yam nkaus. Qhov no kuj yog ib qho kev ceeb toom zoo tias cov khoom siv hluav taws xob ntsug tuaj yeem siv los ua kom cov ntsiab lus sib txawv.

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

Ntawv nyeem

Navbars yuav muaj cov ntawv me me nrog kev pab ntawm .navbar-text. Cov chav kawm no kho cov kab ntsug ntsug thiab kab rov tav qhov sib txawv rau cov hlua ntawm cov ntawv nyeem.

html
<nav class="navbar bg-light">
  <div class="container-fluid">
    <span class="navbar-text">
      Navbar text with an inline element
    </span>
  </div>
</nav>

Sib tov thiab sib xyaw nrog lwm cov khoom siv thiab cov khoom siv raws li xav tau.

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

Xim schemes

Tshiab hauv v5.2.0: Navbar theming yog tam sim no powered los ntawm CSS variables thiab .navbar-lighttau deprecated. CSS variables yog siv rau .navbar, defaulting rau "lub teeb" tsos, thiab yuav overridden nrog .navbar-dark.

Navbar cov ntsiab lus yooj yim dua puas tau ua tsaug rau Bootstrap kev sib xyaw ntawm Sass thiab CSS sib txawv. Lub neej ntawd yog peb "lub teeb navbar" siv nrog lub teeb tom qab xim, tab sis koj tuaj yeem thov .navbar-darkrau xim tom qab tsaus. Tom qab ntawd, kho nrog .bg-*cov khoom siv hluav taws xob.

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

Ntim

Txawm hais tias nws tsis tas yuav tsum tau, koj tuaj yeem qhwv lub navbar rau hauv .containernruab nrab ntawm nplooj ntawv-tab sis nco ntsoov tias tseem yuav tsum muaj lub thawv sab hauv. Lossis koj tuaj yeem ntxiv ib lub thawv rau hauv qhov .navbartsuas yog nruab nrab ntawm cov ntsiab lus ntawm qhov ruaj khov lossis zoo li qub navbar .

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

Siv ib qho ntawm cov thawv teb los hloov qhov dav npaum li cas cov ntsiab lus hauv koj tus navbar tau nthuav tawm.

html
<nav class="navbar navbar-expand-lg bg-light">
  <div class="container-md">
    <a class="navbar-brand" href="#">Navbar</a>
  </div>
</nav>

Qhov chaw

Siv peb txoj haujlwm kev siv hluav taws xob los tso navbars hauv txoj haujlwm tsis zoo li qub. Xaiv los ntawm tsau mus rau sab saum toj, tsau rau hauv qab, nplaum rau sab saum toj (scrolls nrog nplooj ntawv kom txog rau thaum nws mus txog rau sab saum toj, ces nyob twj ywm muaj), los yog nplaum rau hauv qab (scrolls nrog nplooj ntawv kom txog rau thaum nws mus txog rau hauv qab, ces nyob twj ywm. nyob ntawd).

Tsau navbars siv position: fixed, txhais tau tias lawv tau rub los ntawm qhov qub ntws ntawm DOM thiab tej zaum yuav xav tau kev cai CSS (xws li, padding-topntawm lub <body>) txhawm rau tiv thaiv kev sib tshooj nrog lwm cov ntsiab lus.

html
<nav class="navbar bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Default</a>
  </div>
</nav>
html
<nav class="navbar fixed-top bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Fixed top</a>
  </div>
</nav>
html
<nav class="navbar fixed-bottom bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Fixed bottom</a>
  </div>
</nav>
html
<nav class="navbar sticky-top bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Sticky top</a>
  </div>
</nav>
html
<nav class="navbar sticky-bottom bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Sticky bottom</a>
  </div>
</nav>

Scrolling

Ntxiv .navbar-nav-scrollrau ib qho .navbar-nav(los yog lwm yam navbar sub-component) los pab kom ntsug scrolling nyob rau hauv toggleable txheem ntawm ib tug vaub kib navbar. Los ntawm lub neej ntawd, scrolling kicks nyob rau hauv 75vh(lossis 75% ntawm qhov siab qhov siab), tab sis koj tuaj yeem hla dhau qhov ntawd nrog cov khoom siv hauv zos CSS --bs-navbar-heightlossis cov qauv kev cai. Ntawm cov chaw pom loj dua thaum lub navbar nthuav dav, cov ntsiab lus yuav tshwm sim raws li nws ua hauv lub neej ntawd navbar.

Thov nco ntsoov tias tus cwj pwm no los nrog qhov tsis zoo ntawm overflow-thaum teeb tsa overflow-y: auto(yuav tsum tau rub cov ntsiab lus ntawm no), overflow-xyog qhov sib npaug ntawm auto, uas yuav qoob loo qee cov ntsiab lus kab rov tav.

Nov yog ib qho piv txwv navbar siv .navbar-nav-scrollnrog style="--bs-scroll-height: 100px;", nrog rau qee qhov kev siv nyiaj ntxiv rau qhov zoo tshaj qhov sib nrug.

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

Teb tus cwj pwm

Navbars tuaj yeem siv .navbar-toggler, .navbar-collapse, thiab .navbar-expand{-sm|-md|-lg|-xl|-xxl}cov chav kawm los txiav txim siab thaum lawv cov ntsiab lus tawg tom qab lub pob. Ua ke nrog lwm cov khoom siv hluav taws xob, koj tuaj yeem yooj yim xaiv thaum twg los qhia lossis zais cov ntsiab lus tshwj xeeb.

Rau navbars uas yeej tsis tawg, ntxiv cov .navbar-expandchav kawm ntawm tus navbar. Rau navbars uas ib txwm vau, tsis txhob ntxiv ib .navbar-expandchav kawm.

Toggler

Navbar togglers yog sab laug-tsim los ntawm lub neej ntawd, tab sis yog tias lawv ua raws li cov kwv tij txheeb ze .navbar-brand, lawv yuav cia li ua raws li txoj cai nyob deb. Reversing koj markup yuav thim rov qab qhov chaw ntawm lub toggler. Hauv qab no yog cov piv txwv ntawm txawv toggle styles.

Nrog tsis .navbar-brandpom ntawm qhov tsawg tshaj plaws breakpoint:

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

Nrog rau lub npe hom pom nyob rau sab laug thiab toggler ntawm sab xis:

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

Nrog lub toggler ntawm sab laug thiab lub npe hom ntawm sab xis:

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

Cov ntsiab lus sab nraud

Qee lub sij hawm koj xav siv lub cev qhuav dej los ua kom lub thawv ntim rau cov ntsiab lus uas nyob sab nraud ntawm .navbar. Vim tias peb lub plugin ua haujlwm ntawm qhov idsib data-bs-targettxuam, qhov ntawd yooj yim ua tiav!

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

Thaum koj ua qhov no, peb pom zoo kom suav nrog ntxiv JavaScript kom txav qhov tsom mus rau lub thawv thaum nws qhib. Txwv tsis pub, cov neeg siv cov keyboard thiab cov neeg siv cov thev naus laus zis pab yuav muaj lub sijhawm nyuaj nrhiav cov ntsiab lus uas tau qhia tshiab - tshwj xeeb yog tias lub thawv uas tau qhib los ua ntej toggler hauv cov ntaub ntawv cov qauv. Peb kuj pom zoo kom ua kom lub toggler muaj tus aria-controlscwj pwm, taw tes rau idntawm cov ntsiab lus ntim. Hauv txoj kev xav, qhov no tso cai rau cov neeg siv thev naus laus zis pab kom dhia ncaj qha los ntawm lub toggler mus rau lub thawv uas nws tswj tau - tab sis kev txhawb nqa rau qhov no yog tam sim no patchy heev.

Offcanvas

Hloov koj qhov nthuav dav thiab tsoo navbar rau hauv lub tub rau khoom offcanvas nrog cov khoom siv offcanvas . Peb txuas ntxiv ob qho tib si offcanvas default styles thiab siv peb .navbar-expand-*cov chav kawm los tsim kom muaj lub zog thiab hloov tau yooj yim navigation sidebar.

Hauv qhov piv txwv hauv qab no, txhawm rau tsim ib qho offcanvas navbar uas ib txwm tsoo thoob plaws txhua qhov kev tawg, tshem tawm cov .navbar-expand-*chav kawm tag nrho.

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

Txhawm rau tsim ib qho offcanvas navbar uas nthuav mus rau hauv ib txwm navbar ntawm qhov chaw tshwj xeeb xws li lg, siv .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>

Thaum siv offcanvas hauv qhov tsaus nti navbar, nco ntsoov tias koj yuav tsum muaj keeb kwm tsaus nti ntawm cov ntsiab lus offcanvas kom tsis txhob cov ntawv nyeem tsis raug. Hauv qhov piv txwv hauv qab no, peb ntxiv .navbar-darkthiab .bg-darkmus rau .navbar, .text-bg-darkmus rau .offcanvas, .dropdown-menu-darkmus rau .dropdown-menu, thiab .btn-close-whiterau .btn-closekev tsim kom zoo nrog lub qhov tsaus nti offcanvas.

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

Hloov pauv

Ntxiv hauv v5.2.0

Raws li ib feem ntawm Bootstrap qhov hloov pauv CSS hloov pauv mus kom ze, navbars tam sim no siv cov CSS hauv zos hloov pauv rau .navbarkev txhim kho lub sijhawm hloov kho. Cov txiaj ntsig rau CSS qhov sib txawv tau teeb tsa ntawm Sass, yog li Sass customization tseem txhawb nqa, ib yam nkaus.

  --#{$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};
  

Qee qhov ntxiv CSS kuj muaj nyob rau ntawm .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);
  

Customization los ntawm CSS variables tuaj yeem pom nyob rau hauv .navbar-darkchav kawm uas peb override tshwj xeeb qhov tseem ceeb yam tsis tau ntxiv CSS selectors duplicate.

  --#{$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 variables

Hloov pauv rau txhua tus 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;

Variables rau qhov tsaus nti navbar :

$navbar-dark-color:                 rgba($white, .55);
$navbar-dark-hover-color:           rgba($white, .75);
$navbar-dark-active-color:          $white;
$navbar-dark-disabled-color:        rgba($white, .25);
$navbar-dark-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 lub voj

Responsive navbar expand/collapse classes (piv txwv li, .navbar-expand-lg) ua ke nrog $breakpointsdaim ntawv qhia thiab generated los ntawm ib lub voj nyob rau hauv 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;
          }
        }
      }
    }
  }
}