两列自动高度背景颜色

时间:2017-12-05 20:33:19

标签: html css3 responsive-design html-table background-color

我可能在这里遗漏了一些非常简单的东西。我有两个保持相同高度的列,具体取决于哪个div内部有更多内容,但背景颜色不会在较短的列上延伸;使用媒体查询显示较小时,列将更改为垂直布局。 如何让左列的背景颜色延伸到div的底部?

#pageContainer {
        width: 100%;
        text-align: center;
    }
    
    #pageContainer p {
        line-height: 10px;
    }
    
    .sectionContainer {
        width: 80%;
        margin: 0 auto;
        display: table;
    }
    
    .leftColumn {
        display: table-cell;
        float: left;
        width: 50%;
        background-color: #4F4F4F;
        color: #FFFFFF;
    }
    
    .rightColumn {
        display: table-cell;
        float: left;
        width: 50%;
        background-color: #EEEEEE;
        color: #000000;
        
    }
    
    @media all and (max-width: 768px) {    
        .sectionContainer {
            width: 100%;
        }
        
        .leftColumn {
            width: 100%;
        }
        
        .rightColumn {
            width: 100%;
        }
    }
<div id="pageContainer">
  <div class="sectionContainer">
    <div class="leftColumn">
      <h1>Left Column</h1>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
    </div>
    <div class="rightColumn">
      <h1>Right Column</h1>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
    </div>
  </div>
</div>

我已添加CodePen来显示问题:https://codepen.io/Macast/pen/ZaZrNz

谢谢!

3 个答案:

答案 0 :(得分:1)

height:100%sectionContainer类添加leftColumn似乎可以解决此问题。

&#13;
&#13;
#pageContainer {
        width: 100%;
        text-align: center;
    }
    
    #pageContainer p {
        line-height: 10px;
    }
    
    .sectionContainer {
        width: 80%;
        margin: 0 auto;
        display: table;
        height:100%;
    }
    
    .leftColumn {
        display: table-cell;
        float: left;
        width: 50%;
        background-color: #4F4F4F;
        color: #FFFFFF;
        height:100%;
    }
    
    .rightColumn {
        display: table-cell;
        float: left;
        width: 50%;
        background-color: #EEEEEE;
        color: #000000;
        
    }
    
    @media all and (max-width: 768px) {    
        .sectionContainer {
            width: 100%;
        }
        
        .leftColumn {
            width: 100%;
        }
        
        .rightColumn {
            width: 100%;
        }
    }
&#13;
<div id="pageContainer">
  <div class="sectionContainer">
    <div class="leftColumn">
      <h1>Left Column</h1>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
    </div>
    <div class="rightColumn">
      <h1>Right Column</h1>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

擦除浮子(在表格中没有意义)来修复高度问题。然后在媒体查询中,将容器的display更改为block,将两个内部元素更改为inline-block

#pageContainer {
  width: 100%;
  text-align: center;
}

#pageContainer p {
  line-height: 10px;
}

.sectionContainer {
  width: 80%;
  margin: 0 auto;
  display: table;
}

.leftColumn {
  display: table-cell;
  width: 50%;
  background-color: #4F4F4F;
  color: #FFFFFF;
}

.rightColumn {
  display: table-cell;
  width: 50%;
  background-color: #EEEEEE;
  color: #000000;
}

@media all and (max-width: 768px) {
  .sectionContainer {
    display: block;
    width: 100%;
  }
  .leftColumn {
    display: inline-block;
    width: 100%;
  }
  .rightColumn {
    display: inline-block;
    width: 100%;
  }
}
<div id="pageContainer">
  <div class="sectionContainer">
    <div class="leftColumn">
      <h1>Left Column</h1>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
    </div>
    <div class="rightColumn">
      <h1>Right Column</h1>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
    </div>
  </div>
</div>

答案 2 :(得分:0)

您可以使用display: flex;代替display: table;float: left;。这将使孩子们的身高相等。

&#13;
&#13;
#pageContainer {
        width: 100%;
        text-align: center;
    }
    
    #pageContainer p {
        line-height: 10px;
    }
    
    .sectionContainer {
        width: 80%;
        margin: 0 auto;
        display: flex;
    }
    
    .leftColumn {
        flex-basis: 50%;
        background-color: #4F4F4F;
        color: #FFFFFF;
    }
    
    .rightColumn {
        flex-basis: 50%;
        background-color: #EEEEEE;
        color: #000000;
        
    }
    
    @media all and (max-width: 768px) {    
        .sectionContainer {
            width: 100%;
        }
        
        .leftColumn {
            width: 100%;
        }
        
        .rightColumn {
            width: 100%;
        }
    }
&#13;
<div id="pageContainer">
  <div class="sectionContainer">
    <div class="leftColumn">
      <h1>Left Column</h1>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
      <p>Left Column</p>
    </div>
    <div class="rightColumn">
      <h1>Right Column</h1>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
      <p>Right Column</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;