Advanced Search

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Changing background on hover using CSS Classes

  1. #1
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Changing background on hover using CSS Classes

    In order to dynamically change the background colour of a table row when the mouse is over it I am currently using something like:

    Code:
    <TR onMouseOver="this.style.backgroundColor='#E1EAFE'"; onMouseOut="this.style.backgroundColor='transparent'">
    I'd like to get away from using mouse events and use pure styles, ideally created as classes and placed in a separate linked style sheet. Can this be done?

    Please that whilst there may be a hyperlink in the row, more often than not there won't be, but I still want the background colour to change.

    Cheers

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,913
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    tr:hover {
    background-color: #e1eafe;
    }
    However, I have a horrible feeling this might not work in IE.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    27,544
    Thanks
    42
    Thanked 2,878 Times in 2,850 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    Code:
    tr:hover {
    background-color: #e1eafe;
    }
    However, I have a horrible feeling this might not work in IE.
    It won't. IE only uses the hover pseudo class on links.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,913
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's what I thought. Sorry, Alistair. The most common solution to this is to change the class with Javascript, but if you don't want to use Javascript, you're... you can't.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK guys and thanks. I just wanted to avoid having all this extra code in a table which is going to grow quite large in the coming months. But if can't be done, it can't be done.

    Cheers

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    27,544
    Thanks
    42
    Thanked 2,878 Times in 2,850 Posts
    Blog Entries
    12

    Default

    Well, using css to do background color rollovers for table cells can be done in IE, it just requires a lot more effort than Mozilla. Formatting of the contents of the cell becomes a particular challenge. Here is a rather basic example that also shows some of the ways you might use to achieve the desired formatting of text:
    Code:
    <html>
    <head>
    <title></title>
    <style type="text/css">
    a.bg{
    width:100%;
    height:100%;
    text-decoration:none;
    cursor:default;
    color:black;
    padding-top:.6ex;
    margin-bottom:-.4ex;
    }
    a.bg:hover{
    background-color:pink;
    width:100%;
    height:100%;
    }
    td.bg {
    padding:0;
    margin:0;
    }
    </style>
    </head>
    <body>
    <table height="300" border="1">
    <tr>
        <td height="225"><img src="thumb2/photo9.jpg" width="140" height="225" border=0 alt="" title=""></td>
    </tr>
    
    <tr>
        <td class="bg"><a class="bg" href="javascript:void(0);"><br><center>Hi!</center></a></td>
                                                            </tr>
                                                            </table>
                                                            
    </body>
    </html>
    The real challenge is hiding all this garbage from other browsers.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,913
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, he wanted the whole row's class to change, because sometimes there won't be a link in it.
    Quote Originally Posted by Alistair
    Please that whilst there may be a hyperlink in the row, more often than not there won't be, but I still want the background colour to change.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    27,544
    Thanks
    42
    Thanked 2,878 Times in 2,850 Posts
    Blog Entries
    12

    Default

    It can still be fudged around, the row could span all columns and therefore be a cell, to make it look like a row, another table could be put in it with no background specified. As I said before though, the real problem is hiding it all from other browsers.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,913
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    But it's exactly the same problem with a cell as with a row: IE still doesn't support hover on it. Unless there's a way to technically make the link fill the whole cell, whilst still appearing to be the same size.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    27,544
    Thanks
    42
    Thanked 2,878 Times in 2,850 Posts
    Blog Entries
    12

    Default

    Try my example code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •