1

Trying to practice creating a dropdown. Not sure if this is even the best way, but here's my take on it.

https://jsfiddle.net/dtkreox3/1/

HTML

<ul class="test">
  <li class="main"><a href="#">Hover</a>
    <ul class="sub">
      <li>Link 1</li>
      <li>Link 1</li>
      <li>Link 1</li>
      <li>Link 1</li>
     </ul>
  </li>


  <li class="main"><a href="#">About</a></li>
</ul>

CSS

.test {
  border: 1px solid black;
  width: 500px;
}

.main {
  display: inline-block;
  width: 100px;
  border: 1px solid black;
}

.sub {
  border: 1px solid black;
  list-style: none;
  width: 50px;
  display: none;
}

.main:hover .sub {
  display: block;
}

I don't understand why when you hover over the first menu, the "About" gets dropped down. How do I fix this?

also is display: none the best way to hide the content? what about visibility: 0 and opacity: 0?

Thanks

1
  • thanks for the input everyone Commented Jan 31, 2016 at 17:25

4 Answers 4

2

Add position:relative to .main and position:absolute to .sub

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

1 Comment

The relative positioning of the container sets this apart from the other answers.
0

You have to add position:absolute in .sub class

.sub {
  border: 1px solid black;
  list-style: none;
  width: 50px;
  display: none;
  position: absolute;
}

Comments

0

just add position:absolute to .sub like this:

.sub {
  border: 1px solid black;
  list-style: none;
  width: 50px;
  display: none;
  position: absolute;
}

Comments

0

you should add position absolute to sub class and relative to class main

`.sub {
  border: 1px solid black;
  list-style: none;
  width: 50px;
  display: none;
  position: absolute;
  }

.main {
  display: inline-block;
  width: 100px;
  border: 1px solid black;
  position: relative;
}`

Comments

Your Answer

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