简体   繁体   English

DIV 100%父级宽度和高度

[英]DIV 100% width and height of parent

I have this markup: 我有这个标记:

<div>
     <div class="inner"></div
     <img src="blabla" />
</div>

How to make .inner 100% width and 100% height of its parent div dimensions? 如何使.inner的父div尺寸的宽度为100%,高度为100%? Parent div's dimensions rely on img dimensions. 父div的尺寸取决于img尺寸。

Is it possible to do this with CSS? CSS可以做到这一点吗? I don't want JS solution as it's obvious. 很明显,我不希望使用JS解决方案。 Thanks 谢谢

http://jsfiddle.net/7cSzV/ http://jsfiddle.net/7cSzV/

See this fiddle : http://jsfiddle.net/vSE7S/2/ 看到这个小提琴: http : //jsfiddle.net/vSE7S/2/

CSS CSS

.outer { position: relative; }
.inner {
    position: absolute;
    top: 0; left: 0;
    background: black;
    height: 100%;
    width: 100%;
}
img { display: block }

(I just gave an .outer class to the outer div, for the sake of the simplicity) (为简单起见,我只是给外部div提供了.outer类)

note: try also to assign display: inline-block to the outer div: the resulting effect is slightly different, see http://jsfiddle.net/vSE7S/3/ — just choose the effect that fits best to your needs. 注意:还可以尝试将display: inline-block分配给外部div:产生的效果略有不同,请参见http://jsfiddle.net/vSE7S/3/-只需选择最适合您需要的效果即可。

You might change the css and add a class to that parent div 您可能会更改CSS并将类添加到该父div

.inner {
    background: black;
    /* position: absolute; */
    width: 100%;
    height: 100%;
    /*width: 100px;
    height: 100px;*/
}
div.parentDiv{
    position: relative;
}

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

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