Home > Software design >  How do I add a dropdown menu in a navbar that doesn't move the rest of the content
How do I add a dropdown menu in a navbar that doesn't move the rest of the content

Time:11-03

I am trying to add a dropdown menu to my navbar but if I specify position:absolute in CSS, it detaches itself from the navbar and if i leave it as position:relative, it moves the rest of the content on the page. How do I avoid this?

https://jsfiddle.net/oxg92p3d/1/#&togetherjs=GncaTL4Kdv

nav {
  width: 100%;
  margin: 0 0;
  background: #fff;
  padding: 0px 0;
  position: relative;
  z-index: 10;
  margin-bottom: 0px;
}

nav ul {
  list-style: none;
  text-align: center;
  position: relative;
}

nav ul li {
  display: inline-block;
  position: relative;
}

nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: black;
  font-weight: 800;
  font-family: Geneva, Verdana, sans-serif;
  text-transform: uppercase;
  margin: 0 10px;
}

nav ul li a,
nav ul li a:after,
nav ul li a:before {
  transition: all .5s;
}

nav ul li a:hover {
  color: #4CAF50;
}

nav ul li ul {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: black;
  font-weight: 800;
  font-family: Geneva, Verdana, sans-serif;
  text-transform: uppercase;
  margin: 0 10px;
  z-index: 100;
  position: absolute;
}

nav ul li ul li {
  position: absolute;
  display: none;
  padding: 15px;
  text-decoration: none;
  color: black;
  font-weight: 800;
  font-family: Geneva, Verdana, sans-serif;
  margin: 0 10px;
  width: 1%;
  z-index: 10;
  height: 10px;
}

nav ul li ul li a {
  white-space: nowrap;
  z-index: 10;
  left: -24%;
  position: relative;
}

nav ul li ul:hover li a.league {
  z-index: 10;
  position: absolute;
}

nav ul li ul:hover li {
  z-index: 10;
  position: relative;
  display: block;
}

nav.stroke ul li a,
nav.fill ul li a {
  position: relative;
}

nav.stroke ul li a:after,
nav.fill ul li a:after {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 0%;
  content: '.';
  color: transparent;
  background: #4CAF50;
  height: 1px;
}

nav.stroke ul li a:hover:after {
  width: 100%;
}

nav.fill ul li a {
  transition: all 2s;
}

nav.fill ul li a:after {
  text-align: left;
  content: '.';
  margin: 0;
  opacity: 0;
}

nav.fill ul li a:hover {
  color: #fff;
  z-index: 1;
}

nav.fill ul li a:hover:after {
  z-index: -10;
  animation: fill 1s forwards;
  -webkit-animation: fill 1s forwards;
  -moz-animation: fill 1s forwards;
  opacity: 1;
}

* {
  margin: 0;
  padding: 0;
  position: relative;
  z-index: 9;
}

.button {
  background-color: #4CAF50;
  width: 25%;
  border: none;
  color: white;
  padding: 8px 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  font-weight: 500;
  margin: 8px 0;
  transition-duration: 0.4s;
  border-radius: 6px;
  cursor: pointer;
}

input[type=text],
select {
  width: 25%;
  padding: 10px 10px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 6px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 25%;
  background-color: #50ba54;
  color: rgb(0, 0, 0);
  padding: 10px 10px;
  margin: 8px 0;
  text-align: center;
  font-size: 13px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition-duration: 0.4s;
}

.input1:hover {
  background-color: #429a45;
}

.button1 {
  background-color: white;
  color: black;
  border: 1px solid #d0d0d0;
}

.button1:hover {
  background-color: #4CAF50;
}

.button2 {
  background-color: white;
  color: black;
  border: 2px solid #4CAF50;
  top: 35%;
  left: 87%;
  position: absolute;
}

.button2:hover {
  background-color: #4CAF50;
  color: white;
}

div {
  line-height: 0;
  height: 10px;
}

.league-menu {
  width: 380px;
  margin: 140px auto;
  position: relative;
  right: 25px;
}

.league-menu .league-select-btn {
  display: flex;
  height: 0px;
  width: 300px;
  background: #fff;
  padding: 20px;
  font-size: 18px;
  font-weight: 400;
  border-radius: 8px;
  align-items: center;
  cursor: pointer;
  justify-content: space-between;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

.league-select-btn i {
  font-size: 25px;
  transition: 0.3s;
}

.league-menu.active .league-select-btn i {
  transform: rotate(-180deg);
}

.league-menu .leagues {
  position: relative;
  z-index: 2;
  width: 300px;
  padding: 20px;
  margin-top: 10px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
  display: none;
}

.league-menu.active .leagues {
  display: block;
  position: absolute;
  left: 40px;
}

.leagues .league {
  display: flex;
  height: 35px;
  cursor: pointer;
  padding: 20px 16px;
  border-radius: 8px;
  align-items: center;
  background: #fff;
}

.leagues .league:hover {
  background: #F2F2F2;
}

.league i {
  font-size: 25px;
  margin-right: 12px;
}

.league .league-text {
  font-size: 18px;
  color: #333;
  clear: right;
}

header {
  z-index: 9;
  position: relative;
}
<nav >
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="loginScreen.html">Login</a></li>
    <li><a href="createRoom.html">Create Room</a></li>
    <li><a href="mainRoom.html">Main Room</a></li>
    <li><a href="register.html">Register</a></li>
    <li><a href="placeBets.html">Place Bets</a></li>
    <li>
      <ul >View Standings
        <li >
          <a  href="viewPremierStandings.html">Premier League</a>
        </li>
        <li >
          <a  href="viewBundesligaStandings.html">Bundesliga</a>
        </li>
        <li >
          <a  href="viewLigue1Standings.html">Ligue 1</a>
        </li>
        <li >
          <a  href="viewLaLigaStandings.html">La Liga</a>
        </li>
        <li >
          <a  href="viewSerieAStandings.html">Serie A</a>
        </li>
        <li >
          <a  href="viewMLSStandings.html">MLS</a>
        </li>
      </ul>
    </li>
    <li><a href="placeBets.html">Place Bets</a></li>
  </ul>
</nav>

<br>
<header style="color: #000000; font-family: Geneva, Verdana, sans-serif; font-size: 35px; font-weight: 800;">Welcome to WagerBoard</header>
</head>

<div>
  <b style="font-size: 15px; font-weight: 100; position: relative; z-index: 9;">
    Create a room or use a room id
  </b>
</div>

<div>
  <button id="button" >
      New Room
    </button>
  <script type="text/javascript">
    document.getElementById("button").onclick = function() {
      location.href = "createRoom.html";
    };
  </script>
</div>
<div>
  <span style="font-size: 16px; vertical-align: -13px; color: rgb(134, 134, 134);">or</span>
</div>
<div>
  <input id="Enter a Room Code" type="text" name="text">
</div>
<div>
  <input id="Submit Room Code"  type="Submit" value="Join Room" onclick="getInput()">
  <script src="../scripts/index.js">
    getInput();
  </script>
</div>

CodePudding user response:

you should put dropdown links inside a div


<li>
       <ul  style="position:relative">View Standings
           <div >
            <li >
              <a  href="one.html">one</a>
            </li>
             <li >
              <a  href="two.html">two</a>
            </li>
            <li >
              <a  href="etc.html">etc</a>
            </li>
          </ul>
</li>


Now you have to style the new division you created with css for example :

.inside{
        height:max-content;
        position:absolute;
        width:100%;
 }

fiddle : https://jsfiddle.net/ecu081j5/12/

  • Related