1

I have the following html code, and I'm trying to change the background color of the div tags in such a way that when I mouse over the text, the background should turn to grey but when I move the mouse out of the tag, it should change to white. I'm trying to do this using css but I'm having a hard time coming up with the code. Is this possible using css only? Can someone show me an example?

<div class="custom-select">
<div style="background-color: grey;">TEST 1</div>
<div style="background-color: white;">TEST 2</div>
</div>

Thanks in advance.

wazz
  • 4,953
  • 5
  • 20
  • 34
Mark
  • 23
  • 1
  • 5
  • In order for us to help you better, please update your question so that it shows your relevant code in a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). It would be helpful if you could let us know [what you have tried so far](https://meta.stackoverflow.com/questions/261592) to solve your problem. For further information, please refer to the help article regarding [how to ask good questions](https://stackoverflow.com/help/how-to-ask). – wazz Jun 14 '18 at 05:21
  • 1
    https://www.w3schools.com/cssref/sel_hover.asp – wazz Jun 14 '18 at 05:21
  • Possible Duplicate: [div background color, to change onhover ](https://stackoverflow.com/questions/676324/div-background-color-to-change-onhover) – dekts Jun 14 '18 at 05:26
  • 1
    Possible duplicate of [div background color, to change onhover](https://stackoverflow.com/questions/676324/div-background-color-to-change-onhover) – davecar21 Jun 14 '18 at 05:28

6 Answers6

1

You can use onmouseover and onmouseout events like this:

var div = document.getElementById('div_id');

div.onmouseover = function() {

    this.style.backgroundColor = 'grey';
 
};

div.onmouseout = function() {

    this.style.backgroundColor = 'white';
 
};
Charlie
  • 22,886
  • 11
  • 59
  • 90
0

Use :hover to div and then change background-color

.custom-select .div1{
background-color:grey;
}
.custom-select .div2{
background-color:white;
}
.custom-select .div1:hover{
background-color:white;
}
.custom-select .div2:hover{
background-color:grey;
}
<div class="custom-select">
<div class="div1">TEST 1</div>
<div class="div2">TEST 2</div>
</div>
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
0

This can be done via css.

.custom-select .first,
.custom-select .second {
  background: white;
}

.custom-select .first:hover {
  background: gray;
}

.custom-select .second:hover {
  background: gray;
}
<div class="custom-select">
  <div class="first">TEST 1</div>
  <div class="second">TEST 2</div>
</div>
Shahil M
  • 3,836
  • 4
  • 25
  • 44
0

You can use CSS hover pseudo-selectors:

HTML:

<div class="custom-select">
    <div>TEST 1</div>
    <div>TEST 2</div>
</div>

CSS:

.custom-select > div {
    background: white;
}

.custom-select > div:hover {
    background: gray;
}
Spooze
  • 378
  • 1
  • 10
  • thanks for helping, how would the css code change if initially only the TEST 1 div was grey and then it would change to white once I move the mouse to TEXT2? – Mark Jun 14 '18 at 05:57
0

Since this is what you were looking for, I have updated my code snippet to include comments explaining what each part does.

/* PART 1 - Set first child's background colorinside custom-select to grey*/
.custom-select div:first-child
{ 
  background-color:grey; 
}

/* PART 2 - if hovering over custom-select 'undo' the styling set to the first child by setting background-color to white */
.custom-select:hover div:first-child{
  background-color: white;
}

/* PART 3 - Hover effect for divs inside custom-select */
.custom-select>div:hover{
  background-color: grey !important;
  /* !important is used to override the styling in PART 2 */
}
<div class="custom-select">
  <div>TEST 1</div>
  <div>TEST 2</div>
</div>
RyDog
  • 1,169
  • 1
  • 9
  • 12
  • It is very close to what I need. But on initial load, I need to set the background color of the first div to grey so I was using the code below. Then when you move the mouse over each tag it should behave like your code. .custom-select div:first-child{ background-color:grey; } – Mark Jun 14 '18 at 05:52
  • Thank you RyDog! This is what I was looking for. – Mark Jun 15 '18 at 04:50
  • No problem, I added some comments to give a brief explanation of whats going on . – RyDog Jun 15 '18 at 05:08
0

Here is an example by using :hover in css:

Edited

Using first-child & last-child to do without class:

.custom-select>div:first-child {
    background-color: gray;
}

.custom-select>div:last-child {
    background-color: white;
}

.custom-select>div:first-child:hover {
    background-color: white;
}

.custom-select>div:last-child:hover {
    background-color: grey;
}
<div class="custom-select">
  <div>TEST 1</div>
  <div>TEST 2</div>
</div>

Here is an example to modify the style of the other(MouseEvent):

var custom_select = document.getElementsByClassName('custom-select')[0];

var first = custom_select.children[0];
var second = custom_select.children[1];

first.onmouseenter = function(event){
  second.style['background-color'] = 'gray';
}
first.onmouseout = function(event){
  second.style['background-color'] = 'white';
}
second.onmouseenter = function(event){
  first.style['background-color'] = 'white';
}
second.onmouseout = function(event){
  first.style['background-color'] = 'gray';
}
.custom-select>div:first-child {
    background-color: gray;
}

.custom-select>div:last-child {
    background-color: white;
}
<div class="custom-select">
  <div>TEST 1</div>
  <div>TEST 2</div>
</div>
Terry Wei
  • 1,521
  • 8
  • 16
  • thanks for helping but unfortunately, I can't put classes inside the nested div tags. – Mark Jun 14 '18 at 06:33
  • I modified the example without class. – Terry Wei Jun 14 '18 at 06:37
  • If you have multiple div, you could use :nth-child(x) – Terry Wei Jun 14 '18 at 06:38
  • almost there, I changed your code to this, but is it possible to change the background of the first div to white when I hover to the last div? .custom-select>div:first-child{ background-color: gray; } .custom-select>div:last-child { background-color: white; } .custom-select>div:first-child:hover { background-color: white; } .custom-select>div:last-child:hover { background-color: grey; } – Mark Jun 14 '18 at 07:03
  • simply use css I think it is impossible.(if wrong please correct me). so I suggest to use JavaScript to do so. – Terry Wei Jun 14 '18 at 07:06
  • Thank you Terri Wei! I was able to use your code to solve my problem. – Mark Jun 15 '18 at 04:51