chrome/safari display border around image

烈酒焚心 提交于 2019-11-27 14:19:35

Now I don't know if this is a bug with Chrome or not but the grey border appears when it can't find the image, the image url is broken or as in your case the src isn't there. If you give the image a proper URL and the browser finds it then the border goes away. If the image is to not have a src then you will need to remove the height and width.

sarcastyx is right, but if you want a workarround you can set the width and height to 0 and a padding to make space for your image.

If you want a icon of 36x36, you can set width and height to 0 and pading:18px

.related_photo {
   content: '';
}

I know it is an old question. But another solution is to set the src to a 1x1 transparent pixel

<img class="related_photo"
     src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />

This works for me.

This may happen when the image is planted dynamically by css (e.g. by http://webcodertools.com/imagetobase64converter) in order to avoid extra HTTP requests. In this case we don't want to have a default image because of performance issues. I've solved it by switching from an img tag to a div tag.

img[src=""]{
    content: "";
}
img.related_photo {
  width: 80px;
  height: 60px;
  **border: solid thin #DFDFDF;** //just remove this line
  margin-right: 3px;
  float: left;
  overflow: hidden;
}

Inside img.related_photo, you need to change border: solid thin #DFDFDF; to border: 0.

Sebas Palacio Florez

I have fixed this issue with:

<img src="img/1.jpg" style="height:150px; position: absolute; right: 15px;">

The right: 15px is where you want the image to be shown, but you can place it where you want.

I just added src="trans.png", trans.png is just a 100x100 transparent background png from photoshop. Worked like a charm no borders

To summarise the answers given already: your options to remove the grey border from an img:not([src]), but still display an image using background-image in Chrome/Safari are:

  • Use a different tag that doesn't have this behaviour. (Thanks @Druvision)
    Eg: div or span.
    Sad face: it's not quite as semantic.

  • Use padding to define the dimensions. (Thanks @Gonzalo)
    Eg padding: 16px 10px 1px; replaces width:20px; height:17px;
    Sad face: dimensions and intentions aren't as obvious in the CSS, especially if it's not an even square like @Gonalo's example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!