Search code examples
htmlcsstransparency

Text displays on top of popup menu


I made a simple HTML page with a simple drop-down menu (made with CSS). I have a problem with the drop-down text transparency: it shows the bottom text (as explained in the image below). The text in the menu is a link, so it's included in a tag. I tried to change the text color and opacity property but doesn't solve the problem. Any idea?

enter image description here

.menu {
  margin-right: 0px;
  margin-left: -40px;
}

li {
  display: block;
  float: left;
  width: 25%;
  height: 50px;
  line-height: 50px;
  font-family: Helvetica, sans-serif;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #000000;
}

.pat:hover {
  color: #EC008C;
}

.l {
  width: 100%;
  height: 50px;
  margin-left: -20px;
}

.sub-menu {
  visibility: hidden;
}

.menu .pat:hover .sub-menu {
  visibility: visible;
}
<ul class="menu">
  <li><a href="index.html">Home</a></li>
  <li class="pat">Patenti
    <ul class="sub-menu">
      <li class="l"><a href="patentea.html">Patente A</a></li>
      <li class="l"><a href="patenteb.html">Patente B</a></li>
      <li class="l"><a href="patsup.html">Patenti superiori</a></li>
    </ul>
  </li>
  <li>News</li>
  <li><a href="contatti.html">Contatti</a></li>
</ul>


Solution

  • Instead of visibility: hidden; and visibility: visible; on hover for submenus, it's better to use display: none and display: block, and use position: absolute on the submenu and position: relative on its parent menu entry.

    The reason display: none doesn't take any space (and position: absolute will prevent the visible submenu to change the design of the main menu entries), whereas visibility: hidden; reserves the space for the hidden element and just makes it invisible, which makes it impossible to properly position it independently of objects which it should cover when visible.