Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div on hover of direct parent only

Tags:

html

css

I am trying to make a div appear when you only hover its direct parent div. Right now the div will show when you hover any div of that type. See this simple example of the problem.

How can I make a .menu div only show when the mouse hovers directly over that div (and not it's child divs)?

.widget {
  width: 150px;
  height: 150px;
  background-color: #ddd;
  position: relative;
}
.menu {
  background-color: #444;
  display: flex;
  position: absolute;
  top: 0;
  display: none;
}
.widget:hover > .menu {
  display: flex;
}
<div class="widget" style="width: 500px; height: 500px; background-color: #eee;">
  <div class="menu">
    <p>I should ONLY be visible when you hover the big outer widget and NOT when you are hovering the small inner widget</p>
  </div>
  <div class="widget" style="left: 200px; top: 200px;">
    <div class="menu">
      <p>I should ONLY be visible when you hover the small inner widget</p>
    </div>

  </div>
</div>
like image 587
sazr Avatar asked Aug 26 '16 08:08

sazr


People also ask

How can we prevent child element affected by parents hover state?

Basically you want to apply a negative scale on hover. so you apply the positive scale to the parent div and a negative scale to the child.

Why Hover is not working on div?

You might have linked your stylesheet to your HTML file improperly. Some other CSS in the context of your project may be overriding the piece that you've given here. You might be running into browser compatibility issues with the :hover selector or something else in your code that is breaking the styling.

How can I see details on hover?

Answer: We can display an element on hover using :hover pseudo-class. The CSS :hover is a pseudo-class that triggers the HTML element when the mouse or cursor hovers it. We can use this : hover class and make an HTML element visible only when the cursor points to the element else the element will be invisible.


1 Answers

If you can't change the markup, there is no way you can target the first menu item and hide it when the child .widget is hovered. You will need to use JS.

I you cant use JS, you will need to change your makup, there are two ways you can achieve your aim:

Put the first .menu after the child .widget so you can target it with the .widget:hover > .widget:hover + .menu selector when the child widget is hovered :

.widget {
  width: 500px;
  height: 500px;
  position: relative;
  background-color: #eee;
}
.widget > .widget{
  left: 200px;
  top: 200px;
  width: 150px;
  height: 150px;
  background-color: #ddd;
}
.menu {
  background-color: #444;
  display: flex;
  position: absolute;
  top: 0;
  display: none;
}
.widget:hover > .menu {
  display: flex;
}
.widget:hover > .widget:hover + .menu {
  display: none;
}
<div class="widget">
  <div class="widget">
    <div class="menu"><p>I should ONLY be visible when you hover the small inner widget</p></div>
  </div>
  <div class="menu"><p>I should ONLY be visible when you hover the big outer widget and NOT when you are hovering the small inner widget</p></div>
</div>

A second approach would be to make the child .widget a sibling of the first:

.wrap {
  position: relative;
  width: 500px;
  height: 500px;
}
.widget {
  position: absolute;
  background-color: #ddd;
}
.w1 {
  width: 500px;
  height: 500px;
  background-color: #eee;
}
.w2 {
  left: 200px;
  top: 200px;
  width: 150px;
  height: 150px;
}
.menu {
  background-color: #444;
  display: flex;
  position: absolute;
  top: 0;
  display: none;
}
.widget:hover > .menu {
  display: flex;
}
.widget:hover > .widget:hover + .menu {
  display: none;
}
<div class="wrap">
  <div class="widget w1">
    <div class="menu"><p>I should ONLY be visible when you hover the big outer widget and NOT when you are hovering the small inner widget</p></div>
  </div>
  <div class="widget w2">
    <div class="menu"><p>I should ONLY be visible when you hover the small inner widget</p></div>
  </div>
</div>

On a side note, you should try not to use inline CSS.

like image 156
web-tiki Avatar answered Sep 28 '22 19:09

web-tiki