简体   繁体   English

设置高度<div> = 到另一个的高度<div>通过 .css

[英]Set height of <div> = to height of another <div> through .css

I have two <div> elements on this site .我在这个站点上有两个<div>元素。 It's a site I'm developing, and is a work in progress, but some of the design choices I'd like to finalize tonight.这是我正在开发的网站,并且正在进行中,但我想在今晚完成一些设计选择。 Right now my simplified .css is thus:现在我的简化 .css 是这样的:

#leftdiv {
    /*this is the navigation pane*/
    min-height: 600px;
    max-height: 600px;
}
#rightdiv {
    /*this is the primary pane*/
    min-height: 600px;
    max-height: 600px;
    overflow-y: auto;
}

I've set a hard min- and max-heights for both so they keep the same height, and if content overflows out of the #rightdiv , a scrollbar appears.我为两者设置了最小和最大高度,以便它们保持相同的高度,如果内容溢出#rightdiv ,则会出现滚动条。 I'd like this scrollbar to be gone and having the #rightdiv and #leftdiv stretch to fit the contents of the #rightdiv .我想这个滚动条不见了,并具有#rightdiv#leftdiv拉伸以适应内容#rightdiv You can see in the page I linked that there's some content that overflows the boundaries of the #rightdiv , and the scrollbar.您可以在我链接的页面中看到,有些内容超出了#rightdiv和滚动条的边界。 I want the whole site to stretch height-wise to fit the contents, but if I remove the overflow-y: auto;我希望整个站点在高度上拉伸以适应内容,但是如果我删除了overflow-y: auto; from my .css and remove the max-heights, the #rightdiv stretches, but the #leftdiv doesn't, yielding some truly ugly design.从我的 .css 中删除最大高度, #rightdiv拉伸,但#leftdiv没有,产生一些真正丑陋的设计。

I'd like something like the below:我想要像下面这样的东西:

#leftdiv {
    min-height: equal to #rightdiv height if #rightdiv is taller, else 600px;
}
#rightdiv {
    min-height: equal to #leftdiv height if #leftdiv is taller, else 600px;
}

How would I go about setting the min-height of both like this?我将如何像这样设置两者的最小高度?

If you don't care for IE6 and IE7 users, simply use display: table-cell for your divs:如果您不关心 IE6 和 IE7 用户,只需为您的 div 使用display: table-cell

demo演示

Note the use of wrapper with display: table .注意使用带有display: table的包装器。

For IE6/IE7 users - if you have them - you'll probably need to fallback to Javascript.对于 IE6/IE7 用户——如果你有他们——你可能需要回退到 Javascript。

I am assuming that you have used height attribute at both so i am comparing it with a height left do it with JavaScript.我假设您在两者都使用了高度属性,所以我将它与左侧的高度进行比较,用 JavaScript 来做。

var right=document.getElementById('rightdiv').style.height;
var left=document.getElementById('leftdiv').style.height;
if(left>right)
{
    document.getElementById('rightdiv').style.height=left;
}
else
{
    document.getElementById('leftdiv').style.height=right;
}

Another idea can be found here HTML/CSS: Making two floating divs the same height .另一个想法可以在这里找到HTML/CSS: Making two floating divs the same height

If you're open to using javascript then you can get the property on an element like this: document.GetElementByID('rightdiv').style.getPropertyValue('max-height');如果您愿意使用 javascript,那么您可以在这样的元素上获取属性: document.GetElementByID('rightdiv').style.getPropertyValue('max-height');

And you can set the attribute on an element like this: .setAttribute('style','max-height:'+heightVariable+';');您可以像这样在元素上设置属性: .setAttribute('style','max-height:'+heightVariable+';');

Note: if you're simply looking to set both element's max-height property in one line, you can do so like this:注意:如果您只是想在一行中设置两个元素的max-height属性,您可以这样做:

#leftdiv,#rightdiv
{
    min-height: 600px;   
}

You would certainly benefit from using a responsive framework for your project.您肯定会从为您的项目使用响应式框架中受益。 It would save you a good amount of headaches.它会为你节省大量的头痛。 However, seeing the structure of your HTML I would do the following:但是,查看 HTML 的结构后,我会执行以下操作:

Please check the example: http://jsfiddle.net/xLA4q/请检查示例: http : //jsfiddle.net/xLA4q/

HTML: HTML:

<div class="nav-content-wrapper">
 <div class="left-nav">asdasdasd ads asd ads asd ad asdasd ad ad a ad</div>
 <div class="content">asd as dad ads ads ads ad ads das ad sad</div>
</div>

CSS: CSS:

.nav-content-wrapper{position:relative; overflow:auto; display:block;height:300px;}
.left-nav{float:left;width:30%;height:inherit;}
.content{float:left;width:70%;height:inherit;}

It seems like what you're looking for is a variant on the CSS Holy Grail Layout , but in two columns.看起来您正在寻找的是CSS Holy Grail Layout上的变体,但分为两列。 Check out the resources at this answer for more information.查看此答案中的资源以获取更多信息。

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

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