使 child 可滚动,同时保持 parents 填充

Make child scrollable while maintaining parents padding

我想让 child div (.content) 在超过其 parents 高度时可以滚动。我遇到的问题是维护它的 parents padding(.box padding),这样 .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;
}

就用这个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/

尝试为您的内容区域框添加高度,而不是为您的父框分配一个高度,这样可以保持您想要的填充。

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

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

已更新JSFiddle