简体   繁体   English

为什么我不能使绝对div高度100%

[英]Why i can't make absolute div height 100%

I have problem, I have one fixed container, inside them I have absolute div, I want to set .absolute div height:100%; 我有问题,我有一个固定的容器,里面我有绝对的div,我想设置.absolute div height:100%; to be full height of container div(500px). 是容器div的全高(500px)。 Here is what I tried to solve my problem, this need because I want to create mobile menu with toggle container, and its important for me to be height 100% of mobile phone screen. 这是我试图解决我的问题,这需要因为我想用切换容器创建移动菜单,对我来说重要的是100%的手机屏幕高度。

https://jsfiddle.net/d1bh9ncs/ https://jsfiddle.net/d1bh9ncs/

HTML HTML

<div class="container">
  <div class="fixed">
    <div class="absolute">

    </div>
  </div>
</div>

CSS CSS

.container{
  width:100%;
  height:500px;
  background-color:#ddd;
}
.fixed{
  position:fixed; 
  width:100%;
  height:50px;
  background-color:red;
  top:8px;
  left:8px;
  right:15px;
}
.absolute{
  position:absolute;
  height:100%;
  width:100%;
  background-color:green;
  top:51px;
  left:0px;
}

The parent div .fixed is absolutely positioned and has a height 50px. 父div .fixed绝对定位,高度为50px。 So applying height: 100% on it's child will inherit the relative height(ie 50px). 所以应用height: 100%就可以继承它的相对高度(即50px)。

Use height: 100vh; 使用height: 100vh; on .absolute . .absolute I have used calculated height height: calc(100vh - 51px) to avoid scrollbar due to top: 51px . 我已经使用计算出的高度height: calc(100vh - 51px)以避免由于top: 51px滚动条top: 51px

Note : vh is 1/100th of the height of the viewport(visible webpage height). 注意vh是视口高度的1/100(可见网页高度)。

Updated Fiddle 更新小提琴

.absolute {
  position: absolute;
  height: calc(100vh - 51px);
  width: 100%;
  background-color: green;
  top: 51px;
  left: 0px;
}

you are using Fixed div as an Parent div of Absolute div, Absolute div can have 100% of Fixed div it can't extend to its parent's height if you add height value in Percentage.If you want it to extend as parent height you must have to add height in px (pixels) 你使用固定div作为绝对div的父div,绝对div可以有100%的固定div如果你在Percentage中添加高度值它不能扩展到它的父高度。如果你想要它作为父高度扩展你必须必须以px(像素)为单位添加高度

.container{
  width:100%;
  height:500px;
  background-color:#ddd;
}
.fixed{
  position:fixed;
  width:100%;
  height: 101px;
  background-color:red;
  top:8px;
  left:8px;
  right:15px;
}
.absolute{
  position:absolute;
  height: 117px;
  width:100%;
  background-color:green;
  top: 0px !important;
  left:0px;
  z-index: 99999999;
  top: 50px;
}

Try to give height in vh. 尝试在vh中给出高度。 Put .absolute height = 100vh 把。 .absolute height = 100vh

 .absolute
 {
 position:absolute;
 height:100vh;
 width:100%;
 background-color:green;
 top:51px;
 left:0px;
}

https://jsfiddle.net/bj9wcdLs/ https://jsfiddle.net/bj9wcdLs/

A more modern way of doing this is to use vh and vw (view height and width). 更现代的方法是使用vh和vw(视图高度和宽度)。 Which rather than being a percentage of its parent (like %) is a percentage of the full page. 而不是其父项的百分比(如%)是整页的百分比。

In the example below I've done some calc's to help it work out what sizes we really want things. 在下面的例子中,我做了一些计算,以帮助它计算出我们真正想要的尺寸。

 example = function() { var abSel = document.querySelector(".absolute"); abSel.classList.toggle('hidden'); } 
 body { margin: 0; } .container { width: 100vw; height: 100vh; background-color: #ddd; } .fixed { position: fixed; width: calc(100vw - 16px); height: 50px; line-height: 50px; text-align: center; background-color: red; top: 8px; left: 8px; } .absolute { position: absolute; border-top: 1px solid #ddd; height: calc(100vh - 59px); width: calc(100vw - 16px); background-color: green; top: 50px; } .hidden { display: none; } 
 <div class="container"> <div class="fixed"> <button onclick="example()">Example</button> <div class="absolute hidden"></div> </div> </div> 

Hope this helps. 希望这可以帮助。

Just like sticksu said. 就像sticku说的那样。

Change your code 改变你的代码

.fixed{
   position:fixed; 
   width:100%;
   height:100%; //must be 100%
   background-color:red;
   top:8px;
   left:8px;
   right:15px;
}

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

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