简体   繁体   English

将子元素设为父元素最大高度的100%高度

[英]Make child element 100% height of parent max-height

I'm working on a modal layout and struggling to pass height context from a parent element to its child element. 我正在研究模式布局,并努力将高度上下文从父元素传递到其子元素。 Take a look at this CodePen , .child-child needs to be the same height as .child . 看一下此CodePen.child-child需要与.child相同的高度。

One solution to this is changing max-height: 50%; 一种解决方案是将max-height: 50%;更改max-height: 50%; to height: 50%; height: 50%; ; ; this isn't usable because sometimes the modal won't contain that much content and should only be as high as the content it contains, then when there's a lot of content it shouldn't be able to exceed it's max-height . 这是不可用的,因为有时模态将不会包含那么多内容,而应仅与它所包含的内容一样高,那么当有很多内容时,它就不能超过其max-height

Another solution is to go for flexbox and setting .child as a flex container which will set .child-child full height which flex containers do by default; 另一种解决方案是去实现Flexbox和设置.child作为Flex容器将设置.child-child全高度柔性容器默认情况下做到; you can see that in action in this CodePen . 您可以在此CodePen中看到这一点 This actually works great in most browsers and does exactly what I need, but completely fails in IE10/11+; 实际上,这在大多数浏览器中都能很好地工作,并且完全满足我的需要,但是在IE10 / 11 +中完全失败; note that I'm using autoprefixer in the CodePen settings so I doubt this'll be a flexbox syntax issue. 请注意,我在CodePen设置中使用了autoprefixer,因此我怀疑这将是flexbox语法问题。 Maybe I need to add some other flex properties for IE such as flex-grow / flex-shrink etc. I've tried a bunch of different combinations with not much luck, I also can't seem to find this specific bug with Google either. 也许我需要为IE添加其他一些flex属性,例如flex-grow / flex-shrink等。我尝试了很多运气不佳的组合,但我似乎也找不到与Google相关的特定错误。

Any help would be greatly appreciated! 任何帮助将不胜感激!

I think that the only possibility solution is making .child scrollale with overflow-y:scroll;. 我认为唯一可能的解决方案是使.child scrollale与overflow-y:scroll;。

My Demo. 我的演示

.parent {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: red;
}

.child {
 background: blue;
 max-height: 50%;
 max-width: 600px;
 width: 100%;
 position: absolute;
  top: 50%;
 left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
overflow-y:scroll;
}

.child-child {
 background: green;
  height: 100%;
 overflow: auto;
}
 <div class="parent">
      <div class="child">
     <div class="child-child">
      <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
       <p>Blah.</p>
     </div>
   </div>
 </div>

Edit If you want to put the button you can BUT you must set position:relative to .child and position:fixed to the button; 编辑如果要放置按钮,则可以,但是必须将position:相对于.child设置为position:fixed到按钮;

Working DEMO. 工作演示。

Edit If you want to position the buttom on right you must use right:calc((100% - 600px)/2);. 编辑如果要在右侧放置按钮,则必须使用right:calc((100%-600px)/ 2);。

Working DEMO. 工作演示。

PS see calc() browser support there. PS在那里看到calc()浏览器支持。

I think that the only possibility solution is making .child scrollale with overflow-y:scroll; 我认为唯一可能的解决方案是使.child scrollale与.child overflow-y:scroll; .

My Demo . 我的演示

 .parent { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: red; } .child { background: blue; max-height: 50%; max-width: 600px; width: 100%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; overflow-y:scroll; } .child-child { background: green; height: 100%; overflow: auto; } 
 <div class="parent"> <div class="child"> <div class="child-child"> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> </div> </div> </div> 

PS If you want to keep the padding you have to apply overflow-y:scroll; PS:如果要保留填充,则必须应用overflow-y:scroll; to a child container. 到一个儿童容器。

Edit If you want to put the button you can BUT you must set position:relative to .child and position:fixed to the button; 编辑如果要放置按钮,则可以,但是必须将position:relative对于.child设置为position:fixed到按钮;

Working DEMO . 工作演示

Edit If you want to position the buttom on right you must use right:calc((100% - 600px)/2); 编辑如果要在右侧放置按钮,则必须使用right:calc((100% - 600px)/2); .

Working DEMO . 工作演示

PS see calc() browser support there . PS见calc()浏览器支持那里

Is this what you're looking for? 这是您要找的东西吗?

 $(document).ready(function() { $('.plus-btn').on('click', function() { $(this).closest('.parent').toggleClass('active'); }); }); 
 *{ box-sizing: border-box; } .parent { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: red; } .top-right { position: absolute; top: 0; right: 0; } .plus-btn { overflow: hidden; position: relative; cursor: pointer; width: 20px; height: 20px; display: inline-block; background: #f0f0f0; } .plus-btn span { position: absolute; margin: 45% 10%; width: 80%; height: 10%; background: #222; border-radius: 4px; pointer-events: inherit; -webkit-transition: -webkit-transform .3s; transition: transform .3s; } .plus-btn span:first-child { -webkit-transform: rotate(0deg); transform: rotate(0deg); } .plus-btn span:last-child { -webkit-transform: rotate(90deg); transform: rotate(90deg); } .parent.active .plus-btn span:first-child { -webkit-transform: rotate(-45deg); transform: rotate(-45deg); } .parent.active .plus-btn span:last-child { -webkit-transform: rotate(45deg); transform: rotate(45deg); } .child { background: blue; max-height: 50vh; max-width: 600px; width: 100%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; } .child-child { background: green; overflow-y: auto; width: 100%; height: 100%; max-height: 0; -webkit-transition: max-height .3s; transition: max-height .3s; } .parent.active .child-child { max-height: calc(50vh - 40px); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <div class="child"> <div class="top-right"> <div class="plus-btn"> <span></span> <span></span> </div> </div> <div class="child-child"> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p> <p>Blah.</p><p>Blah.</p> <p>Blah.</p> <p>Blah.</p> </div> </div> </div> 

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

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