【问题标题】:How can I cover a div completely with an image?如何用图像完全覆盖 div?
【发布时间】:2014-07-17 04:03:21
【问题描述】:

所以我有一个 div,里面有内容,但我想用一个位于其他所有内容之上的图像完全覆盖该内容。

如果有帮助,我正在使用引导程序

<div class="jumbotron">
  <div> I have images and paragraphs inside of me </div>
  <div> I have images and paragraphs inside of me </div>
  <div class="clearfix"></div>
</div>

我尝试为 jumbotron 设置背景图像,但这会将图像放在其他所有内容的后面,而不是在顶部。我尝试了其他一些方法,但一切都搞乱了应该存在于覆盖图像下方的现有布局。

【问题讨论】:

  • 喜欢上述 div 的样式 background-image,还是您想隐藏 DIV 的内容?
  • 请问您为什么想要那里的内容但不可见?
  • 如果您希望图像位于顶部,则应使用带有 alt 文本的 img Background is a BACK ground

标签: html css image twitter-bootstrap


【解决方案1】:

You could position the image absolutely to cover the text.

.jumbotron{
    position: relative;
}
.jumbotron img{
    position: absolute;
}

You could also use a background image in this situation as well if your text is way too large. This is kind of goofy but the requirements here are kind of goofy:

.jumbotron {
    position: relative;
    height: 100%;
    width: 100%;
}
.jumbotron img {
    position: absolute;
}
.jumbotron .covering{
    position: absolute;
    height: 100%;
    width: 100%;
    background-image: url('http://lorempixel.com/400/200/');
    background-size: cover;
}

【讨论】:

  • 如果他的文字和图片比图片大怎么办? jsfiddle.net/j08691/Gyk33
  • 我会反其道而行之,所以图像是相对的,文本是绝对的——这样 div 将占据图像的空间而不是文本
  • 您可以使用带有背景的 div 来代替图像。父母和孩子都必须使用 width / height: 100%; jsfiddle.net/MathiasaurusRex/Gyk33/2
【解决方案2】:

您可以为图像和文本设置相同的 CSS 定位和大小规则,并使图像的 z-index 高于文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多