简体   繁体   English

高度为100%时将div设置为高度的100%

[英]Setting divs to 100% height when height: 100% doesn't work

I've seen a lot of examples here that claim just using... 我在这里看到了很多例子,声称只是使用...

height: 100%;

... should set the height of a div to the height of its container. ...应将div的高度设置为其容器的高度。 That is not happening, so I realise there must be some other problem; 那没有发生,所以我意识到肯定还有其他问题。 I'd like to know what that problem is. 我想知道那是什么问题。

My aim is to have the nested image displayed at the same height of the .container div, ideally stretching to fit height-wise and width-wise. 我的目标是使嵌套图像显示在.container div的相同高度,理想情况下可以拉伸以适合高度方向和宽度方向。

At the moment, at some resolutions, the image does not occupy the full height of the .container div which means the p elements can overspill. 目前,在某些分辨率下,图像未占据.container div的整个高度,这意味着p个元素可能会溢出。

And I realise it might look like I should just set a background image for the .container div (something I've already tried) but, due to the way our AngularJS engine is set up, the url value for background-image gets broken so that is not an option. 而且我意识到它看起来应该只为.container div设置背景图像(我已经尝试过了),但是由于设置AngularJS引擎的方式,背景图像的url值被破坏了,所以那不是一个选择。

 #container { background-color: transparent; border-radius: 0px 0px 0px 0px; font-size: 0.8em; font-family: Courier; padding: 0px; overflow: hidden; margin: 40px 0px; height: 100%; width: 100%; border: solid 4px green; } #container > div:first-child { display: block; border: solid 2px red; height: 100%; } #container > div:first-child > div { display: block; height: 100%; } #container > div:first-child > div > img { position: absolute; z-index: 1; margin: 0px; } #container p { margin: 20px 40px 20px 40px; position: relative; z-index: 2; } 
 <div id="container"> <div> <div> <img src="path/to/image.png"/> </div> </div> <div> <div> <p>Correctly displaying text here.</p> </div> </div> <div> <div> <p>More correctly displaying text here.</p> </div> </div> </div> 

I have since tried various things, including setting min-height or max-height to 100% on all the divs (which also did not work) 此后,我尝试了各种操作,包括在所有div上将min-height或max-height设置为100%(这也无效)

I have added the border colours to establish that the height of the div is, indeed NOT 100% of the height of its container. 我添加了边框颜色来确定div的高度实际上不是其容器高度的100%。 When I set it to an explicit pixel height, it displays at that height - but I want it to be responsive, so 100% height would work nicely if it would work. 当我将其设置为明确的像素高度时,它会以该高度显示-但我希望它具有响应性,因此,如果100%的高度可以工作,则效果很好。 I've also tried changing the display properties of the various divs, but 'inline' or 'inline-block' just made the border collapse to the left side with what looked like a 'line-height's height. 我也尝试过更改各种div的显示属性,但是“内嵌”或“内嵌块”只是使边框向左侧折叠,看起来像是“行高”的高度。

Any pointers as to what's going on here? 有什么关于这里发生的事情的指针吗? Surely setting something to actually be 100% the height of its container should be simple? 确定将东西实际设置为其容器高度的100%应该很简单吗? Thanks. 谢谢。

EDIT I should have pointed out that I (think I) need the image to be positioned absolutely so that the text elements appear over the image. 编辑我应该指出,我(认为我)需要将图像绝对定位,以便文本元素出现图像上。 This is all an elaborate alternative to not being able to simply use background-image, as detailed above. 如上所述,这是无法简单使用背景图像的一种精心设计的替代方案。 Thanks again. 再次感谢。

What i see about your coding is : 我对您的编码所看到的是:

1 - container should have position relative if you want to have the first div with the red border with full height that can be overlapping the container you have to make the first div position absolute and i provided here jsfiddle with an exmaple of your case the the img inside the div should take height 100%; and width 100%; 1-容器应该具有相对位置,如果您想让第一个div的红色边框具有可以与容器重叠的 ,则必须使第一个div位置为绝对值 ,我在这里为jsfiddle提供了案例的示例div内的imgheight 100%; and width 100%;height 100%; and width 100%; height 100%; and width 100%; from the parent and you can use object-fit: cover; 从父母那里,您可以使用object-fit: cover; to have it same height and width respecting the ratio of your container 具有相同的高度和宽度,并尊重容器的比例

https://jsfiddle.net/Letwug7t/ https://jsfiddle.net/Letwug7t/

 #container { background-color: transparent; border-radius: 0px 0px 0px 0px; font-size: 0.8em; font-family: Courier; padding: 0px; /* overflow: hidden; */ margin: 40px 0px; height: 100%; width: 100%; border: solid 4px green; position: relative; } #container > div:first-child { display: block; border: solid 2px red; height: 100%; width: 100%; position: absolute; } #container > div:first-child > div { display: block; height: 100%; } #container > div:first-child > div > img { position: absolute; z-index: 1; margin: 0px; height: 100%; width: 100%; object-fit: cover; } #container p { margin: 20px 40px 20px 40px; position: relative; z-index: 2; } 
 <div id="container"> <div> <div> <img src="http://placehold.it/300x300"/> </div> </div> <div> <div> <p>Correctly displaying text here.</p> </div> </div> <div> <div> <p>More correctly displaying text here.</p> </div> </div> </div> 

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

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