充满背景,内容固定

时间:2018-09-25 19:49:06

标签: html css layout grid css-position

我有以下两个容器,它们的总宽度应为1400px。我的代码中的问题是,当我希望两个容器为1400px时,背景的宽度不是100%。

如何才能达到最佳效果?我希望你们能解决我的问题。

我正在使用以下代码:

.wrapper {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 50px;
}

#left-container {
  width: 50%;
  background: blue;
  float: left;
}

#right-container {
  width: 50%;
  background: green;
  float: left;
}
<div class="wrapper">

  <div id="left-container">

    <h2>Left container</h2>

  </div>

  <div id="right-container">

    <h2>Right container</h2>

  </div>

</div>

2 个答案:

答案 0 :(得分:0)

除了背景之外,您还可以使用伪元素进行着色,并且可能会有一些溢出:

.wrapper {
  max-width: 1400px;
  margin: 0 auto;
  padding:0 50px;
}

#left-container {
  width: 50%;
  background: blue;
  float: left;
  position:relative;
}
#left-container:before {
  content:"";
  position:absolute;
  left:-100vw;
  right:100%;
  top:0;
  bottom:0;
  background:inherit;
}

#right-container {
  width: 50%;
  background: green;
  float: left;
  position:relative;
}
#right-container:before {
  content:"";
  position:absolute;
  right:-100vw;
  left:100%;
  top:0;
  bottom:0;
  background:inherit;
}
body {
 overflow:hidden;
}
<div class="wrapper">

  <div id="left-container">

    <h2>Left container</h2>

  </div>

  <div id="right-container">

    <h2>Right container</h2>

  </div>

</div>

答案 1 :(得分:0)

可以通过多种方式完成。我现在想到的最简单的解决方案是添加一个容器并使用线性渐变:

body {margin:0; padding:0;}
.container {
  background: linear-gradient(to left, blue 50%, green 50%);
}
.wrapper {
  border: 1px solid white;
  max-width: 1400px;
  margin: 0 auto;
  overflow: auto;
}

#left-container,
#right-container{
  box-sizing: border-box;
  width: 50%;
  float: left;
  padding-left: 50px;
}
<div class="container">
  <div class="wrapper">
    <div id="left-container">
      <h2>Left container</h2>
    </div>
    <div id="right-container">
      <h2>Right container</h2>
    </div>
  </div>  
</div>