Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change body bgcolor on hovering a div, using CSS only

Tags:

css

I want that when I hover an element(a box made with css), the background color of the body changes from one color to another, for example white to red. The problem is that this should be done using css only and no javascript. And if javascript has to be neccesarily be used, then the color should change back to the previous one on mouse out.

---------------EDIT---------------

Actually I was trying this:

body{backgroung: #000;}
#div{some properties}
body #div:hover{background: #fff;}
like image 394
sdnr1 Avatar asked Dec 26 '12 11:12

sdnr1


People also ask

How do you make something change color when you hover over it CSS?

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.

How do you make a div hover in CSS?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.

What is CSS hover effect?

What is a CSS Hover Effect? A CSS hover effect takes place when a user hovers over an element, and the element responds with transition effects. It is used to mark the key items on the web page and it's an effective way to enhance the user experience.


3 Answers

Pure CSS experiment:

http://jsfiddle.net/Tymek/yrKRX/

HTML

<div id="trigger"></div>
<div id="bg"></div>​

CSS

body {
    height: 100%;
}

#bg {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    widht: 100%;
    height: 100%;
    z-index: 1;
    background: #EEE;
}

#trigger {
    position: absolute;
    width: 200px;
    height: 136px;
    top: 50%;
    left: 50%;
    margin: -68px 0 0 -100px;
    background: #333;
    z-index: 2;
}

/* KEY */
#trigger:hover ~ #bg {
    background: #EE0;
}​
like image 116
Tymek Avatar answered Oct 02 '22 21:10

Tymek


Please use like this

<html>
   <body>

     <style type="text/css">   

       .top{  
           background:red;
       }

       .top2{   
           background:white;
       }
     </style>

    <div class="top" onmouseover="this.className='top2'" 
                    onmouseout="this.className='top'">Here</div>
  </body>
</html>
like image 27
Sky5005e Avatar answered Oct 02 '22 23:10

Sky5005e


Use the :hover selector. It seems pretty straight forward unless you are doing something very different.

Check following example for reference:

.classname {
    background-color:white;
 } 

 .classname:hover {
    background-color:red;
 } 
like image 31
VJS Avatar answered Oct 02 '22 22:10

VJS