skip to Main Content

I’m refactoring a website using Bootstrap and I need a navbar with a video in background.

I set the navbar as sticky top to keep it always on top even if I scroll.

I succeeded after several tests by setting the video as position: absolute and moving the video before the navbar.

But there was a problem. When I tried to insert other code after the navbar, the items went under the video.

So I removed the position: absolute from the video and put the navbar before the video.

I can not in any way to make it work. Could someone more experienced with bootstrap help me?

window.onscroll = function() {
  scrollFunction()
};

function scrollFunction() {
  if (document.body.scrollTop > 500 || document.documentElement.scrollTop > 20) {
    document.getElementById("navbar").style.background = "#fff";
    document.getElementById("nav-link1").style.color = 'red';
    document.getElementById("nav-link2").style.color = 'red';
    document.getElementById("nav-link3").style.color = 'red';
    document.getElementById("nav-link4").style.color = 'red';
    document.getElementById("nav-link5").style.color = 'red';
    document.getElementById("nav-link6").style.color = 'red';
    document.getElementById("nav-link-dropdown").style.color = 'red';
  } else {
    document.getElementById("navbar").style.background = "none";
    document.getElementById("nav-link1").style.color = '';
    document.getElementById("nav-link2").style.color = '';
    document.getElementById("nav-link3").style.color = '';
    document.getElementById("nav-link4").style.color = '';
    document.getElementById("nav-link5").style.color = '';
    document.getElementById("nav-link6").style.color = '';
    document.getElementById("nav-link-dropdown").style.color = '';
  }
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<!-- SECTION NAVBAR -->
<nav class="navbar navbar-expand-md sticky-top" style="background: none; transition: 1s;" id="navbar">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">
      <img src="https://via.placeholder.com/80" alt="Top Life" style="width: 40%; height: auto;">
    </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 ms-auto">
        <li class="nav-item">
          <a class="nav-link" id="nav-link1" href="#">Servizi</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" id="nav-link2" href="#">Tecnologia</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" id="nav-link3" href="#">Immonbili</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" id="nav-link4" href="#">News</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" id="nav-link5" href="#">Identità</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" id="nav-link6" href="#">Contatti</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="nav-link-dropdown">
            <ion-icon name="globe-outline"></ion-icon>
          </a>
          
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Italiano</a></li>
            <li><a class="dropdown-item" href="#">English</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>

<!-- SECTION VIDEO -->
<video playsinline="playsinline" autoplay="autoplay" muted="muted" loop="loop">
    <source src="/img/0314_compressed.mp4" type="video/mp4">
</video>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

The result should be something like this:

enter image description here

2

Answers


  1. In the Nav tag use 
    ```style=position:"fixed";
    background:"transparent"; ````
    this will make the navbar to get fixed at the top and the background will be transparent and hence the video will be visible as background.
    
    Login or Signup to reply.
  2. Video Background with Bootstrap Navbar

    This is based on a page I created years ago and I think it will also work for you. If you look at the snippet, there is an video element with an absolute position centered in the page. We then lay the navbar on top of it and let the video loop. You can optionally add a still image to display while the video loads on low bandwidth devices. You should also mute the video to ensure that it automatically plays when the page loads.

    The navbar background was black in the original code by including class bg-dark. That ensured the video didn’t obscure the navigation text. However, in the snippet I’ve removed bg-dark to allow the video to show through as in your example. You’ll need to experiment with the background color, opacity, and video to find which combination works best for your application.

    Run the snippet to try

    header {
      position: relative;
      background-color: black;
      height: 75vh;
      min-height: 25rem;
      width: 100%;
      overflow: hidden;
    }
    
    header .container {
      position: relative;
      z-index: 2;
    }
    
    
    
    header video {
      /* Optional - Use a still frame background while video is loading 
      background-image: url("./video/video-background.jpg");
      */
      
      background-repeat: no-repeat;
      background-size: cover;
      position: absolute;
      top: 50%;
      left: 50%;
      min-width: 100%;
      min-height: 100%;
      width: auto;
      height: auto;
      z-index: 0;
      transform: translateX(-50%) translateY(-50%);
    }
    
    
    /* shrink the video on small devices */
    
    @media (pointer: coarse) and (hover: none) {
      header {
        height: 15vh;
        min-height: 15rem;
      }
    }
    <!-- change class x-bg-dark to bg-dark to use black navbar -->
    
    <nav class="navbar navbar-expand-lg fixed-top  navbar-dark x-bg-dark shadow">
        <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="#">Banijay Italia</a>
    
                    <ul class="navbar-nav ms-auto">
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link1" href="#">Servizi</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link2" href="#">Tecnologia</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link3" href="#">Immonbili</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link4" href="#">News</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link5" href="#">Identità</a>
                      </li>
                      <li class="nav-item">
                        <a class="nav-link" id="nav-link6" href="#">Contatti</a>
                      </li>
                      <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="nav-link-dropdown">
                            <ion-icon name="globe-outline"></ion-icon>
                        </a>
                        <ul class="dropdown-menu">
                          <li><a class="dropdown-item" href="#">Italiano</a></li>
                          <li><a class="dropdown-item" href="#">English</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>
      </nav>
    
    
      <header>
        <video id="video1" playsinline autoplay muted loop>
                <source src="https://cms.twopointo.film/assets/project/eucerin.mp4" type="video/mp4">
            </video>
        <div class="container h-100">
          <div class="d-flex h-100 text-center align-items-center">
            <div class="w-100 text-white">
              <div class="h1 display-3" style="color:lime;">Banijay Italia</div>
              <div class="h1 mb-0">We are Banijay</div>
            </div>
          </div>
        </div>
      </header>
    
    <!-- Bootstrap 5 -->
    
      <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search