简体   繁体   English

'.col-md-6'内'.row'的整个一半的CSS背景

[英]CSS background for entire half of '.row' inside '.col-md-6'

I am trying to set a background for half of a row as shown in the image 我正在尝试将背景设置为一半的行,如图所示 剖面图 with the grey representing the background image. 灰色代表背景图片。

But when I set the background of the second .col-md-6, It only sets the background for that and leaves out the padding. 但是,当我设置第二个.col-md-6的背景时,它仅为此设置背景,而忽略了填充。 I understand that is how it is meant to be. 我了解这就是它的本意。 But I was hoping for suggestions on how to go about this while achieving the layout I want ( shown in the image). 但是我希望在实现所需布局的同时就此进行建议(如图所示)。 I am using Bootstrap 4. 我正在使用Bootstrap 4。

So far my code structure is: 到目前为止,我的代码结构是:

<section>
 <div class="container">
  <div class="row"> 
    <!-- LEFT SECTION -->
    <div class="col-md-6">
      <div class="container">
         TITLE AND CONTENT LEFT
      </div>
    </div>

    <!-- RIGHT SECTION -->
    <div id="right" class="col-md-6">
      <div class="container">
         TITLE AND CONTENT LEFT
      </div>
    </div>

  </div>
 </div>
</section>

My CSS is: 我的CSS是:

#right{
 background-size:cover;
 background-image: imageUrl;
}

//For overlay over image
#right:before{
 content:"";
 position: absolute;
 left:0; right: 0; 
 top: 0; bottom: 0;
 background: rgba(195,68,86,0.2);
}

I assume you are using Bootstrap. 我假设您正在使用Bootstrap。 The .row class has margins related to its parent. .row类具有与其父级相关的边距。 And since the .col-md-6 is inside the .row element, it has to respect its boundaries (thats how HTML and CSS works), except if you make the position of the .col-md-6 class "absolute", but that's not the default behavior with out-of-the-box bootstrap. 由于.col-md-6位于.row元素内部,因此它必须遵守其边界(即HTML和CSS的工作方式),除非您将.col-md-6类的位置设置为“绝对”,但这不是现成的引导程序的默认行为。

You could consider using .row-fluid, it changes the margins, but if you want to keep that padding, you will need to make some custom css to make the padding inside the .col-md-6 classes. 您可以考虑使用.row-fluid,它会改变边距,但是如果您想保留该填充,则需要制作一些自定义css以在.col-md-6类中进行填充。

And remember: margin > border > padding 并记住:边距>边框>填充

The background is applied only inside the borders. 背景仅在边框内应用。

Use class container-fluid and following css. 使用类container-fluid和以下CSS。

<div class="container-fluid">

CSS 的CSS

.row {
  border: 5px solid black;
}

.col-6 {
  padding: 0 0 0 30px;
}

.row div.col-6:last-child {
  background: #aaa;
  padding: 0 30px 0 0;
}

.container {
  padding: 0 30px 0 0;
  border: 1px solid grey;
}

Sample: https://jsfiddle.net/aq9Laaew/47448/ 范例: https//jsfiddle.net/aq9Laaew/47448/

The easiesst way: 最简单的方法:

 section{position: relative;min-height: 200px;} section:before{content:"";position: absolute;left:50%; right: 0; top: 0; bottom: 0;background: rgba(195,68,86,0.2);} 
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/> <section> <div class="container"> <div class="row"> <!-- LEFT SECTION --> <div class="col-sm-6"> LEFT </div> <!-- RIGHT SECTION --> <div class="col-sm-6"> RIGHT </div> </div> </div> </section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM