简体   繁体   English

HTML div超出了父容器的最大高度

[英]HTML div growing beyond max-height of parent container

I know there are tons of questions asking the same thing but none of the solutions I came across works for the scenario that I set up. 我知道有很多问题要问同样的事情,但是我遇到的所有解决方案都无法解决我设置的情况。

I have a simple structure that has one main outer div, and a inner div. 我有一个简单的结构,具有一个主要的外部div和一个内部div。 Within the inner div I separated into two of 10% and 90% height respectively. 在内部div中,我分别分为10%和90%高度的两个。 The second separation seems to grow beyond the restrictions set by max-height and I can't seem to figure out why. 第二个分隔似乎超出了max-height设置的限制,我似乎无法弄清楚为什么。

EDIT I need for the scroll bar to be present in the second separation (.content) and not on the inner div (.main-body) 编辑我需要滚动条出现在第二个分隔(.content)中,而不是在内部div(.main-body)中

 .outer-layer { position: fixed; width: 100%; height: 100%; background: rgba(0, 0, 0, .6); z-index: 15000; top: 0; left: 0; padding: 10px; } .main-body { position: fixed; top: 18%; left: 18%; background: #fff; width: 60%; min-height: 30%; max-height: 60%; padding: 10px; } .header { height: 10%; max-height: 10%; background: red; } .content { max-height: 90%; width: calc(100% - 20px); height: 90%; background: blue; padding: 10px; overflow: auto; } 
 <div class="outer-layer"> <div class="main-body"> <div class="header"> <h2>Hello</h2> <!--header--> </div> <div class="content"> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <!--content--> </div> <!--body--> </div> <!--overlay--> </div> 

I am currently testing in Chrome 我目前正在Chrome浏览器中进行测试

I included this Fiddle 我包括了这个小提琴

Is this JSFiddle what you're aiming for? 您想要的是JSFiddle吗?

You had a few slight hiccups in your original version that I've tidied up and outlined below... 您在原始版本中有一些小毛病,我整理过并在下面概述...

1. You cannot use a percentage-based height on an element whose parent does not have a set height. 1.您不能在其父级没有设置高度的元素上使用基于百分比的高度。 Because you are not giving .main-body a set height, its children will not respect percentage-based heights. 因为您没有给.main-body设定高度,所以它的子代将不会遵守基于百分比的高度。 This is resolved by giving .main-body a height. 这可以通过给.main-body一个高度来解决。

2. Your <h2> has large vertical margins, and so its height is expanding past the height of its parent. 2.您的<h2>具有很大的垂直边距,因此它的高度正在扩展超过其父代的高度。 Remove these by doing h2 {margin: 0;} . 通过执行h2 {margin: 0;}删除它们。

3. Remember that padding is additive ! 3.请记住,填充是可加的 10px padding on both the top and bottom of an element will add 20px to its height. 元素顶部和底部的10px填充会将其高度增加20px。 You can use box-sizing: border-box; 您可以使用box-sizing: border-box; on your elements to avoid this - it forces the element to apply their padding inwards instead of outwards , thereby not messing with it's height/width. 在元素上避免这种情况-强制元素向内而不是向外应用其填充,从而不会弄乱其高度/宽度。

The page needs to render the information regardless. 该页面无论如何都需要呈现信息。 Assuming that the inner content is larger than the outer, using overflow creates a scroll bar for you. 假设内部内容大于外部内容,则使用overflow为您创建一个滚动条。

.main-body {
  overflow-y: scroll;
}

http://www.w3schools.com/cssref/pr_pos_overflow.asp http://www.w3schools.com/cssref/pr_pos_overflow.asp


Post edit Updates: 发布编辑更新:

.header {
  position:fixed;
  width: 60%;
}

.content {
  margin-top: 10%; // To match the header's height
}

The styling will be off, but you can adjust it to fit your project better. 样式将关闭,但您可以对其进行调整以更好地适合您的项目。

JSfiddle: https://jsfiddle.net/nu5LkL6b/9/ JSfiddle: https ://jsfiddle.net/nu5LkL6b/9/

As discussed: You cannot use a percentage-based height on an element whose parent does not have a set height. 如前所述:您不能在其父级没有设置高度的元素上使用基于百分比的高度。 Because you are not giving .main-body a set height, its children will not respect percentage-based heights. 因为您没有给.main设定高度,所以它的子代将不会遵守基于百分比的高度。 This is resolved by giving .main-body a height. 这可以通过给.main-body一个高度来解决。

Little Enhc: 小恩克:

.content {
  max-height: 90%;
  width: calc(100% - 20px);
  height: calc(90% - 20px);
  background: blue;
  padding: 10px;
  overflow: auto;
}

CSS: CSS:

.outer-layer {
  position: fixed;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .6);
  z-index: 15000;
  top: 0;
  left: 0;
  padding: 10px;
}
.main-body {
  position: fixed;
  top: 18%;
  left: 18%;
  background: #fff;
  width: 60%;
  min-height: 30%;
  height: 60%;
  padding: 10px;
}
.header {
  height: 10%;
  max-height: 10%;
  background: red;
}
.content {
  max-height: 90%;
  width: calc(100% - 20px);
  height: calc(90% - 20px);
  background: blue;
  padding: 10px;
  overflow: auto;
}
h2 {
  margin-top: 0;
}

Your h2 has "native" margin which is added to the main-body height and breaks the percentage. 您的h2具有“本机”边距,该边距被添加到main-body高度并破坏了百分比。 Remove this margin. 删除此边距。 Then your main-body has padding that add extra pixels too. 然后,您main-body也会添加额外的像素填充。 Remove it, and 90% + 10% will fit 100% . 删除它,则90% + 10%将适合100%

 h2 { margin: 0; } .outer-layer { position: fixed; width: 100%; height: 100%; background: rgba(0, 0, 0, .6); z-index: 15000; top: 0; left: 0; padding: 10px; } .main-body { position: fixed; top: 18%; left: 18%; background: #fff; width: 60%; min-height: 30%; max-height: 60%; } .header { height: 10%; max-height: 10%; background: red; } .content { max-height: 90%; width: calc(100% - 20px); height: 90%; background: blue; padding: 10px; overflow: auto; } 
 <div class="outer-layer"> <div class="main-body"> <div class="header"> <h2>Hello</h2> <!--header--> </div> <div class="content"> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <p>dynamic content</p> <!--content--> </div> <!--body--> </div> <!--overlay--> </div> 

you need to add height to the parent element ie .main-body 您需要为父元素(即.main-body增加高度

 .main-body{
        height:72%;    /* added */
        padding:0px;  /* editied  */
    }
    .header{
      height:20%;   /* edited*/
      max-height:20%; /* edited but actually not needed to have max height */
    }

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

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