2015-06-18 20:05
浏览 188


Having the source of an image as a dataurl, is there a way to get the image height and width either in javascript or PHP so that I can set these as properties of a div?

or is there a better way to make the div scale to the image size. Consider that the image is used as background-image property in the CSS of the div.

图片转代码服务由CSDN问答提供 功能建议

将图像的来源作为dataurl,是否有办法在javascript中获取图像的高度和宽度 或PHP,以便我可以将它们设置为div的属性?

还是有更好的方法使div缩放到图像大小。 考虑将图像用作div的CSS中的 background-image 属性。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doukuiqian5345 2015-06-18 20:40

    If you know the aspect ratio of the image then you can create a responsive <div> which 'mimics' the native <img> element as the page resizes.

    For example if you have an image that is 300x180 - then the aspect ratio is

    1 : 0.6

    (180/300=0.6) This means if the image is 100% wide, then it is 60% high.

    .image {
      background-image: url(;
      background-repeat: no-repeat;
      background-size: cover;
      padding-bottom: 60%;

    See this jsFiddle

    解决 无用
    打赏 举报

相关推荐 更多相似问题