Use the HTML <img> tag as a background image instead of the CSS background-image property?

China☆狼群 提交于 2021-02-04 22:18:10

问题


I need to use the html <img> tag as a background image for a <div>. I will then be placing a <p> of content over this. I've tried this but cant seem to get them to display correctly. I've used position relative, margin with negative values.

Any suggestions or point me in the right direction would be appreciated.

<div>

  <img src="http://www.shiaupload.ir/images/77810787432153420049.png" />

  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book
  </p>

</div>

回答1:


To make an almost perfectly duplicate of the features used for background-image, we've to consider the features of the img include:

  • It doesn't have any pointer events.
  • It's rendered at least one layer below the div.

The result would be, something like this jsfiddle:

div {
    display: inline-block;
    overflow: hidden;
    position: relative;
    width: 100%;
}

img {
    pointer-events: none;
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: -1;
}

You might modify the width and height to your needs.



来源:https://stackoverflow.com/questions/31267425/use-the-html-img-tag-as-a-background-image-instead-of-the-css-background-image

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