Aadhi Arun
Aadhi Arun

Reputation: 61

How do i make the navbar shrink when window shrinks?

Im new to web dev and im developing a furniture website. The website navbar works fine in normal view, but when i minimize it, it breaks. How do i make the navbar shrink when the window shrinks?

Navbar in normal view: Working Navbar

Navbar in shrunken view: Broken Navbar

Navbar HTML:

<div class="topnav">
        <ul>
            <li><a href="index.html">Oak <span style="color:green">N' </span>Oak</a></li>
            <li class="current"><a href="index.html">Home</a></li>
            <li><a href="About Us.html">About Us</a></li>
            <li><a href="Products.html">Products</a></li>
            <li><a href="Our Dealers.html">Our Dealers</a></li>
            <li><a href="Apply For Dealership.html">Apply For Dealership</a></li>
            <li><a href="Wholesale Price.html">Wholesale Price</a></li>
            <li><a href="Contact Us.html">Contact Us</a></li>
        </ul>
    </div>

Navbar CSS:

.topnav {
    background-color: rgb(255, 255, 255);
    overflow: hidden;
    display: block;
    place-content: space-between;
  }

Upvotes: 1

Views: 1550

Answers (1)

Vintage Farms
Vintage Farms

Reputation: 64

You should try using bootstrap CSS for your navbar.

You can use npm to install it into your project by running the command, if it's an angular project:

npm i bootstrap

And it will be added to your project.

or

if it is not an angular project rather just an html project, simply add Bootstrap CSS to the tag as a link to your html file. You can achieve that by doing thus:

<head><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous" /></head>

As for you code, you can rewrite it like this:

<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="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link href="#">Link</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

Each time you resize or "shrink" your browser window size, there will be a toggle button (=) at the top right where your navbar items will be hidden. A single click on it will display your navbar item. A double click will hide it again.

I also saw there was a mini link below the navbar (home>>products). You can use breadcrumbs to get that part sorted. Follow this link to learn how to use breadcrumbs in Bootstrap:

Bootstrap breadcrumb: https://getbootstrap.com/docs/5.1/components/breadcrumb/

I hope this helps you.

Upvotes: 1

Related Questions