<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap demo</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>

<body>

  <nav class="navbar navbar-expand-lg bg-body-tertiary">
    <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="#test">Test 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="#test">Go to in page test link</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" aria-disabled="true">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>

  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>

  <p id="test">Destination</p>

  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>
  <p>text to scroll</p>

  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
  <script>
    document
      .querySelectorAll(".navbar-nav li a:not(.dropdown-toggle)")
      .forEach((link) => {
        link.addEventListener("click", () => {
          const navbarResponsive = document.getElementById(
            "navbarSupportedContent"
          );
          if (navbarResponsive.classList.contains("show")) {
            const navbarToggler = document.querySelector(".navbar-toggler");
            setTimeout(() => {
              navbarToggler.click();
            }, "1000");
          }
        });
      });
  </script>

</body>

</html>
:target {
  background: red;
}
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.