Navbar Transparent to Solid on Scroll using CSS

It looks really cool to trigger an animation on scroll event, especially for the navbar. As earlier we created shrinking navbar animation on scroll event, today we are back with a similar tutorial for the navbar animation. In this tutorial, we’ll create a transparent navbar and make it solid on the scroll event.

If you have already a navbar and wanted to change its color after some scrolling then you just need to add a little JavaScript function to your project. If so, add an id "navbar" to your existing navbar and copy the JavaScript function given at the end of this tutorial.

On the other hand, if you don’t have a navbar then you can check the demo of this project and consider it to use this navbar in your project. Basically, it is a defult Bootstrap navbar with some custom CSS. If you are already familiar with Bootstrap then you will enjoy playing with the navbar coding. So, let’s get started on how to create a transparent navbar.

How to Create Transparent Navbar

Generally, the default Bootstrap navbar is transparent if there is no background color class defined in the navbar element. Therefore, we only need to create navbar markup without adding bg-color class. So, in the very first step, load the Bootstrap framework and jQuery into the head tag of your HTML page.

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

After loading the Bootstrap and jQuery now we are ready to build the navbar. So, create a bootstrap default navbar component with an id "navbar" and place your navigation links inside it.

<div class="navbar navbar-fixed-top container-fluid" id="navbar">
  	 <div class="navbar-header">
      <button type="button" class="navbar-toggle navbar-default" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand">Brand</a>
    </div>
    <div class="collapse navbar-right navbar-collapse" id="myNavbar">
    	<ul class="nav navbar-nav">
    		<li>Home</li>
    		<li>About</li>
    		<li>Contact</li>
    		<li>Login</li>
    		<li>Signup</li>
    	</ul>
    </div>
  </div>

Of course, you need to have content on the page along with the navbar. Therefore, create a div element with a class name "para" just after the above navbar HTML code.

<div class="para">
  <h3>Your page content goes here...</h3>
</div>

Styling Navbar with CSS

As we have created Bootstrap default navbar, it is already styled in Bootstrap CSS. Anyhow, we’ll define some CSS styles in order to customize it.

Maybe you don’t need to set the background image for the body, but if you want to make the whole page just like the demo then define the following CSS styles for the body.

body{
   background: url(https://images.pexels.com/photos/414102/pexels-photo-414102.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940);
   background-size: 100%;
   background-repeat: no-repeat;
   background-attachment: fixed;
}

In order to make the fixed navbar use the CSS position property and set its fixed value. Similarly, define the transition for the smoothly change the background color.

.navbar{
  position: fixed;
  transition: 1s;
}

After that, target the "navbar-brand" and define its color, font size, and font-family property. If you have placed the image logo inside this div container then you can skip these styles.

  .navbar-brand{
  		color: #fff;
  		font-size: 45px;
  		font-family: Germania;
  		cursor: pointer;
  	}
  	.icon-bar{
  background-color: #000;

}

The "nav" class is the wrapper element for the navigation links. Define the font family, color and padding property as mentioned below:

.nav{
   font-family: Lithos Pro;
   color: #fff;
   padding: 10px 20px;
}

Similarly, target the "li" element of nav class and define padding, cursor style, and font size. You can set the custom values for these attributes in order to customize the links.

.nav li{
    padding: 10px;
    cursor: pointer;
    font-size: 16px;
}
.nav li:after{
  content: '';
  display: block;
    border-bottom: 2px solid #38fd07;
    transform: scaleX(0);
    transition: .2s ease-in-out;
}
.nav li:hover:after{
   transform: scaleX(1);
}

The following are some basic CSS styles for page typography and scroll bar. We defined 500px minimum height for the "para" class (page content area) to make the page scrollable. You can set the custom values for typography or skip these styles.

.para{
  min-height: 500px;
}
.para h3{
  text-align: center;
  margin-top: 15%;
  color: #fff;
  font-family: nyala;
  font-size: 44px;
}
    body::-webkit-scrollbar-track
{
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
  
}
body::-webkit-scrollbar
{
  width: 14px;
}

body::-webkit-scrollbar-thumb
{
  
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.5);
  background:  #501e27;
}
@media(max-width: 720px){
  body{
    background-size: 100% 100%;
  }
  .navbar-collapse{
  background: #501e27;
}
  .para{
    min-height: 800px;
  }
}

Making Navbar Solid on Scroll

In order to change the background color of the navbar, we’ll define a JavaScript function. In the following function, we check if the windows scrolling position is greater than 20 then change the background otherwise keep the "none" background. You can set the custom value for the background color that you wanted to have when the navbar becomes solid.

<script>
    window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    
    document.getElementById("navbar").style.background = "#501e27";
  } else {
   
    document.getElementById("navbar").style.background = "none";
  }
}
</script>

That’s all! Hopefully, now you are able to create a transparent navbar and convert into solid on scroll event. If you have any questions or suggestions, let me know by comment below.

You Might Be Interested In:

Muhammad Asif is a Front End Developer and Editor Staff at Codeconvey.com. He enjoys experimenting with new CSS features and helping others learn about them.

6 thoughts on “Navbar Transparent to Solid on Scroll using CSS”

  1. Hi,
    This article is a great help for me to make a transparent nav to solid in a bt5 page, However, I would like to change the color of the navlinks too when scrolling. I would highly appreciate any solution?
    Regards
    Ferenc

    • Hi Ferenc!
      You can change the color by adding the following line inside the if condition "document.body.scrollTop > 20".

      document.querySelectorAll(".nav li").forEach(el => el.style.color = "red");
      

      Change the color name “red” to your choice. Similarly, you can add the same statement inside the else condition to change the color on scroll up. The complete JS code will be:

          window.onscroll = function() {scrollFunction()};
      
      function scrollFunction() {
        if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
          
          document.getElementById("navbar").style.background = "#501e27";
          
            document.querySelectorAll(".nav li").forEach(el => el.style.color = "red");
            
        } else {
         
          document.getElementById("navbar").style.background = "none";
            
            document.querySelectorAll(".nav li").forEach(el => el.style.color = "white");
            
        }
      }
      

      If you added hyperlinks inside the li element, then your selector will be ".nav li a" instead of ".nav li".

    • Yes, you can use it in bootstrap 5.2.3

  2. hi i ve don it like this (see below),
    since i made nav bar not completely transparent i would like the font color to be white on top , and when scrolling become balck, do it need to be in the java script

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

    function scrollFunction() {
    if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {

    document.getElementById(“navbar”).style.background = “#ffffff”;
    } else {

    document.getElementById(“navbar”).style.background = “#19191960”;
    }
    }

    thank you

  3. Hello, thank you, i made it work in bootstrap 5.2.3
    I have a question…
    Since in my case i would like to have the navbar not completely transparent, but a see through gray “#19191960” instead on “none” with font color white, and when scrolled it should be white “#ffffff” with font color black.
    To achieve this, how should be done? in the JS?

Comments are closed.