Setting an Image Background that Changes on Mouseover

Discussion in 'CSS' started by travoholic, May 1, 2007.

  1. #1
    Hey guys,

    I'm slowly getting a grasp on the basics of CSS but images and divs still have me a bit baffled.

    I've got an image that's 100x100 and I'd like to have an image border around it that has 2 or 3 pixels of light grey background and then a darker grey 1px border. When the image is scrolled over, I'd like the backround and border to change colour. Is this possible?

    I'd like to add similar border to all my images although not all of them will need the scrollover thing.

    Can anyone point me in the right direction?

    Thanks!
    Kirsty
     
    travoholic, May 1, 2007 IP
  2. Obelia

    Obelia Notable Member

    Messages:
    2,083
    Likes Received:
    171
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Yes, but you might need to use Javascript. There are certainly ways you can make images rollover with that. With that, you could make your border image an actual image, which would allow you more control over the way it looks and wouldn't restrict you to simply using a square box.

    A search for "Javascript rollover" should yield plenty of tutorials.

    However, with CSS you can use the "a hover:" attribute combined with making the image a link, to get a mouseover style. For instance, this will style your links grey:
    A:hover {
    COLOR: #999999
    }
    But if you simply use "a hover:", that would style all your links, not just the image.

    I found this on MSDN (the page is no longer there, however):

    http://216.109.125.130/search/cache...asp&w=img+hover&d=FvUSePmdOn7a&icp=1&.intl=us
     
    Obelia, May 1, 2007 IP
  3. semantic7

    semantic7 Member

    Messages:
    92
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    48
    #3
    This is actually possible using just css. In order to achieve what you are aiming for, follow these steps...

    Style an a element as block [ display:block ]
    width and height [ 100 x 100 ]
    padding: 3px
    background color light grey
    border color dark grey
    background image no-repeat center center

    then under a:hover put in the styles for the hover state which would different border and background color.

    Since you are aiming to be specific remember to assign a element a class or an id
     
    semantic7, May 1, 2007 IP
  4. travoholic

    travoholic Well-Known Member

    Messages:
    299
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Thanks guys.

    Semantic7 cheers for that, I was pretty sure it could be done with only CSS. Any change you could spell it out to me like I'm an idiot though as my CSS skills are pretty horrible. If you or someone could tell me what to type in my stylesheet and what exactly to put into my HTML file it'd be a life saver.

    Thanks again!
    Kirsty
     
    travoholic, May 2, 2007 IP
  5. semantic7

    semantic7 Member

    Messages:
    92
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    48
    #5
    If you can post a link to your site or an exact picture of what you want to achieve I can give you the exact code. Without that its difficult.
     
    semantic7, May 2, 2007 IP