使子可滚动,同时保持父级填充

时间:2016-02-01 21:40:10

标签: html css parent-child padding

我希望将子div( .content )设为可滚动,如果它超出其父级高度。我遇到的问题是维护其父级填充( .box 填充),因此 .content 的文本不会放在底部边缘。

.box 高度未固定为250px,仅用于演示目的。

Fiddle

HTML

<div class="box">
 <h3>Title</h3>
 <div class="content">
  <p>
   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. 

    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 
  </p>
 </div>
</div>

CSS

.box {
  width: 250px;
  height: 250px; //height is not fixed, just for demonstration purposes.
  padding: 25px;      
  overflow: hidden;

  background-color: rgb(200,200,200);
}

.content {
  overflow: scroll;
}

2 个答案:

答案 0 :(得分:0)

只需使用此CSS:

.box {
  display:flex;
  flex-flow: column;
  width: 250px;
  height: 250px;
  padding: 25px;
  overflow: hidden;
  background-color: rgb(200,200,200);
}

.content {
  flex:2;
  height:100%;
  width:100%;
  overflow-y: scroll;//only for y axis
}

的jsfiddle: https://jsfiddle.net/08ea4ehm/4/

答案 1 :(得分:0)

尝试为内容区域框添加高度,而不是为父框指定高度,以保持所需的填充。

.box {
  width: 250px;
  padding: 25px;
  background-color: rgb(200,200,200);
}

.content {
  height: 250px;
  overflow: auto; 
}

更新了JSFiddle