1

I have some div such as :

<div onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#FFFFFF'">

So when i go on and out with the mouse, they change colour. Any way to do

2 Answers 2

9

Yes:

div{
   background-color:#CCC;
}
div:hover{
   background-color:#FFF;
}

However, beware that this might fail in some browsers (IE6-) because :hover was at the start only meant to work with links.

Sign up to request clarification or add additional context in comments.

5 Comments

Note, not all browsers support :hover on other elements than a.
Although it won't (and shouldn't) matter to most people, if you would like this code to work in IE6, you can use Whatever:hover.
It will work on all browsers, check out the link that thirtydot posted.
on IE6 for example :) I wanted to know if there are any ways, but add patch/script to do workaround :) But ok, I'll surrender on this i think... :)
In fact, if the users use IE6 can't see this effect, but the div keep his aspect, right? So I can do it anyway....
0

Are you using the <div> as a link? Keep in mind that you can just use an actual anchor, <a>, and make it block level so it can maintain the width of height of your choosing.

a {
  display: block;
  width: 200px;
  background-color: #ccc;
}
a:hover {
  background-color: #fff;
}

1 Comment

Yeah, but I don't want to be a link-div, just get the effect :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.