Home > other >  Why is my background image blocking and restricting use of my navbar?
Why is my background image blocking and restricting use of my navbar?

Time:01-18

I'm trying to create a sticky navbar for my homepage but when I put "position: fixed" to my nav, the ".home-banner .banner-img" image will appear in front of my navbar and the links won't work.

Picture of navbar not working: navbar not working

I can't figure out what is causing this, because I have another section of images but those don't affect my navbar.

Navbar working fine: navbar working

After looking around a bit I've noticed that my "home-banner" div was taking up my navbar's space. I've tried to add a div for my navbar to try to push the "home-banner" div down but it didn't work.

* {
  padding: 0%;
  margin: 0%;
  text-decoration: none;
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
  background-color: #fafcff;
}


/*top navigation bar*/

nav {
  background-color: green;
  height: 80px;
  width: 100%;
  overflow: hidden;
  position: fixed;
}

.logo {
  color: white;
  font-size: 32px;
  line-height: 80px;
  margin: 0 50px;
  float: left;
}

nav ul li {
  display: inline-block;
  line-height: 80px;
  margin: 0 25px;
}

nav ul li a {
  color: white;
  font-size: 18px;
  text-transform: uppercase;
  width: 60px;
}

.nav-left {
  float: left;
}

.nav-right {
  float: right;
  padding-right: 50px;
}

a:hover {
  color: red;
  transition: 0.5s;
}


/*homepage banner*/

.home-banner {
  position: relative;
  text-align: center;
  padding-top: 50px;
}

.banner-img {
  width: 50%;
  height: 600px;
  opacity: 75%;
}

.banner-text {
  color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  padding-top: 25px;
}

.banner-button {
  color: white;
  margin-top: 25px;
  padding: 10px 25px;
  border: none;
  background-color: #1352ab;
  cursor: pointer;
}


/*homepage 'product' section*/

.content {
  margin-top: 50px;
  text-align: center;
}

.content-header {
  margin-bottom: 25px;
  font-size: 28px;
}

.container {
  margin: 0 200px;
}

.item {
  width: 33%;
  display: inline-block;
  margin-bottom: 50px;
}

.item img {
  height: 300px;
  width: 450px;
  margin: 0 auto;
}

.caption {
  font-size: 20px;
}
<nav id="navbar">
  <a href="homepage.html">
    <h2 >OFM</h2>
  </a>

  <!--top navigation bar-->
  <div >
    <ul>
      <li><a href="homepage.html">Home</a></li>
      <li><a href="#">Shop</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>

  <div >
    <ul>
      <li><a href="#">Login</a></li>
      <li><a href="#">Cart</a></li>
    </ul>
  </div>

  <script>
    let nav = document.querySelector("nav");
    window.onscroll = function() {
      if (document.documentElement.scrollTop > 20) {
        nav.classList.add("sticky");
      } else {
        nav.classList.remove("sticky");
      }
    }
  </script>

</nav>

<main>
  <!--homepage banner-->
  <div >
    <img  src="organic.png" alt="background-pic">
    <div >
      <h2>MADE FOR ORGANIC LOVERS</h2>
      <button  onclick="window.location.href='#';">SHOP NOW</button>
    </div>
  </div>

  <!--homepage 'product' section-->
  <div >
    <h2 >PRODUCT CATEGORIES</h2>

    <div >
      <div >
        <a href="#">
          <img src="vegetables.jpg" alt="vegetables">
          <figcaption >Vegetables</figcaption>
        </a>
      </div>
      <div >
        <a href="#">
          <img src="fruits.jpg" alt="fruits">
          <figcaption >Fruits</figcaption>
        </a>
      </div>
      <div >
        <a href="#">
          <img src="meat.jpg" alt="meat">
          <figcaption >Meat</figcaption>
        </a>
      </div>
      <div >
        <a href="#">
          <img src="seadfood.jpg" alt="Seafood">
          <figcaption >Seafood</figcaption>
        </a>
      </div>
      <div >
        <a href="#">
          <img src="bakery.jpg" alt="bakery">
          <figcaption >Bakery</figcaption>
        </a>
      </div>
      <div >
        <a href="#">
          <img src="beverages.jpg" alt="Beverages">
          <figcaption >Beverages</figcaption>
        </a>
      </div>
    </div>

  </div>

</main>

CodePudding user response:

You have to give your navbar a z-index. The higher the z-index, the higher the element is on the z-axis:

nav {
    ...
    z-index: 1;
}

As soon as you specify a fixed position, the element no longer reserves space on the lower layer. So you should define a content container with a margin-top value that is at least as high as the navbar. Pseudo code:

<nav>...</nav>
<content>...</content>

nav {
    ...
    height: 50px;
}

content {
    ...
    margin-top: 50px;
}

CodePudding user response:

You are missing a top and left attribute on your nav styles. Since you don't specify them, the element will show some strange behavior.

You will also want to set a z-index on the nav bar so it is not overlapped or covered.

CodePudding user response:

The issue is that the .home-banner .banner-img image is positioned on top of the navbar because of the position: fixed property in the CSS. This is causing the links in the navbar to be unclickable because the image is covering them.

One solution is to give the nav element a higher z-index value than the .home-banner .banner-img element. This will ensure that the navbar is positioned on top of the image.

Try adding this in your CSS:

nav {
    z-index: 1;
}

and this also

.home-banner .banner-img {
    z-index: -1;
}

This will make the navbar appear on top of the banner-img and the links in the navbar will be clickable again.

  • Related