简体   繁体   English

如何动态设置div高度值?

[英]How to set div height value dynamically?

I have a HTML page and include 3 div like this: Link 我有一个HTML页面,并包含3 div,如下所示: 链接

<div style="min-height:100%;">
        <div id="TopContent" style="display: inline-block; height: 100px; width: 100%; background-color: #CCDEBB;">
        </div>

        <div id="LeftContent" style="display: inline-block; width: 25%; border: 3px solid #DFE8F6;">

                </div>             

        <div id="RightContent" style="border: 3px solid #b8cfee; display: inline-block; float: right; width: 72%;">


        </div>

            </div>

I want to set height value of LeftContent and RightContent according to page height value. 我想根据页面高度值设置LeftContent和RightContent的高度值。 I mean LeftContent and RightContent height extend end of the page. 我的意思是LeftContent和RightContent的高度延伸到页面的末端。

Use calc(); 使用calc();

#LeftContent{
float:left;
margin-right:3px;
height:calc(100% - 100px); //-100px because height of top bar is 100px

}

same for Right Content 正确的内容相同

You can't control height with this HTML, but you can emulate this effect with :before/:after elements: 您无法使用此HTML控制高度,但可以使用:before /:after元素来模拟这种效果:

#LeftContent:after,
#RightContent:after{
    content:'';
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:9999px;
    z-index:-1;
    background:#DFE8F6;
}
#RightContent:after{background:#b8cfee;}

http://jsfiddle.net/EhnhZ/ http://jsfiddle.net/EhnhZ/

I suggest you to use classes instead of id. 我建议您使用类而不是id。

HTML HTML

<div class="content top"></div>
<div class="content left"></div>
<div class="content right"></div>

CSS CSS

*{
  height:100%;
  width:100%;
  margin:0;
} /* instead of HTML and BODY attributes */

.content{
  display:inline-block;
  margin-right:-4px; /*to margin left div with the right one!*/
  margin-top:-4px; /*to margin left and right divs with the top one!*/
}

.left{
  height:calc(100% - 200px); /*use calc(100% - "top div height" */
  width:50%; /*choose your own width*/
  background-color:red;
}

.right {
  height: calc(100% - 200px); /*use calc(100% - "top div height" */
  width:50%; /*choose your own width*/
  background-color:green;
}

.top{
  width:100%;
  height:200px;
  background-color:blue;
}

Hope this can help you! 希望这可以帮到你! ( see more on http://codepen.io/utopy/pen/zcyti ) (有关更多信息,请参见http://codepen.io/utopy/pen/zcyti

Like this demo ? 喜欢这个演示吗?

just add float: left; 只需添加float: left; to LeftContent 到LeftContent

I built my answer using the answer from this question 我用这个问题的答案建立了答案

<style>
    html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}
</style>

<div style="min-height:100%;height:100%;">
    <div id="TopContent" style="height: 100px; width: 100%; background-color: #CCDEBB;">
    </div>

    <div id="LeftContent" style="min-height:100%;width: 25%; border: 3px solid #DFE8F6;float:left">    
    </div>             

    <div id="RightContent" style="min-height:100%;border: 3px solid #b8cfee; width:72%;float:right">
    </div>
</div>

At current your solution will not work, this is because of how web page documents render. 目前,您的解决方案将无法正常工作,这是因为网页documents呈现方式。

At the moment from your HTML your current document is the following height : 从HTML开始,当前文档的高度如下:

100px (from TopContent)

+ 3px (from LeftContent)

+ 3px (from RightContent)

Because the document does not need to be larger than 106px (based on the content) then the document element will not render as more than that. 由于document不必大于106px (基于内容),因此document元素将不会呈现大于该像素的值。

What you can do to trick this is by setting the body/html CSS attributes to have a height of 100%. 您可以通过设置body / html CSS属性的高度为100%来达到目的。

Use the following CSS to do this : 使用以下CSS可以做到这一点:

body,html {
    height:100%;
}

Also you will need to set the height of your container div to be as 另外,您需要将容器div的高度设置为

<div style="height:100%;">

Here is an example JSFiddle for your problem. 这是解决您的问题的示例JSFiddle

You should use jquery,in this event set the height of divs 您应该使用jquery,在这种情况下设置div的高度

$(window).resize(function () {
 $('#LeftContent').css('height',$(window).height());
 $('#RightContent').css('height',$(window).height());
        });

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

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