如果父级的div也为100%的宽度/高度,如何给孩子的div的宽度/高度为100%
我希望两个div的宽度和高度为100%.我知道孩子的div不会工作,因为父母的div没有特定的身高,但是有办法解决这个问题吗?
I want two divs having the width and height 100%. I know that the child div won't work because the parents div has not a specific height but is there a way to fix this?
HTML:
<div class="container">
<div class="child-container">
</div>
</div>
CSS:
body
{
width: 100%;
height: 100;
}
.container
{
position: relative;
min-width: 100%;
min-height: 100%;
background-image: url('http://www.earthbusinessdirectory.com/web/images/london.jpg');
}
.child-container
{
position: relative;
min-width: 100%;
min-height: 100%;
background-image: url('/images/black.png');
}
您使用视口的相对值,并将元素的min-height
设置为100vh
. (示例)
You use viewport relative values and give the element a min-height
of 100vh
. (example)
(显然,这是假设您希望该元素是视口的100%
而不是父元素)
(This obviously assumes you are wanting the element to be 100%
of the viewport and not the parent element)
.child-container {
position: relative;
min-width: 100%;
min-height: 100vh;
background-image: url('/images/black.png');
}
视口百分比长度相对于初始包含块的大小.更改初始包含块的高度或宽度时,将对其进行相应缩放.
The viewport-percentage lengths are relative to the size of the initial containing block. When the height or width of the initial containing block is changed, they are scaled accordingly.
或者,您可以将html
元素的高度设置为100%
,然后将.container
元素的min-height
更改为height
. (示例)
Alternatively, you could set the height of the html
element to 100%
and change the .container
element's min-height
to a height
. (example)
html, body {
height: 100%;
}
.container {
position: relative;
min-width: 100%;
/* min-height: 100%; */
height: 100%;
}