Muhammad Abdullah
Muhammad Abdullah

Reputation: 198

How to fix color-gradient to a fixed-top navbar?

I have a fixed navbar in my project that I have made using Bootstrap 4.I am using linear gradient for the background image and as one might imagine I had to make the navbar transparent. That was the easy part. My problem is that I also want to make the navbar fixed but when I scroll down the gradient disappears. Does anyone know how to prevent a transparent navbars background image from disapperaing whilst scrolling down a page? Thanks in advance for your answer. The code that I have written is given below:

CSS

.Mynavbar{
    background-image: linear-gradient(to right, rgba(0, 0, 0, 1), rgba(255, 255, 255, 1));
  }
  .Mynavbar .navbar {
  background-color: transparent !important;
}

HTML

  <div class="Mynavbar">
    <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
        <div class="Container container">
            <a class="navbar-brand" href="#">
                <img src="images/Logo.png" alt="Image Goes Here">
            </a>
            <button id="hamburger" class="navbar-toggler" type="button" data-toggle="collapse"
                data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
                aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse Navbaritems" id="navbarResponsive">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Home</a>
                        <span class="sr-only">(current)</span>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">About</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Products</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Services</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</div>

Note that I have added the necessary bootstrap references already but I have not shown them in this code snippet because I didn't want to make the question lengthy :)

Upvotes: 0

Views: 2981

Answers (2)

Muhammad Abdullah
Muhammad Abdullah

Reputation: 198

I found the answer. It was actually really simple. A silly mistake made by me. Turns out that I gave the div the gradient colors instead of the navbar so the final answer is:

     .Mynavbar .navbar {
      background-color: transparent !important;
   background-image: linear-gradient(to right black, white);
    }

This answer is to anyone who is/was wondering how to fix this. I swear I didn't know this answer from before. A really silly mistake! Sorry if I wasted your time :(

Upvotes: 1

Om_16
Om_16

Reputation: 686

Don't make it complex for CSS.. just use rgba values instead of rgb for the gradient's colors and specify their alpha value between 0-1, It will make your colors transparent without even adding background-color: transparent property... It will surely work for you

Upvotes: 0

Related Questions