Skip to content Skip to sidebar Skip to footer

Css Change Body Background-color On Button Hover?

For my Website, I have a set background-color of the body, for example body { background-color: #FFFFFF; } I also have a button defined in the .html-File (Let's say it has the ID

Solution 1:

It's not possbile in CSS. In JavaScript it's quite easy, you're looking for onmouseover/out events.

var button = document.getElementById('hover');
var body = document.body;

button.onmouseover = function() {
	body.className = 'hovered';
}

button.onmouseout = function() {
	body.className = '';
}
body {background: #000;}
body.hovered {background: #ff0;}
<button id="hover">button</button>

Solution 2:

Using pure css, you can add a background div:

#button:hover ~ #background {
  background-color: red;
}

#background {
  position:absolute;
  background-color: blue;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  z-index: -1;
}
<button id="button">Button</button>
<div id="background"></div>

Post a Comment for "Css Change Body Background-color On Button Hover?"