Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On child hover, add background color to container

Tags:

html

css

hover

When the cursor hovers the left div, I need to overlap the whole wrapper, except for left div, with black color with opacity 0.2.

How can I do that in css? Thanks.

<div id="wrapper">
  <div id="left">... some elements</div>
  <div id="right">... some elements</div>
</div>
like image 750
Denis Stephanov Avatar asked Aug 14 '16 14:08

Denis Stephanov


People also ask

How do you change color when hovering?

Changing link color on hover using CSS To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

Can you put a background color for a div?

To set a background color for a div or related element in CSS, you use the background-color property. While setting this background color, your creativity is your limit as to how far you want to go.

How do you change the background color of a container in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.

How do you add a background color in navigation?

Use any of the . bg-color classes to add a background color to the navbar. Tip: Add a white text color to all links in the navbar with the . navbar-dark class, or use the .


2 Answers

You can use general sibling combinator (~) and a div with absolute position to obtain this effect. In that example, you would be selecting the div with the class ".bgr" that come after the hovered child and making it pink/ blue.

#wraper {
position:relative;
width:200px;
height:200px;
}
.bgr {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background:#fff;
padding:30px;
}
#left, #right {
position:relative;
z-index:1;
width:200px;
height:100px;
border:1px solid #333;
margin:20px;
}
#left{
background:#fff;
}
#right{
background:#f1f1f1;
}
#left:hover {
background:#f9f9f9;
}
#right:hover {
background:#f9f9f9;
}
#left:hover ~ .bgr {
background:blue;
}
#right:hover ~ .bgr {
background:pink;
}
<div id="wrapper">
<div id="left"> ... some elements </div>
<div id="right"> ... some elements </div>
<div class="bgr"></div>
</div>
like image 60
Madalina Taina Avatar answered Sep 25 '22 20:09

Madalina Taina


You could do that by applying a very large box-shadow which is black and has 0.2 opacity by using rgba() color.

The container (#wrapper) must have overflow: hidden to hide excess shadow.

#wrapper {
  border:1px solid red;
  padding: 1em;
  overflow: hidden;
}

#wrapper > div {
  border: 1px solid blue;
}

#left:hover {
  box-shadow: 0 0 10em 10em rgba(0,0,0,0.2);
}
<div id="wrapper">
    <div id="left"> ... some elements </div>
    <div id="right"> ... some elements </div>
</div>

jsFiddle: https://jsfiddle.net/azizn/sfq252g5/

like image 25
Aziz Avatar answered Sep 25 '22 20:09

Aziz