Reputation: 4101
Similar to this question, but I'm not sure if the answer applies.
I'm trying to make it so that when you hover over any portion of this parent div:
The top div within it changes background-color:
I have:
/*
Maybe something like this?
var oldBackground;
var parentNode = document.getElementByClassName('item');
var childNodes = document.getElementsByClassName('child');
parentNode.onmouseenter = e => {
var c = e.target.childNodes[0]
c && ((oldBackground = c.style.background) &c.style.background="blue")
};
parentNode.onmouseout = e => {
var c = e.target.childNodes[0]
c && oldBackground && (c.style.background=oldBackground)
};
*/
.dashboard {
display: flex;
}
.dashboard .item {
border-radius: .25rem;
border: 1px solid rgba(0,0,0,.125);
background: white;
text-align: center;
flex-grow: 1;
margin-right: 20px;
width: 20%;
}
.dashboard .item:hover {
cursor: pointer;
border: 1px solid #A7A7A7;
}
.dashboard-item-top {
border-bottom: 1px solid #dfdfdf;
padding: 2px;
font-size: 20px;
transition: background-color 0.5s ease;
/*this background color should change to #F2F3F3 when
.item div is hovered*/
}
.info-block-item {
font-size: 20px;
}
<div class="dashboard">
<div class="item">
<div class="dashboard-item-top">
Classes
</div>
<div class="dashboard-item-bottom">
<span class="info-block-item">0</span>
</div>
</div>
</div>
I'm not sure where to begin. Is it better to try this with JavaScript / jQuery or CSS?
EDIT: Updated, working JSFiddle
Upvotes: 0
Views: 1712
Reputation: 884
The below example might help in achieving what you want through CSS alone.
#parent {
cursor: pointer;
}
#parent:hover .parent-highlight {
background-color: red;
color: white;
}
<div id="parent">
<div>hover over me</div>
<div>hover over me</div>
<div>hover over me</div>
<div class="parent-highlight">I get highlighted</div>
</div>
Upvotes: 1
Reputation: 1721
You can add additional hover behavior in CSS to a child element like so:
/*on hovering the .item class, manipulate the child with the class .dashboard-item-top*/
.dashboard .item:hover > .dashboard-item-top {
background: #CECECE;
/*any other styles*/
}
.dashboard {
display: flex;
}
.dashboard .item {
border-radius: .25rem;
border: 1px solid rgba(0,0,0,.125);
background: white;
text-align: center;
flex-grow: 1;
margin-right: 20px;
width: 20%;
}
.dashboard .item:hover {
cursor: pointer;
border: 1px solid #A7A7A7;
}
.dashboard .item:hover > .dashboard-item-top {
background: #CECECE;
}
.dashboard-item-top {
border-bottom: 1px solid #dfdfdf;
padding: 2px;
font-size: 20px;
transition: background-color 0.5s ease;
/*this background color should change to #F2F3F3 when
.item div is hovered*/
}
.info-block-item {
font-size: 20px;
}
<div class="dashboard">
<div class="item">
<div class="dashboard-item-top">
Classes
</div>
<div class="dashboard-item-bottom">
<span class="info-block-item">0</span>
</div>
</div>
</div>
Upvotes: 4
Reputation:
What you want to do essentially is change the color of the first child node, when the parent node is hovered over. I also assume you want to change it back when the hover leaves. So you need to add a mouseenter and mouseout event, changing e.target.childNodes[0].style.background
So, psuedo code
var oldBackground;
parentNode.onmouseenter = e => {
var c = e.target.childNodes[0]
c && ((oldBackground = c.style.background) &c.style.background="blue")
};
parentNode.onmouseout = e => {
var c = e.target.childNodes[0]
c && oldBackground && (c.style.background=oldBackground)
};
Upvotes: 1