Define an &#

2019-01-08 09:49发布

This question already has an answer here:

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

7条回答
Luminary・发光体
2楼-- · 2019-01-08 10:09

After trying these solutions I still wasn't satisfied but I found a solution in this article and it works in Chrome, Firefox, Opera, Safari, IE8+

#divId {

  display: block;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  background: url(http://notrealdomain2.com/newbanner.png) no-repeat;
  width: 180px; /* Width of new image */
  height: 236px; /* Height of new image */
  padding-left: 180px; /* Equal to width of new image */

}
查看更多
老娘就宠你
3楼-- · 2019-01-08 10:09

They are right. IMG is a content element and CSS is about design. But, how about when you use some content elements or properties for design purposes? I have IMG across my web pages that must change if i change the style (the CSS).

Well this is a solution for defining IMG presentation (no really the image) in CSS style.
1: create a 1x1 transparent gif or png.
2: Assign propery "src" of IMG to that image.
3: Define final presentation with "background-image" in the CSS style.

It works like a charm :)

查看更多
趁早两清
4楼-- · 2019-01-08 10:10

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;
}
查看更多
何必那么认真
5楼-- · 2019-01-08 10:16
#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;
}
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-01-08 10:19

just this as img tag is a content element

img {
content:url(http://example.com/image.png);
}
查看更多
Deceive 欺骗
7楼-- · 2019-01-08 10:22

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

查看更多
登录 后发表回答