html - 如何让左侧栏的高度与右侧栏的高度相等?

标签 html css

在我的网页上,我希望我的侧边栏都向下延伸到页面底部,即使主要内容区域变长了。然而,事实并非如此。

#main {
}
#left {
    background-color: grey;
    height: 100%;
}
#right {
    background-color: yellow;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div id="main" class="row">
    <div id="left" class="col-sm-6 col-xs-6">
        <div id="sidebar">
            <p>Item 1</p>
            <p>Item 2</p>
            <p>Item 3</p>
            <p>Item 4</p>
            <p>Item 5</p>
        </div>
    </div>
    <div id="right" class="col-sm-6 col-xs-6">
        <div id="somethingLong">
            <div id="bigItem">
                 <h2> My big Car </h2>
            </div>
            <div id="bigItem">
                 <h2> My big house </h2>
            </div>
            <div id="bigItem">
                 <h2> My big Paycheck </h2>
            </div>
             <div id="bigItem">
                 <h2> My big mom </h2>
            </div>
        </div>
    </div>
</div>

如何让 left 至少与 right 一样长,或者最多到达页面底部?

最佳答案

如我的评论所述,要使列的高度相同,您可以使用 display: table;flex

这是一个使用 display: table 的演示。

#main {
  display: table;
}
#left {
  display: table-cell;
  background-color: grey;
}
#right {
  display: table-cell;
  background-color: yellow;
}
<div id="main" class="row">
  <div id="left" class="col-sm-6 col-xs-6">
    <div id="sidebar">
      <p>Item 1</p>
      <p>Item 2</p>
      <p>Item 3</p>
      <p>Item 4</p>
      <p>Item 5</p>
    </div>
  </div>
  <div id="right" class="col-sm-6 col-xs-6">
    <div id="somethingLong">
      <div id="bigItem">
        <h2> My big Car </h2>

      </div>
      <div id="bigItem">
        <h2> My big house </h2>

      </div>
      <div id="bigItem">
        <h2> My big Paycheck </h2>

      </div>
      <div id="bigItem">
        <h2> My big mom </h2>

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

注意: 这不是使用 Bootstrap 。


或者让列使用 while 页面使用 height: 100%;

这是一个演示:

html,
body {
  height: 100%;
}
#main {
  height: 100%
}
#left {
  background-color: grey;
  height: 100%;
}
#right {
  background-color: yellow;
  height: 100%
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<div id="main" class="row">
  <div id="left" class="col-sm-6 col-xs-6">
    <div id="sidebar">
      <p>Item 1</p>
      <p>Item 2</p>
      <p>Item 3</p>
      <p>Item 4</p>
      <p>Item 5</p>
    </div>
  </div>
  <div id="right" class="col-sm-6 col-xs-6">
    <div id="somethingLong">
      <div id="bigItem">
        <h2> My big Car </h2>

      </div>
      <div id="bigItem">
        <h2> My big house </h2>

      </div>
      <div id="bigItem">
        <h2> My big Paycheck </h2>

      </div>
      <div id="bigItem">
        <h2> My big mom </h2>

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

注意: 由于高度为窗口大小的 100%,内容溢出。

关于html - 如何让左侧栏的高度与右侧栏的高度相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29208607/

相关文章:

html - 我如何使文本 block 在左边,图像列在右边

html - 阻止用户放大和缩小网页

html - 两个div之间的分隔线

html - SVG 在 DOM 中使用时不加载外部 CSS

javascript - jQuery 根据数据值进行选择

javascript - 如何显示您点击的当前元素的图像?

html - 我怎样才能让 <ul> 在它所在的内容区域中均匀分布?

html - CSS3 动画太快,背景图像有点晃动

html - CSS定位

html - 如何更好地对齐内嵌段落元素(没有表格)