2

In the current project I use to two html tags. The second one is hidden by default. If you hover the first one the second one will appear. If you head over the the second now visible one it stays open.

#button{
  background:#050;
  width:200px;
  height:30px;
  line-height:30px;
  text-align:center;
  cursor:pointer;
}
#button{
  color:white;
  text-decoration:none;
}

#button:hover{
  color:#ff4;
  text-decoration:none;
}

#subfield{
  width:500px;
  height:300px;
  background:#777;
}

#button + #subfield{
  display:none;
}

#button:hover + #subfield{
  display:block;
}

#button + #subfield:hover{
  display:block;
}
<div id="button">Menu Button</div>
<div id="subfield"></div>

That works fine. But the first html tag has a hover effect itself. How can I keep this hover effekt while heading over to the other html tag?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Pete
  • 564
  • 4
  • 29

1 Answers1

2

You don't have a parent selector or previous selector in CSS. Simple fix, give :hover for the parent of the two:

  1. Give a parent div.
  2. Apply display: inline-block.
  3. Give the :hover to the parent, instead of the button.

#parent {
  display: inline-block;
}
#button {
  background: #050;
  width: 200px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  cursor: pointer;
}
#button {
  color: white;
  text-decoration: none;
}
#parent:hover #button {
  color: #ff4;
  text-decoration: none;
}
#subfield {
  width: 500px;
  height: 300px;
  background: #777;
}
#button + #subfield {
  display: none;
}
#button:hover + #subfield {
  display: block;
}
#button + #subfield:hover {
  display: block;
}
<div id="parent">
  <div id="button">Menu Button</div>
  <div id="subfield"></div>
</div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252