Search code examples
htmlcsscarousel

CSS dropdown menu disappears when using bootstrap carousel


I have a dropdown nav menu (using css from w3schools) that works fine until a carousel is put immediately after. Then the dropdown no longer shows and I do not know why. I am new to css html .., so I don't find the solution to my problem. I have changed the Z-index and position on multiple classes and it doesn't solve the issue. Whenever i delete the bootstrap css link then the dropdown menu works again. Do you have any suggestions? thanks!

ps: i put all the code, sorry if i added more than the required, its my first time posting

/* CSS Document */

* {
  box-sizing: border-box;
  font-family: Arial, Helvetica, sans-serif;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.background-image {
  background-image: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0)), url("../img/header.jpg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  width: 100%;
}

.header-text {
  text-align: center;
  color: white;
  padding: 30px;
}


/* Add a black background color to the top navigation */

.topnav {
  background-color: #333;
  overflow: hidden;
}


/* Style the links inside the navigation bar */

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}


/* Change the color of links on hover */

.topnav a:hover {
  background-color: #1C8CE8;
  text-decoration: none;
  color: black;
}


/* Add an active class to highlight the current page */

.activenav {
  background-color: #4F47E1;
  color: white;
}


/* Hide the link that should open and close the topnav on small screens */

.topnav .icon {
  display: none;
}


/* Dropdown container - needed to position the dropdown content */

.dropdown {
  float: left;
  overflow: hidden;
}


/* Style the dropdown button to fit inside the topnav */

.dropdown .dropbtn {
  font-size: 17px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}


/* Style the dropdown content (hidden by default) */

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}


/* Style the links inside the dropdown */

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}


/* Add a dark background on topnav links and the dropdown button on hover */

.topnav a:hover,
.dropdown:hover .dropbtn {
  background-color: #555;
  color: white;
}


/* Add a grey background to dropdown links on hover */

.dropdown-content a:hover {
  background-color: #ddd;
  color: black;
}


/* Show the dropdown menu when the user moves the mouse over the dropdown button */

.dropdown:hover .dropdown-content {
  display: block;
}

.carousel {
  max-width: 100% !important;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.carousel-inner>.item>img {
  height: 600px!important;
  width: 100%;
}


/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child),
  .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}


/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {
    float: none;
  }
  .topnav.responsive .dropdown-content {
    position: relative;
  }
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

  <nav class="topnav" id="myTopnav">
    <a href="#home" class="activenav">Home</a>
    <div class="dropdown">
      <button class="dropbtn">Products 
            <i class="fa fa-caret-down"></i>
        </button>
      <div class="dropdown-content">
        <a href="#">Make</a>
        <a href="#">Model</a>
        <a href="#">Year</a>
      </div>
    </div>
    <a href="#news">News</a>
    <a href="#about">About</a>
    <a href="#contactus">Contact us</a>
    <a href="javascript:void(0);" class="icon" onclick="Functionnav()"><i class="fa fa-bars"></i></a>
  </nav>
  <section id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="img/img1slideshow.jpg" alt="Chania">
        <div class="carousel-caption">
          <h3>title</h3>
          <p>paragraph</p>
        </div>
      </div>

      <div class="item">
        <img src="img/img2slideshow.jpg" alt="Chania">
        <div class="carousel-caption">
          <h3>title</h3>
          <p>paragraph</p>
        </div>
      </div>

      <div class="item">
        <img src="img/img3slideshow.jpg" alt="Chania">
        <div class="carousel-caption">
          <h3>title</h3>
          <p>paragraph</p>
        </div>
      </div>

    </div>


Solution

  • Problem with css property overflow in the menu wrap. Use my below's code, hope it helps

    .dropdown {overflow: initial;}
    .topnav {
       overflow: visible;
       width: 100%;
       height: 53px;
    }