Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: CSS equivalent of <img> "src"attribute?

  1. #1
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CSS equivalent of <img> "src"attribute?

    I am creating dynamically an <img> element in Javascript. I set the "src" attribute to a default image file path i.e. 'src="images/defaulticon.jpg"' and I would like to be able to override that path using CSS. I checked my CSS books and the list previous posts and could not find anything. I tried
    "img {background: url('images/myicon.jpg')" and got the image in the background but the image with a red cross is also surimposed.

    Basically I would like to see the myicon.jpg and not defaulticon.jpg in a configuration like this:

    img {
    src: url("images/calendar.gif");
    }

    <img style="vertical-align: middle;" src="images/defaulticon.gif" id="calendarContainer_Icon">

    Thanks

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    ...Why?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    img {
    src: url("images/calendar.gif");
    }
    I don't think this is valid

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    It's not.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I know

    img {
    src: url("images/calendar.gif");
    }

    does not work. That's what I am asking! How could you override the default image in CSS for an existing IMG tag? Is there another attribute than "src" I can use to specify the URL of the image in CSS?

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    But why?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I think using CSS you can only specify background image not the SRC of an <IMG>. If you want to change the SRC of an IMG then i think you can do it using JavaScript.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's one way:

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    
    .rollimage1 {
    display:block;
    width:70px;
    height:113px;
    background-image:url('image1.gif');
    }
    .rollimage1:hover {
    background-image:url('over1.gif');
    }
    
    </style>
    </head>
    <body>
    <a class="rollimage1" href="javascript:void(0);" onclick="return false;"></a>
    </body>
    </html>
    Notes: If you want the rollover to be linked, you can forget about the javascript:void(0); and remove the onclick event (the two of which together degrade gracefully in most non-javascript enabled browsers) and just give the href attribute the URL for the link. Because IE does not apply the hover pseudo class except to anchor links, this syntax (or one much like it) pretty much must be used. Browsers require display:block; to render properly. If you want the images to appear to be inline, floating, positioning or the use of a table would be required. The width and height should be that of the images. Two images of the same dimensions should be used but, with a little creativity that can be worked around.

    This sort of effect can be more conveniently accomplished with active javascript but, that leaves out non-javascript enabled browsers.
    - John
    ________________________

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

  9. #9
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Why?

    I have an HTML/JS page that I don't want to modify each time a customer asks for a different look (meaning also the path and name for the IMG src) => therefore the existence of external CSS stylesheets! The problem is that <img src='defaultIcon'> is not easily customized with an external stylesheet. All the solutions involving another element and its background don't work because you need to know the size of the image to have a snug enclosing element! The IMG tag, although simple, does actually get the size of the image and sizes the containing box accordingly!

    I thought I missed something in the CSS2 spec and there was a way to specify the image src path in a stylesheet...

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Nope, you cannot get that sort of configurability and functionality with css alone. If you use javascript, you could configure your images (as well as have all or most of the script code) in an external javascript file.
    - 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
  •