Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define an <img>'s src attribute in CSS [duplicate]

People also ask

Can you use src in CSS?

You can simulate having a src attribute by using the content attribute in CSS, and passing in the image path url() .

What is the src attribute of an IMG tag for?

The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image, if the image for some reason cannot be displayed.

What is CSS src?

Definition and Usage The src attribute specifies the location (URL) of the external resource.


just this as img tag is a content element

img {
    content:url(http://example.com/image.png);
}

#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;
}

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


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


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;
}