Hla mus rau cov ntsiab lus tseem ceeb Hla mus rau docs navigation
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.
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.

<nav class="navbar navbar-expand-lg navbar-light 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="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <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">
        <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 ( my-2, my-lg-0, me-sm-0, my-sm-0) 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.

<!-- As a link -->
<nav class="navbar navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
  </div>
</nav>

<!-- As a heading -->
<nav class="navbar navbar-light 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>.

<nav class="navbar navbar-light bg-light">
  <div class="container">
    <a class="navbar-brand" href="#">
      <img src="/docs/5.1/assets/brand/bootstrap-logo.svg" alt="" 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>.

<nav class="navbar navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">
      <img src="/docs/5.1/assets/brand/bootstrap-logo.svg" alt="" 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.

<nav class="navbar navbar-expand-lg navbar-light 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.

<nav class="navbar navbar-expand-lg navbar-light 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.

<nav class="navbar navbar-expand-lg navbar-light 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="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown link
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <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:

<nav class="navbar navbar-light bg-light">
  <div class="container-fluid">
    <form class="d-flex">
      <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.

<nav class="navbar navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand">Navbar</a>
    <form class="d-flex">
      <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.

<nav class="navbar navbar-light 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.

<nav class="navbar navbar-light 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.

<nav class="navbar navbar-light 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.

<nav class="navbar navbar-expand-lg navbar-light 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

Theming lub navbar yeej tsis tau yooj yim dua ua tsaug rau kev sib xyaw ntawm cov chav kawm thiab background-colorcov khoom siv hluav taws xob. Xaiv los ntawm .navbar-lightkev siv nrog lub teeb tom qab xim, lossis .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 navbar-light" 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 .

<div class="container">
  <nav class="navbar navbar-expand-lg navbar-light 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.

<nav class="navbar navbar-expand-lg navbar-light 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, lossis nplaum rau sab saum toj (scrolls nrog nplooj ntawv kom txog rau thaum nws mus txog rau sab saum toj, ces 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.

Tsis tas li ntawd nco ntsoov tias .sticky-topsiv position: sticky, uas tsis txaus siab rau txhua qhov browser .

<nav class="navbar navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Default</a>
  </div>
</nav>
<nav class="navbar fixed-top navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Fixed top</a>
  </div>
</nav>
<nav class="navbar fixed-bottom navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Fixed bottom</a>
  </div>
</nav>
<nav class="navbar sticky-top navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Sticky top</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.

<nav class="navbar navbar-expand-lg navbar-light 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="#" id="navbarScrollingDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Link
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarScrollingDropdown">
            <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">
        <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:

<nav class="navbar navbar-expand-lg navbar-light 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">
        <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:

<nav class="navbar navbar-expand-lg navbar-light 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">
        <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:

<nav class="navbar navbar-expand-lg navbar-light 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">
        <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!

<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 lub offcanvas plugin. 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.

<nav class="navbar navbar-light 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 text-reset" 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="#" id="offcanvasNavbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Dropdown
            </a>
            <ul class="dropdown-menu" aria-labelledby="offcanvasNavbarDropdown">
              <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">
          <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-light 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>

Sass

Hloov pauv

$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-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-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;
$navbar-dark-brand-color:                 $navbar-dark-active-color;
$navbar-dark-brand-hover-color:           $navbar-dark-active-color;

Loop

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: $navbar-nav-link-padding-x;
            padding-left: $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-header {
          display: none;
        }

        .offcanvas {
          position: inherit;
          bottom: 0;
          z-index: 1000;
          flex-grow: 1;
          visibility: visible !important; // stylelint-disable-line declaration-no-important
          background-color: transparent;
          border-right: 0;
          border-left: 0;
          @include transition(none);
          transform: none;
        }
        .offcanvas-top,
        .offcanvas-bottom {
          height: auto;
          border-top: 0;
          border-bottom: 0;
        }

        .offcanvas-body {
          display: flex;
          flex-grow: 0;
          padding: 0;
          overflow-y: visible;
        }
      }
    }
  }
}