0

I have the following code

.navbar {
  overflow: hidden;
  background-color: #2A3D5A;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #69788C;
  text-align: center;
  padding: 10px;
  text-decoration: none;
  font-size: 14px;
  width: 20%;
}

.navbar a:hover {
  background: #4F76B1;
  color: white;
}

.navbar a.active {
  background: #4F76B1;
  color: white;
}
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>

How to add a dropdown list that contains link 1, link 2 and link 3 when hovering the News tab.

I found many examples on the internet, but I couldn't just add it to this code instead I had to rewrite this code. Can you suggest a simple enhancement to this existing code to do the extra function.

1
  • use a <div> to wrap around the News <a> Tag, from there you can use css :hover to display/hide the submenu. Commented Sep 27, 2022 at 14:40

1 Answer 1

1

Is this what you're looking for? It should be fairly self-explanatory but drop me a comment if not and I'll explain.

.navbar {
  background-color: #2A3D5A;
  position: fixed;
  top: 0;
  width: 100%;
}

.navbar a {
  float: left;
  display: block;
  color: #69788C;
  text-align: center;
  padding: 10px;
  text-decoration: none;
  font-size: 14px;
  width: 20%;
}

.navbar a:hover {
  background: #4F76B1;
  color: white;
}

.navbar a.active {
  background: #4F76B1;
  color: white;
}

#news {
  position: relative;
}

.menu {
  position: absolute;
  display: none;
  top: 2rem;
  background-color: green;
  width: 100%;
  padding: 0.25rem;
}

#news:hover .menu {
  display: block;
}

.menuitem {
  padding-block: 0.125rem;
}
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news" id='news'>News
    <div class='menu'>
      <div class='menuitem'>Link1</div>
      <div class='menuitem'>Link2</div>
      <div class='menuitem'>Link3</div>
    </div>
  </a>
  <a href="#contact">Contact</a>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.