html - 使用 HTML <img> 标签作为背景图片而不是 CSS background-image 属性?

标签 html css image

我需要使用 html <img>标记为 <div> 的背景图片.然后我将放置一个 <p>关于这一点的内容。我已经试过了,但似乎无法让它们正确显示。我使用了相对位置,负值的边距。

如有任何建议或指出正确的方向,我们将不胜感激。

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

最佳答案

为了几乎完美地复制用于背景图像的功能,我们必须考虑 img 的功能,包括:

  • 它没有任何指针事件。
  • 它至少呈现在 div 下一层。

结果会是这样的:

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

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

p {
  padding: 25px;
}
<div>
  <img src="http://via.placeholder.com/720x480/ddd/007" />
  <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>

您可以根据需要修改宽度和高度。

关于html - 使用 HTML <img> 标签作为背景图片而不是 CSS background-image 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31267425/

相关文章:

javascript - jQuery 滚动到 div onclick 不起作用

javascript - 显示总数HTML <span> 中的行

javascript - 保持日期选择器粘在文本字段上

android - 如何正确居中/ float 这些图像?

javascript - 三.js图像质量模糊

javascript - jQuery动态更新div内容,按滚动位置跳转

javascript - HTML5 Canvas - 按类在所有 Canvas 元素中绘制相同的内容

html - 如果容器 div 较小,则按比例缩小 float div

javascript - 简单的 JS 代码错误

java - 如何从 Eclipse 项目中的 res 文件夹中获取文件?