【问题标题】:Making layers in CSS在 CSS 中创建图层
【发布时间】:2015-01-22 01:33:12
【问题描述】:

我有一个问题,我怀疑答案很简单。我正在使用 Bootstrap 制作个人网页,并尝试将背景分成 3 个相等的列(它们都有不同的图像)。

我知道这可以用 class="col-xs-4" 来完成,但问题是我想保持背景的原样(它是一个响应式的“col-lg-12” )。

有没有办法分割我的背景(再次,将图像上传到 3 个面板中,这些面板基本上会掩盖整个图像),并且仍然在顶部保留所有“col-lg-12”标题内容?

感谢您提供的任何帮助,我当前的 html 代码是这样的:

<header>
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <img class="img-responsive" src="img/picture.png" alt="">
                    <div class="intro-text">
                        <span class="intohead">Filler Text</span>
                        <span class="subhead">More detailed, longer filler text for below</span>
                    </div>
                </div>
            </div>
        </div>
    </header>

【问题讨论】:

  • 我真的不明白您希望最终页面是什么样子。你只希望&lt;div class="col-lg-12"&gt;...&lt;/div&gt; 里面有 3 张图片?
  • 不,我希望它有 3 列的背景,我可以将图像放入其中(例如我将图像设置为背景的 div)

标签: html css twitter-bootstrap-3 multiple-columns layer


【解决方案1】:

基本上,有三列带有背景图像,然后在三列顶部放置一个封面div。你可以把任何你喜欢的东西放在封面div。这是一篇关于CSS positioning的文章。

.wrap {
  width: 100%;
  height: 300px;
  position:relative;
}
.section {
  float: left;
  height: 300px;
  width: 33.33333%;
}
.one {
  background: url(http://placehold.it/200x300/ccc/666/&text=img+1) no-repeat;
  background-size: 100%;
}
.two {
  background: url(http://placehold.it/200x300/666/ccc/&text=img+2) no-repeat;
  background-size: 100%;
}
.three {
  background: url(http://placehold.it/200x300/ccc/666/&text=img+3) no-repeat;
  background-size: 100%;
}
.cover {
  width: 100%;
  height: 100%;
  /*A background isn't needed, it's just to show that the element is there*/
  background: salmon;
  opacity: .5;
  /* this stuff is key */
  position: absolute;
  top: 0;
  left: 0;
  /* place cover on top */
  z-index: 10;
}
<div class="wrap">
  <div class="cover">Put all you content in here</div>
  <div class="section one"></div>
  <div class="section two"></div>
  <div class="section three"></div>
</div>

运行代码 sn-p 并告诉我会发生什么。这是你要找的吗?

【讨论】:

  • 是的,这正是我想要的背景,但我可以使用引导程序的 3 个“col-xs-4”来做到这一点(正如我在问题中提到的,我正在使用引导程序)。问题是将“col-lg-12” div 放在上面,它有自己的响应图像、文本等。我​​不能让它们在同一行中彼此重叠。
  • 好的,所以要将div 放在另一个上面,请使用封面上的position: absolute; top: 0; left: 0; 将其直接放在顶部。请参阅我的更新答案。
  • 是的,这正是我需要做的,非常感谢您的帮助!
  • 嘿 773 我实际上仍有问题。再次感谢您的帮助。标题本身具有背景颜色,其他所有内容都浮动在它上面。所以我把 3 个部分的 div 放在 header 标记之后,并将“cover”类添加到容器 div 中。但是,仅添加绝对值(即使没有左)会导致所有内容在之前都响应居中时向左移动。
  • 嘿773,我通过添加“left:0;right:0;margin-left:auto;margin-right:auto;”让它工作了。除了你所拥有的之外,还可以使用 CSS 进行覆盖。感谢您的帮助!
猜你喜欢
  • 2019-12-05
  • 2015-11-11
  • 2018-09-06
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
相关资源
最近更新 更多