BlackHawkCH91
BlackHawkCH91

Reputation: 81

HTML button and div appearing over navbar but using z-index breaks animations

body {
    background-color: rgb(30, 21, 120);
    margin: 0;
    padding: 0;
}

p {
  color: white;
  font-size: 20px;
  display: inline;
  padding: 20px;
}

div.package {
    position: relative;
    border: 4px solid white;
    border-radius: 70px;
    margin: 60px 0 60px 0;
    background-color: rgb(37, 110, 194);
    padding: 40px;
    transition-duration: 0.4s;
}
p.package {
    color: white;
    padding-left: 3vw;
    font-size: 2vw;
    text-align: center;
    }
div.package:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 35px 60px 0 
    rgba(0,0,0,0.19);
}
.purchase {
    float: right;
    width: 20vw;
    height: 75px;
    background-color: rgb(10,27,64);
    color: white;
    border-color: rgb(24,146,40);
    position: relative;
    bottom: 17px;
    border-radius: 30px;
    border-style: solid;
    border-width: 5px;
    transition-duration: 0.4s;
    font-size: 2vw;
}
.purchase:hover {
    z-index: 2;
    background-color: rgb(116,164,242);
    color: black;
    width: 24vw;
}
.purchase_b {
    display: none;
}
.navbar {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: rgb(10,10,10);
    top: 0;
}

.navcont {
    margin: 0;
    position: relative;
    float: left;
}

.navcont a {
    margin: 0;
    position: relative;
    display: block;
    color: white;
    font-size: 20px;
    text-align: center;
    padding: 5vh 7vw;
    text-decoration: none;
    border-right: 1px solid rgb(50,50,50);
    border-left: 1px solid rgb(50,50,50);
}
.navcont a:hover {
    transition-duration: 0.3s;
    background-color: rgb(30,30,30);
}
.navcont a:active {
    background-color: rgb(9,194,36);
}
#navbarIMG {
    margin: 0;
}
.sticky {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
}
@media screen and (max-width: 600px) {
    .purchase_b {
        position: absolute;
        text-align: center;
        display: block;
    }
    button.purchase {
        width: 100%;
        bottom: 10px;
    }
    p.package {
        bottom: 100px;
        font-size: 2.65vw;
    }
    .package {
        height: 70px;
    }
    .purchase:hover {
        background-color: rgb(116,164,242);
        color: black;
        width: 100%;
    }
    p {
        font-size: 10px;
        padding: 0;
    }
    .navcont a {
        padding: 3vh 3vw;
    }
}
    <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>My site</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <div class="sticky">
                <ul class="navbar">
                    <li class="navcont"><a href="index.htm">Home</a></li>
                    <li class="navcont"><a href="#about">About</a></li>
                    <li class="navcont"><a href="#purchase">Purchase</a></li>
                    <li class="navcont" style="float: right;"><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <br>
            <br>
            <br>
            <br class="purchase_b">
            <br class="purchase_b">
            <div class="package">
                <p class="package">Recommended Package: Standard Package | Only $20 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            <div class="package">
                <p class="package">Popular Choice: Premium Package | Only $35 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
        </body>
    </html>

So I am currently having trouble with both the purchase box and purchase button appearing on top of the navigation bar. I tried using Z-index, however, the animations for the both the purchase box and button stopped working. Is there any way I can make the purchase box and button appear behind the nav bar? Thanks!

I have scrolled down a bit and the purchase box is going over the navbar.

Upvotes: 0

Views: 90

Answers (2)

Chris_00
Chris_00

Reputation: 406

I have changed the sticky class in the css code.

.sticky {
  position: fixed;
  z-index: 5;
  width: 100%;
}

body {
  background-color: rgb(30, 21, 120);
  margin: 0;
  padding: 0;
}

p {
  color: white;
  font-size: 20px;
  display: inline;
  padding: 20px;
}

div.package {
  position: relative;
  border: 4px solid white;
  border-radius: 70px;
  margin: 60px 0 60px 0;
  background-color: rgb(37, 110, 194);
  padding: 40px;
  transition-duration: 0.4s;
}

p.package {
  color: white;
  padding-left: 3vw;
  font-size: 2vw;
  text-align: center;
}

div.package:hover {
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 35px 60px 0 rgba(0, 0, 0, 0.19);
}

.purchase {
  float: right;
  width: 20vw;
  height: 75px;
  background-color: rgb(10, 27, 64);
  color: white;
  border-color: rgb(24, 146, 40);
  position: relative;
  bottom: 17px;
  border-radius: 30px;
  border-style: solid;
  border-width: 5px;
  transition-duration: 0.4s;
  font-size: 2vw;
}

.purchase:hover {
  z-index: 2;
  background-color: rgb(116, 164, 242);
  color: black;
  width: 24vw;
}

.purchase_b {
  display: none;
}

.navbar {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgb(10, 10, 10);
  top: 0;
}

.navcont {
  margin: 0;
  position: relative;
  float: left;
}

.navcont a {
  margin: 0;
  position: relative;
  display: block;
  color: white;
  font-size: 20px;
  text-align: center;
  padding: 5vh 7vw;
  text-decoration: none;
  border-right: 1px solid rgb(50, 50, 50);
  border-left: 1px solid rgb(50, 50, 50);
}

