How to give a child div width/height 100% if the parent's div is also 100% width/height

核能气质少年 提交于 2020-01-04 02:43:17

问题


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');
}

回答1:


You use viewport relative values and give the element a min-height of 100vh. (example)

(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');
}

5.1.2. Viewport-percentage lengths: the ‘vw’, ‘vh’, ‘vmin’, ‘vmax’ units

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.

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%;
}


来源:https://stackoverflow.com/questions/26207568/how-to-give-a-child-div-width-height-100-if-the-parents-div-is-also-100-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!