我无法使div高度100%&在CSS中可滚动

时间:2016-10-21 10:42:53

标签: javascript jquery html css css3

这是我的fiddle

我正在尝试制作100%高度的div,并且当该特定div中有更多内容时它应该是可滚动的。

我正在使用flex属性,但我的代码无法正常工作 - 当内容超过div高度时,它不会滚动。

body {
  font-family: 'arial';
}
header,
footer {
  background: #333;
  height: 30px;
  color: #fff;
}
img {
  width: 100%;
}
.wrapper {
  bottom: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
  top: 0;
  width: 100%;
  background: #ccc;
}
.content {
  height: 100%;
  width: 100%;
  -webkit-box-flex: 2;
  /* OLD - iOS 6-, Safari 3.1-6 */
  -moz-box-flex: 2;
  /* OLD - Firefox 19- */
  -webkit-flex: 2;
  /* Chrome */
  -ms-flex: 2;
  /* IE 10 */
  flex: 2;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
}
.content .inner_content {
  display: table;
  width: 100%;
  height: 100%;
}
.content .inner_content .left_content {
  display: table-cell;
  background: yellow;
  width: 50%;
  padding: 10px;
}
.content .inner_content .right_content {
  display: table-cell;
  background: green;
  width: 60%;
  padding: 10px;
}
.scrollable-content {
  background: #ddd;
  padding: 10px;
}
<div class="wrapper">
  <header>this is header</header>
  <div class="content">
    <div class="inner_content">
      <div class="left_content">
        <div class="image">
          <img src="http://l7.alamy.com/zooms/19704c162b4446c984a50bfdb49b45ac/a-colour-image-taken-on-a-cloudy-dawn-of-the-town-of-saint-malo-from-g1k27a.jpg" alt="">
        </div>
        <div class="scrollable-content">
          <h1>I am trying to do:</h1>
          <h3>1) make these gray div height 100% till bottom</h3>
          <h3>2) When content is more then this gray div should be scrollable or else not</h3>
          <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          </p>
        </div>
      </div>
      <div class="right_content">
        <img src="http://l7.alamy.com/zooms/19704c162b4446c984a50bfdb49b45ac/a-colour-image-taken-on-a-cloudy-dawn-of-the-town-of-saint-malo-from-g1k27a.jpg" alt="">
      </div>
    </div>
  </div>
  <footer>this is footer</footer>
</div>

4 个答案:

答案 0 :(得分:1)

我不确定这是否是您想要获得的效果,但请尝试添加&#39;溢出&#39;在div CSS定义中,例如:

.scrollable-content{
  background:#ddd;
  padding:10px;
  overflow: auto; max-height: 100%;
}

(我试着在小提琴中加了一个卷轴,这是你想要的卷轴吗?)

答案 1 :(得分:1)

这对你有什么用?

&#13;
&#13;
body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}

body {
  display: flex;
  max-height: 100%;
}

.wrapper {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  height: 100%;
  max-height: 100%;
  width: 100%;
  overflow: hidden;
}

header {
  background: grey
}

main {
  display: flex;
  flex-direction: row;
  flex: 1;
  background: blue
}

.column {
  width: 50%;
  padding: 10px;
  box-sizing: border-box;
  display: flex;
  flex: 1;
  flex-direction: column;
}

.column:first-child {
  background: yellow;
}

.column:last-child {
  background: green;
}

.image-holder {
  margin-bottom: 10px;
  background: orange;
  min-height:1%;
}

.image {
  width: 100%;
  display: block;
  height: auto;
}

.scrollable-content {
  flex: 1;
  background: grey;
  position: relative;
  height: 10px;
}

.firefox-scroller {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  overflow: auto;
  padding: 20px;
  box-sizing:border-box;
}
&#13;
<div class="wrapper">
  <header>header</header>
  <main>
    <div class="column">
      <div class="image-holder">
        <img src="http://l7.alamy.com/zooms/19704c162b4446c984a50bfdb49b45ac/a-colour-image-taken-on-a-cloudy-dawn-of-the-town-of-saint-malo-from-g1k27a.jpg" alt="" class="image">
      </div>
      <div class="scrollable-content">
        <div class="firefox-scroller">
          <h1>I am trying to do:</h1>
          <h3>1) make these gray div height 100% till bottom</h3>
          <h3>2) When content is more then this gray div should be scrollable or else not</h3>
          <p>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          </p>

        </div>
      </div>
    </div>
    <div class="column">
      <div class="image-holder">
        <img src="http://l7.alamy.com/zooms/19704c162b4446c984a50bfdb49b45ac/a-colour-image-taken-on-a-cloudy-dawn-of-the-town-of-saint-malo-from-g1k27a.jpg" alt="" class="image">
      </div>

    </div>
  </main>
  <footer>this is footer</footer>
</div>
&#13;
&#13;
&#13;

Here is a fiddle for you to play with

答案 2 :(得分:0)

您需要设置可滚动容器的最大高度。

 max-height: 100%;

然后你必须说你想要垂直溢出是可滚动的:

overflow-y: scroll;

答案 3 :(得分:0)

到目前为止,我正在考虑你的想法,我认为你正试图为你的div获得一个固定的高度,如果内容更多,你希望它们适合div。

您可以为div使用固定高度,也可以使用css overflow 属性来获取可滚动内容。

所以我认为您的解决方案可能如下 -

.scrollable-content{
    background:#ddd;
    padding:10px;
    height: (your desired height goes here);
    overflow:scroll;
}