.navcont a:hover {
  transition-duration: 0.3s;
  background-color: rgb(30, 30, 30);
}

.navcont a:active {
  background-color: rgb(9, 194, 36);
}

#navbarIMG {
  margin: 0;
}

.sticky {
  position: fixed;
  z-index: 5;
  width: 100%;
}

@media screen and (max-width: 600px) {
  .purchase_b {
    position: absolute;
    text-align: center;
    display: block;
  }
  button.purchase {
    width: 100%;
    bottom: 10px;
  }
  p.package {
    bottom: 100px;
    font-size: 2.65vw;
  }
  .package {
    height: 70px;
  }
  .purchase:hover {
    background-color: rgb(116, 164, 242);
    color: black;
    width: 100%;
  }
  p {
    font-size: 10px;
    padding: 0;
  }
  .navcont a {
    padding: 3vh 3vw;
  }
}
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My site</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div class="sticky">
    <ul class="navbar">
      <li class="navcont"><a href="index.htm">Home</a></li>
      <li class="navcont"><a href="#about">About</a></li>
      <li class="navcont"><a href="#purchase">Purchase</a></li>
      <li class="navcont" style="float: right;"><a href="#contact">Contact</a></li>
    </ul>
  </div>
  <br>
  <br>
  <br>
  <br class="purchase_b">
  <br class="purchase_b">
  <div class="package">
    <p class="package">Recommended Package: Standard Package | Only $20 per month!</p>
    <br class="purchase_b" />
    <br class="purchase_b" />
    <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
  </div>
  <div class="package">
    <p class="package">Popular Choice: Premium Package | Only $35 per month!</p>
    <br class="purchase_b" />
    <br class="purchase_b" />
    <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
  </div>
</body>

</html>

Upvotes: 0

AG_
AG_

Reputation: 2689

I have added z-index:3 to .sticky class, have a look at below snippet.

body {
    background-color: rgb(30, 21, 120);
    margin: 0;
    padding: 0;
}

p {
  color: white;
  font-size: 20px;
  display: inline;
  padding: 20px;
}

div.package {
    position: relative;
    border: 4px solid white;
    border-radius: 70px;
    margin: 60px 0 60px 0;
    background-color: rgb(37, 110, 194);
    padding: 40px;
    transition-duration: 0.4s;
}
p.package {
    color: white;
    padding-left: 3vw;
    font-size: 2vw;
    text-align: center;
    }
div.package:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 35px 60px 0 
    rgba(0,0,0,0.19);
}
.purchase {
    float: right;
    width: 20vw;
    height: 75px;
    background-color: rgb(10,27,64);
    color: white;
    border-color: rgb(24,146,40);
    position: relative;
    bottom: 17px;
    border-radius: 30px;
    border-style: solid;
    border-width: 5px;
    transition-duration: 0.4s;
    font-size: 2vw;
}
.purchase:hover {
    z-index: 2;
    background-color: rgb(116,164,242);
    color: black;
    width: 24vw;
}
.purchase_b {
    display: none;
}
.navbar {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: rgb(10,10,10);
    top: 0;
}

.navcont {
    margin: 0;
    position: relative;
    float: left;
}

.navcont a {
    margin: 0;
    position: relative;
    display: block;
    color: white;
    font-size: 20px;
    text-align: center;
    padding: 5vh 7vw;
    text-decoration: none;
    border-right: 1px solid rgb(50,50,50);
    border-left: 1px solid rgb(50,50,50);
}
.navcont a:hover {
    transition-duration: 0.3s;
    background-color: rgb(30,30,30);
}
.navcont a:active {
    background-color: rgb(9,194,36);
}
#navbarIMG {
    margin: 0;
}
.sticky {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    z-index:3
}
@media screen and (max-width: 600px) {
    .purchase_b {
        position: absolute;
        text-align: center;
        display: block;
    }
    button.purchase {
        width: 100%;
        bottom: 10px;
    }
    p.package {
        bottom: 100px;
        font-size: 2.65vw;
    }
    .package {
        height: 70px;
    }
    .purchase:hover {
        background-color: rgb(116,164,242);
        color: black;
        width: 100%;
    }
    p {
        font-size: 10px;
        padding: 0;
    }
    .navcont a {
        padding: 3vh 3vw;
    }
}
<html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>My site</title>
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body>
            <div class="sticky">
                <ul class="navbar">
                    <li class="navcont"><a href="index.htm">Home</a></li>
                    <li class="navcont"><a href="#about">About</a></li>
                    <li class="navcont"><a href="#purchase">Purchase</a></li>
                    <li class="navcont" style="float: right;"><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <br>
            <br>
            <br>
            <br class="purchase_b">
            <br class="purchase_b">
            <div class="package">
                <p class="package">Recommended Package: Standard Package | Only $20 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            <div class="package">
                <p class="package">Popular Choice: Premium Package | Only $35 per month!</p>
                <br class="purchase_b" />
                <br class="purchase_b" />
                <button value="Purchase" class="purchase" style="cursor: pointer;">Purchase</button>
            </div>
            
        </body>
    </html>

Upvotes: 1

Related Questions