html - CSS, HTML : inside bootstrap div element, 填充背景

标签 html css twitter-bootstrap

我正在尝试填充 bootstrap jumbotron 的背景

我的代码是

.jumbotron {
    padding: 5px;
    margin-bottom: 5px;
    font-size: 10px;
    font-weight: 200;
    line-height: 2.1428571435;
}

.jumbotron  div#bg:after {
    content: "";
    background: url("image.jpg");
    opacity: 0.2;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    position: absolute;
    z-index: -1;   
}

以及我要填充的元素

<div class="jumbotron">
  <div id="bg"></div>
  <p><a class="btn btn-lg btn-success" href="http://github.com" target="blank">GitHub</a></p>
</div>

但是这段代码填充了整个页面,而我想要的只是填充选定的元素。

我应该如何使用 CSS 做到这一点?

最佳答案

你需要在 .jumbotron 中添加 position:relative 来限制绝对定位的元素在它的边界内,你也不需要空的 div,你可以在 .jumbotron 本身上使用 :after 伪元素:

 .jumbotron {
     position: relative;
     background: none;
     padding: 5px;
     margin-bottom: 5px;
     font-size: 10px;
     font-weight: 200;
     line-height: 2.1428571435;
 }
 .jumbotron:after {
     content:"";
     background: url("image.jpg");
     opacity: 0.2;
     top: 0;
     left: 0;
     bottom: 0;
     right: 0;
     position: absolute;
     z-index: -1;
 }

Demo fiddle

关于html - CSS, HTML : inside bootstrap div element, 填充背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910142/

相关文章:

html - Paypal 按钮中的 Paypal 电子邮件地址

html - 均匀分布的 HTML 元素

html - 防止图像在没有 float 的情况下中断到下一行

javascript - 如何使输入标签中的字母在类型上增长并在退格键上收缩

javascript - 使用旋转效果更改跨度内的文本

jquery - 如何在幻灯片结束时停止 Bootstrap 3 Carousel 循环

html - 使用::before 创建围绕圆的线

html - 如何判断一个网站是HTTP还是HTTPS?

jquery - 如何在翻转父级时保持文本原样?

jquery - Bootstrap下拉菜单悬停时滑动动画