1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is This Possible?? (Change div background on hover..)

Discussion in 'CSS' started by wd_2k6, Feb 28, 2008.

  1. #1
    Hi OK picture this...

    I have 1 div.
    Inside it is a thumbnail img floated left.
    Then some text to the right of the img regarding the link and views, tags information etc..

    I was wondering if i could change the background colour/background image when a user hovers over this div??

    But i would like the different links inside the div to still remain valid e.g links for tags.
     
    wd_2k6, Feb 28, 2008 IP
  2. deques

    deques Peon

    Messages:
    206
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    .divclass:hover {background-color: #FFF;}

    i guess you mean changing the background colour of this div element
     
    deques, Feb 28, 2008 IP
  3. nicangeli

    nicangeli Peon

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That wont work in I.E will it?
     
    nicangeli, Feb 28, 2008 IP
  4. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not without javascript or whatever:hover, no. But it would work for everyone else.

    wd, you could wrap the whole image and text in an <a>nchor, which would make everyone do something on hover. It would change the background colour of that group... but if there are multiple images/links in one div, then none of them alone can affect the whole div.
     
    Stomme poes, Feb 28, 2008 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have multiple links inside the div...
    What i tried is:
    div.whatever:hover { background:url(whatever.jpg) ;} This seemed to work OK in FF and I.E and changed the background image when the mouse was on the div?
     
    wd_2k6, Feb 28, 2008 IP
  6. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Heh, by "whatever:hover" I meant this file that tells IE6 to recognise :hover on something other than a <a>...
    website: http://www.xs4all.nl/~peterned/csshover.html
    file: http://www.xs4all.nl/~peterned/htc/csshover2.htc

    If you wanted the whole div which contains a bunch of links to do that same thing whenever any of the links inside is hovered over (not NOT simply when the div itself was hovered meaning you only wanted this to happen when a LINK was hovered), then you could do something fancy like this:
    <div id="bar">
    <div id="foo">
    <a href="#"><img src="foo.gif" width="" height="" alt="" />text text text</a>
    <a href="#"><img src="bar.gif" width="" height="" alt="" />text text text</a>
    <a href="#"><img src="spam.gif" width="" height="" alt="" />text text text</a>
    </div>
    </div>

    #foo {
    background-color: white;
    }
    #bar :hover > #foo {
    background-color: red;
    }

    Eh, not sure if this works, it's something like, if some child in #bar is hovered over, then its child #foo turns red. Maybe it could be more specific like
    #bar a:hover > #foo {
    background-color: red;
    }
    When an anchor which is some (not direct) child of #bar is hovered, #foo changed red. Of course, IE6 doesn't know what > is, so no go.

    It's much easier to do it for just changing the image which is inside the <a> and would also work with IE6. But if the parent needs to be affected because of the child being hovered, that's harder (like above, which I don't even know if it works).
     
    Stomme poes, Feb 29, 2008 IP
  7. SEOaaron

    SEOaaron Peon

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I want a robust way of changing a background image of a DIV, on hover. As I understand it I need to either use JS or wrap it in an anchor? Am I correct?
     
    SEOaaron, Sep 10, 2010 IP
  8. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #8
    Hi!

    I Think U Should Try Javascript :S

    <table border="1" width="100%">
    <tr id="tr1" onmouseover="document.getElementById('tr1').style.backgroundColor='#ccc';" onmouseout="document.getElementById('tr1').style.backgroundColor='#fff';">
    <td>
    &nbsp;
    </td>
    </tr>
    <tr id="tr2" onmouseover="document.getElementById('tr2').style.backgroundColor='#f00';" onmouseout="document.getElementById('tr2').style.backgroundColor='#fff';">
    <td>
    &nbsp;
    </td>
    </tr>
    <tr id="tr3" onmouseover="document.getElementById('tr3').style.backgroundColor='#ff0';" onmouseout="document.getElementById('tr3').style.backgroundColor='#fff';">
    <td>
    &nbsp;
    </td>
    </tr>
    </table>
     
    Last edited: Sep 10, 2010
    HungryMinds, Sep 10, 2010 IP