views:

402

answers:

5

I need to define an <img>'s src attribute in CSS. Is there a way to specify this attribute?

+6  A: 

No there isn't. You can specify a background image but that's not the same thing.

cletus
Indeed. `<img>` represents a content image. If the image isn't content then it shouldn't be an `<img>` element. If it is content, then it should be. If the content is duplicated on multiple pages (such as being part of a menu) then the HTML for it should be too (e.g. using a template system) just like any other duplicated content.
David Dorward
+4  A: 
#divID {
background-image: url("http://imageurlhere.com");
background-repeat: no-repeat;
width: auto; //or your image's width
height: auto; //or your image's height
margin: 0;
padding: 0;
}
Ronnie Chester Lynwood
That's about as close as you can get.
zipcodeman
A: 

CSS is not used to define values to DOM element attributes, javascript would be more suitable for this.

Darin Dimitrov
+3  A: 

No. The closest you can get is setting a background image:

<div id="myimage"></div>

#myimage {
  width: 20px;
  height: 20px;
  background: white url(myimage.gif) no-repeat;
}
RoToRa
A: 

To elaborate, you can really only control (via CSS) things you'd put into an HTML element's style attribute. Since image source is controlled in the src attribute, you'd have to use a method like Ronnie's to do what you want.

Matt Ball