<div id="header"></div>
<div id="main">
<div id="navigation"></div>
<div id="content"></div>
</div>
<div id="footer"></div>
导航栏在左侧,内容区域在右侧。内容区域的信息通过 PHP 动态获取,因此每次加载页面时都不同。
我该如何垂直缩放导航栏,以使其高度与内容区域的高度相同,无论加载哪个页面?
对于家长:
display: flex;
你应该添加一些前缀, http://css-tricks.com/using-flexbox/.
正如@Adam Garner所指出的,align-items: stretch;是不必要的。它的使用也是针对父元素而非子元素。如果你想定义子元素的伸展,可以使用align-self。
.parent {
background: red;
padding: 10px;
display:flex;
}
.other-child {
width: 100px;
background: yellow;
height: 150px;
padding: .5rem;
}
.child {
width: 100px;
background: blue;
}
<div class="parent">
<div class="other-child">
Only used for stretching the parent
</div>
<div class="child"></div>
</div>
align-items: center
,那么需要拉伸的项目应该使用align-self: normal
。 - Dominic注意: 本解答适用于不支持Flexbox标准的旧版浏览器。对于现代方法,请参见: https://dev59.com/G3NA5IYBdhLWcg3wBpBm#23300532
我建议你查看使用跨浏览器CSS和无需Hack实现等高列。
基本上,以兼容浏览器的方式使用CSS来做这件事不是很容易(但在表格中很简单),所以最好找到一个合适的预打包解决方案。
此外,答案取决于您需要100%高度还是等高。通常是等高的。如果要求100%高度,则答案略有不同。
container2
上加上边框border:1px solid blue
之后,问题就出现了:蓝色边框会在前两列显示,而不是只在第二列显示,这并不符合我们的预期。 - Julien Kroneggdisplay:table
。 - Siler这是一个设计师经常遇到的令人沮丧的问题。窍门在于您需要在CSS中将BODY和HTML的高度设置为100%。
html,body {
height:100%;
}
这段看似没用的代码是为了向浏览器定义100%的含义。虽然有些令人沮丧,但这是最简单的方法。
html { height: 100%; } body { height: 98%; }
。 - Chris Middleton我发现将两个列设置为 display: table-cell;
而不是 float: left;
可以很好地解决问题。
display: table-cell
等解决布局问题的解决方案已经被flexbox取代。 - csvan如果你不介意在内容div出现意外缩短的情况下导航div被裁剪,那么至少有一种简单的方法:
#main {
position: relative;
}
#main #navigation {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 10em; /* or whatever */
}
#main #content {
margin: 0;
margin-left: 10em; /* or whatever width you set for #navigation */
}
否则,还可以使用伪列技术。
#main {
overflow: hidden;
}
#navigation, #content {
margin-bottom: -1000px;
padding-bottom: 1000px;
}
使用jQuery:
$(function() {
function unifyHeights() {
var maxHeight = 0;
$('#container').children('#navigation, #content').each(function() {
var height = $(this).outerHeight();
// alert(height);
if ( height > maxHeight ) {
maxHeight = height;
}
});
$('#navigation, #content').css('height', maxHeight);
}
unifyHeights();
});
Math.max()
函数。 - alexcss
函数时,您无法像使用纯CSS解决方案时那样区分屏幕和打印媒体。因此,您可能会遇到打印问题。 - Julien Kronegg#main {
display: table;
}
#navigation, #content {
display: table-cell;
}
请看这个示例。
height: 100%
,但事实证明它并不需要。 - jcubicheight : <百分比>
只有在所有父节点都指定了像素、ems等固定高度的情况下才能生效。这些高度值应该在顶层设置,以便高度可以级联到您的元素。
您可以像 @Travis 所述那样将 html 和 body 元素指定为 100%,以使页面高度级联到您的节点。
经过长时间的搜索和尝试,除了以下方法外,没有解决我的问题:
style = "height:100%;"
在子元素上应用以下样式:
对于父元素,请应用此样式:
.parent {
display: flex;
flex-direction: column;
}
此外,我正在使用Bootstrap,这并没有破坏我的响应式设计。
flex-direction: column;
才能让它对我起作用。 - Richard Hedges
display: flex; align-items: stretch;
,但不要对 div#content 使用height: 100%
。 - Ihor