2015-10-23 62 views
0

如何在下面的图像中产生效果?Bootstrap3网格 - 2种背景颜色

我想拥有一个有2列的容器。较小的列应该在黄色背景上,而较大的列应该在白色上。黄色应该从左侧开始,在col-xs-4的右侧结束。

Example

+0

给予类 –

+0

那么背景将是只在此列,而是应该从左侧开始身体。 Conrtiner不像你看到的那样流畅! – user3573535

+0

如果您使用的是具有两列的容器类,则意味着从左侧没有空间。请探讨这个问题。 –

回答

1

是,使用一个伪元素。

注意这里的一般布局方法并不重要(我用Flexbox进行练习),但伪元素技术是常见的。在你的CSS像.container .COL-XS-4 {背景色:黄}

* { 
 
    -webkit-box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    box-sizing: border-box; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.container { 
 
    width: 50%; 
 
    height: 300px; 
 
    margin: auto; 
 
    border: 1px solid grey; 
 
    display: flex; 
 
} 
 
.container div { 
 
    border: 1px solid green; 
 
} 
 
.left { 
 
    width: 25%; 
 
} 
 
.right { 
 
    width: 75%; 
 
} 
 
/* MAGIC */ 
 

 
body { 
 
    overflow: hidden; 
 
    /* prevent scrollbars */ 
 
} 
 
.left { 
 
    position: relative; 
 
    /* positioning context */ 
 
} 
 
.left:after { 
 
    content: ''; 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
    width: 50vw; 
 
    /* half viewport width */ 
 
    height: 100%; 
 
    /* parent height */ 
 
    background: orange; 
 
    z-index: -1; 
 
    /* push under parent */ 
 
}
<div class="container"> 
 
    <div class="left"></div> 
 
    <div class="right"></div> 
 
</div>

+0

很棒!这种方法对我来说很好。谢谢。 – user3573535