简体   繁体   English

防止元素溢出容器

[英]Prevent element from overflowing container

I have the following structure, where the number of names ( .name ) in .list is dynamic. 我具有以下结构,其中.list中名称( .name )的数量是动态的。 What i would like to achieve is when content (depending on n of .names) is longer than .parent 's fixed height, both .children fit inside the .parent (inherit height). 我想实现的是,当含量(取决于.names的N)长于.parent的固定高度,无论是.children适应内部.parent (继承高度)。 Lack of space would be solved with .list getting a scrollbar ( overflow:auto ). .list获取滚动条可以解决空间不足的问题( overflow:auto )。

Height inheritance works well with single child, but I am having huge problems when there are two or more. 身高继承对于单身孩子来说效果很好,但是当有两个或更多孩子时,我遇到了巨大的问题。

在此处输入图片说明

JSFIDDLE HERE JSFIDDLE这里

HTML HTML

<div id="grandparent">
  <div id="parent">
    <div id="list" class="children">

      <div class="name">john</div>
      <div class="name">mike</div>
      <div class="name">jack</div>
      <div class="name">terry</div>

    </div>

    <div id="footer" class="children">

      <div>footer</div>

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

CSS CSS

body, html {

  margin: 0;
  width: 100%;
  height: 100%;

}

div {

  box-sizing: border-box;

}

#grandparent {

  background-color:yellow;
  display:flex;
  align-items:center;
  width:100%;
  height: 100%;
  flex: 1;

}

.children, .children div {

  padding: 5px;

}

.children {

  max-height: inherit;

}

.children div {

  width: 100%;
  max-height: inherit;

}

#list {

  overflow: auto;
  padding-bottom:0;

}

#footer {

  padding-top:0;

}

.name {

  background-color: green;

}

#footer div {

  background-color: pink;

}

#parent {

  background-color: blue;
  margin: 0 auto;
  max-height: 100px;

}

PS sorry for the code mess, i was just testing out different options. PS抱歉的代码混乱,我只是测试了不同的选择。

Add this to your code: 将此添加到您的代码:

#parent {
  display: flex;
  flex-direction: column;
}

 body, html { margin: 0; width: 100%; height: 100%; } div { box-sizing: border-box; } #grandparent { background-color: yellow; display: flex; align-items: center; width: 100%; height: 100%; flex: 1; } .children, .children div { padding: 5px; } .children { max-height: inherit; } .children div { width: 100%; max-height: inherit; } #list { overflow: auto; padding-bottom: 0; } #footer { padding-top: 0; } .name { background-color: green; } #footer div { background-color: pink; } #parent { background-color: blue; margin: 0 auto; max-height: 100px; /* new */ display: flex; flex-direction: column; } 
 <div id="grandparent"> <div id="parent"> <div id="list" class="children"> <div class="name">john</div> <div class="name">mike</div> <div class="name">jack</div> <div class="name">terry</div> </div> <div id="footer" class="children"> <div>footer</div> </div> </div> </div> 

jsFiddle demo jsFiddle演示

Because flex items are set to flex-shrink: 1 by default, they will reduce their size in order to not overflow the container. 由于flex项目默认设置为flex-shrink: 1 ,因此它们将减小其大小,以免容器溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM