简体   繁体   English

使用可变高度的周围元素最大化div的高度

[英]Maximising the height of a div with surrounding elements of variable height

I've a div that I'd like to maximise the size of, within a parent that's based on a 100vh. 我有一个div,我希望最大化,在父母的基础上100vh。

The problem being that I have two p divs that can also change their height based on the width of the window, leading to a varying size. 问题是我有两个p div,它们也可以根据窗口的宽度改变它们的高度,从而导致不同的大小。

Now the quick and dirty solution might simply be to run a jQuery snippet to detect the sizes of the parent div and p divs and set the div's height based on these, running the function on doc.ready and on window.onresize . 现在快速而肮脏的解决方案可能只是运行一个jQuery片段来检测父divp div的大小,并根据这些设置div的高度,在doc.readywindow.onresize上运行该函数。

But that feels unnecessary, and I'm wondering if there's a neater solution using only CSS that I'm unaware of? 但这感觉不必要,我想知道是否有一个更简洁的解决方案只使用我不知道的CSS?

I've set a simplified, but non-working version of the problem below to see if you can get a clearer idea than what I'm referring to. 我已经在下面设置了一个简化但不起作用的问题版本,看看你是否能比我所指的更清楚。

Any tips would be great, thanks! 任何提示都会很棒,谢谢!

 *{ margin: 0; padding: 0; font-family: Arial, san-serif; } #parent{ height: 100vh; background: lightblue; } p{ font-size: 40px; background: yellow; } #p1{ top: 0; } #p2{ bottom: 0; position: absolute; } div{ background-color: red; height: 100%; } 
 <div id="parent"> <p id="p1">Long block that only appears on the top</p> <div>Maximising Div</div> <p id="p2">Long block that only appears on the bottom</p> </div> 

You can achieve what you want with flexbox . 你可以用flexbox实现你想要的。

Since there are a lot of resources about how to use flexbox , it will not be included in the scope of this answer. 由于有很多关于如何使用flexbox的资源,因此它不会包含在本答案的范围内。 However, I'll explain the basics so that you can understand what is going on here. 但是,我将解释基础知识,以便您了解这里发生了什么。

  1. You define a container, in this case #parent with the property display: flex; 您可以定义一个容器,在本例中为#parent ,其属性为display: flex; , this makes it a flex-container. ,这使它成为一个灵活的容器。
  2. This container's child elements are now flex-items, in your case the <p> elements and the <div> in the middle. 此容器的子元素现在是flex-items,在您的情况下是<p>元素,中间是<div>
  3. We then let the middle <div> grow to fill the available space of the container with the shorthand property flex: 1; 然后我们让中间的<div>增长,用速记属性flex: 1;填充容器的可用空间flex: 1; .

Code Snippet: 代码片段:

 * { margin: 0; padding: 0; font-family: Arial, san-serif; } #parent { height: 100vh; background: lightblue; display: flex; flex-direction: column; } p { font-size: 40px; background: yellow; } div > div { background-color: red; flex: 1; } 
 <div id="parent"> <p id="p1">Long block that only appears on the top</p> <div>Maximising Div</div> <p id="p2">Long block that only appears on the bottom</p> </div> 


Notes: 笔记:

  • Check Michael's answer for current browser support. 查看Michael对当前浏览器支持的回答

 *{ margin: 0; padding: 0; font-family: Arial, san-serif; } #parent{ height: 100vh; display: flex; /* establish flex container */ flex-direction: column; /* stack child elements ("flex items") vertically */ background: lightblue; } p{ font-size: 40px; background: yellow; } #p1 { /* can be variable height */} #p2 { /* can be variable height */ } div { background-color: red; flex: 1; /* consume free space in container; will force p elements to top and bottom */ } 
 <div id="parent"> <p id="p1">Long block that only appears on the top</p> <div>Maximising Div</div> <p id="p2">Long block that only appears on the bottom</p> </div> 

Browser support: Flexbox is supported by all major browsers, except IE < 10 . 浏览器支持: 所有主流浏览 器都支持 Flexbox, IE <10除外 Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes . 最近的一些浏览器版本,例如Safari 8和IE10,需要供应商前缀 For a quick way to add prefixes use Autoprefixer . 要快速添加前缀,请使用Autoprefixer More details in this answer . 这个答案的更多细节。

Adding width: 100% to the <p> elements and setting body { overflow: hidden } to your current code provides the functionality you want if you can't use flexbox. 添加width: 100%<p>元素,并将body { overflow: hidden }为当前代码,如果不能使用flexbox,则可以提供所需的功能。

 body{ overflow: hidden; } *{ margin: 0; padding: 0; font-family: Arial, san-serif; } #parent{ height: 100vh; background: lightblue; } p{ font-size: 40px; background: yellow; width: 100%; } #p1{ top: 0; } #p2{ bottom: 0; position: absolute; } div{ background-color: red; height: 100%; } 
 <div id="parent"> <p id="p1">Long block that only appears on the top</p> <div class="maximising">Maximising Div</div> <p id="p2">Long block that only appears on the bottom</p> </div> 

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

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