html - 两列自动高度背景颜色

标签 html css 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

谢谢!

最佳答案

删除 float (在表格中毫无意义)以解决高度问题。然后在媒体查询中,将容器的 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>

关于html - 两列自动高度背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662203/

相关文章:

html - 多个 ID 选择器不起作用

css - 使用 CSS 向我的工具提示添加底部小箭头

css - 响应式设计像素宽度的清晰度

css - 语义菜单 - 需要放置一个居中的标题

css - 响应式网页设计 - 使用 px 值

javascript - JS : name of last input element (HTML/PHP)

c# - 如何从 C# with\r 获取 String 到 JavaScript 变量?

javascript - 内联 block 网格系统 - CSS 不适用于动态创建的元素

javascript - 无法获取重置按钮来清除innerHTML

javascript - 根据 id 对多个 div 使用相同的函数