我有一个网站与以下结构:
<div id="header"></div>
<div id="main">
<div id="navigation"></div>
<div id="content"></div>
</div>
<div id="footer"></div>
导航栏在左边,内容div在右边。内容div的信息是通过PHP拉入的,因此每次都是不同的。
我怎样才能垂直缩放导航,使其高度与内容div的高度相同,无论哪个页面被加载?
我有一个网站与以下结构:
<div id="header"></div>
<div id="main">
<div id="navigation"></div>
<div id="content"></div>
</div>
<div id="footer"></div>
导航栏在左边,内容div在右边。内容div的信息是通过PHP拉入的,因此每次都是不同的。
我怎样才能垂直缩放导航,使其高度与内容div的高度相同,无论哪个页面被加载?
注意:此答案适用于不支持Flexbox标准的旧浏览器。有关现代方法,请参见:https://stackoverflow.com/a/23300532/1155721
我建议你看看跨浏览器CSS的等高列,没有黑客。
基本上,以浏览器兼容的方式使用CSS来实现这一点并不简单(但是对于表来说很简单),所以请找到一个适当的预打包解决方案。
同样,答案也取决于你想要100%的高度还是相同的高度。通常都是等高的。如果是100%高,答案就略有不同。
(在另一个回答中提到了Dmity的Less代码)我猜这是某种“伪代码”?
据我所知,尝试使用人造柱技术应该可以做到这一点。
http://www.alistapart.com/articles/fauxcolumns/
希望这对你有所帮助。
如果你不介意导航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 */
}
除此之外,还有假柱技术。
这是设计师一直在处理的一个令人沮丧的问题。诀窍是你需要在CSS中的BODY和HTML上设置高度为100%。
html,body {
height:100%;
}
这段看似毫无意义的代码是为了向浏览器定义100%的含义。的确令人沮丧,但这是最简单的方法。
这道题的题目和内容有点矛盾。标题说的是父div,但问题让它听起来像你想要两个兄弟div(导航和内容)是相同的高度。
你(a)希望导航和内容都是主高度的100%,还是(b)希望导航和内容是相同的高度?
我假设(b)……如果是这样的话,我不认为你将能够做到这一点,因为你目前的页面结构(至少,不是纯CSS和没有脚本)。你可能需要这样做:
<main div>
<content div>
<navigation div></div>
</div>
</div>
并将内容div设置为左侧空白,无论导航窗格的宽度是多少。这样,内容的内容就在导航栏的右边,你可以将导航栏的高度设置为内容高度的100%。
编辑:我完全是在我的头脑中做的,但你可能还需要将导航div的左侧空白设置为负值,或将它的绝对左侧设置为0,以将它推回到最左边。问题是,有很多方法可以实现这一点,但并不是所有的方法都能与所有浏览器兼容。
要做到这一点,最简单的方法就是假装。《A List Apart》多年来对此进行了广泛的报道,比如Dan Cederholm 2004年的一篇文章。
我通常是这样做的:
<div id="container" class="clearfix" style="margin:0 auto;width:950px;background:white url(SOME_REPEATING_PATTERN.png) scroll repeat-y center top;">
<div id="navigation" style="float:left;width:190px;padding-right:10px;">
<!-- Navigation -->
</div>
<div id="content" style="float:left;width:750px;">
<!-- Content -->
</div>
</div>
您可以通过将#container包装在另一个div中,将标题div嵌入为#container的兄弟,并将边缘和宽度样式移动到父容器中,轻松地在此设计中添加标题。此外,CSS应该移动到一个单独的文件,而不是保持内联等等。最后,clearfix类可以在positioniseverything上找到。
使用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();
});
基于本文中描述的方法,我创建了.Less dynamic解决方案:
Html:
<div id="container3">
<div id="container2">
<div id="container1">
<div id="col1">Column 1</div>
<div id="col2">Column 2</div>
<div id="col3">Column 3</div>
</div>
</div>
</div>
少:
/* Changes these variables to adjust your columns */
@col1Width: 60%;
@col2Width: 1%;
@padding: 0%;
/* Misc variable. Do not change */
@col3Width: 100% - @col1Width - @col2Width;
#container3 {
float: left;
width: 100%;
overflow: hidden;
background-color: red;
position: relative;
#container2 {
float: left;
width: 100%;
position: relative;
background-color: yellow;
right: @col3Width;
#container1 {
float: left;
width: 100%;
position: relative;
right: @col2Width;
background-color: green;
#col1 {
float: left;
width: @col1Width - @padding * 2;
position: relative;
left: 100% - @col1Width + @padding;
overflow: hidden;
}
.col2 {
float: left;
width: @col2Width - @padding * 2;
position: relative;
left: 100% - @col1Width + @padding + @padding * 2;
overflow: hidden;
}
#col3 {
float: left;
width: @col3Width - @padding * 2;
position: relative;
left: 100% - @col1Width + @padding + @padding * 4;
overflow: hidden;
}
}
}
}
高度:< % >将只工作,如果你所有的父节点都具有指定的百分比高度和固定的高度(像素,ems等)在顶层。这样,高度就会随你的喜好而下降。
您可以像前面提到的@Travis那样为html和body元素指定100%,以使页面高度向下级联到节点。
对于父节点:
display: flex;
您应该添加一些前缀,例如http://css-tricks.com/using-flexbox/。
正如@Adam Garner所指出的,align-items:拉伸;不需要。它的用法也适用于父母,而不是孩子。如果你想定义儿童拉伸,你使用align-self。
.parent { 背景:红色; 填充:10 px; 显示:flex; } .other-child { 宽度:100 px; 背景:黄色; 身高:150 px; 填充:.5rem; } .child { 宽度:100 px; 背景:蓝色; } < div class = "父" > < div class = "另一个孩子“> 仅用于拉伸父节点 < / div > < div class = "孩子" > < / div > < / div >
#main {
overflow: hidden;
}
#navigation, #content {
margin-bottom: -1000px;
padding-bottom: 1000px;
}
我知道这个问题已经有很长一段时间了,但我找到了一个简单的解决方法,并且认为有人可以使用它(对不起,英语不好)。是这样的:
CSS
.main, .sidebar {
float: none;
padding: 20px;
vertical-align: top;
}
.container {
display: table;
}
.main {
width: 400px;
background-color: LightSlateGrey;
display: table-cell;
}
.sidebar {
width: 200px;
display: table-cell;
background-color: Tomato;
}
HTML
<div class="container clearfix">
<div class="sidebar">
simple text here
</div>
<div class="main">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam congue, tortor in mattis mattis, arcu erat pharetra orci, at vestibulum lorem ante a felis. Integer sit amet est ac elit vulputate lobortis. Vestibulum in ipsum nulla. Aenean erat elit, lacinia sit amet adipiscing quis, aliquet at erat. Vivamus massa sem, cursus vel semper non, dictum vitae mi. Donec sed bibendum ante.
</div>
</div>
简单的例子。注意,您可以将其转换为响应性。
#main {
display: table;
}
#navigation, #content {
display: table-cell;
}
看看这个例子。
如前所述,flexbox是最简单的。 如。
#main{ display: flex; align-items:center;}
这将使所有子元素对齐到父元素的中心。
这个答案是不理想的,因为CSS flexbox和网格实现到CSS。然而,这仍然是一个可行的解决方案
在一个较小的屏幕上,你可能希望保持高度自动,因为col1, col2和col3是相互堆叠的。
然而,在媒体查询断点之后,您希望所有列的cols都以相同的高度紧挨着出现。
1125 px只是窗口宽度断点的一个例子,在此之后,您将希望将所有列设置为相同的高度。
<div class="wraper">
<div class="col1">Lorem ipsum dolor sit amet.</div>
<div class="col2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos laudantium, possimus sed, debitis amet in, explicabo dolor similique eligendi officia numquam eaque quae illo magnam distinctio odio, esse vero aspernatur.</div>
<div class="col3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorem, odio qui praesentium.</div>
</div>
当然,如果需要,还可以设置更多的断点。
<script>
$(document).ready(function(){
$(window).on('load resize', function() {
let vraperH = $('.wraper').height();
if (window.innerWidth>= 1125) {
$('.col1').height(vraperH);
$('.col2').height(vraperH);
$('.col3').height(vraperH);
}
if (window.innerWidth < 1125) {
$('.col1').height('auto');
$('.col2').height('auto');
$('.col3').height('auto');
}
});
});
</script>
经过长时间的寻找和尝试,没有什么能解决我的问题
style = "height:100%;"
关于儿童div
对于父母应用这个
.parent {
display: flex;
flex-direction: column;
}
此外,我使用bootstrap,这并没有腐败的响应为我。
我的解决方案:
$(window).resize(function() {
$('#div_to_occupy_the_rest').height(
$(window).height() - $('#div_to_occupy_the_rest').offset().top
);
});
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
来自:
http://getbootstrap.com.vn/examples/equal-height-columns/equal-height-columns.css
状态引导,但你不需要引导来使用这个。回答这个老问题,因为这对我来说很管用,而且似乎很容易实现。
这和我对这个问题的回答是一样的
这个技巧确实有用:在HTML部分中添加最后一个元素 具有明确的风格:兼而有之; < div风格= "明确:;" > < / div > 在此之前的所有内容都将包含在高度中。
你使用CSS Flexbox
.flex-container { display: flex; background-color: DodgerBlue; } .flex-container > div { background-color: #f1f1f1; margin: 10px; padding: 20px; font-size: 30px; } <!DOCTYPE html> <html> <head> </head> <body> <div class="flex-container"> <div>1</div> <div>2</div> <div>3</div> </div> <p>A Flexible Layout must have a parent element with the <em>display</em> property set to <em>flex</em>.</p> <p>Direct child elements(s) of the flexible container automatically becomes flexible items.</p> </body> </html>
我有同样的问题,它的工作基于Hakam Fostok的答案,我已经创建了一个小例子,在某些情况下,它可能不需要添加显示:flex;而弯曲方向:圆柱;在父容器上
.row {
margin-top: 20px;
}
.col {
box-sizing: border-box;
border: solid 1px #6c757d;
padding: 10px;
}
.card {
background-color: #a0a0a0;
height: 100%;
}
JSFiddle
对我有效的是添加 style={{display: 'flex', overflowY: "auto"}} 给所有孩子的父母,然后 style={{overflowY: "auto"}} 对孩子本身。
2021年的读者:
试试这个:
.parent {
position: relative;
display: flex; // And you will probably need this too.
flex-direction: row; // or column.
}
.child {
position: absolute;
top: 0;
bottom: 0;
align-self: center; // And you will probably need this too.
}
以下是我正在做的事情:
我可能有点晚了,但我找到了一个周围的工作,可能是一个hack。 首先给父层一个弯曲和100vh的高度
就像这样:
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;/* Can be less depends on the container, basically this forces the element to expand */
}
下面的代码提供了设置滚动div的最小高度的能力,并通过使用flex使其具有其父div的100%高度。
CSS:
.main {
display: flex;
flex-direction: column;
}
.scroll {
overflow: auto;
flex: 1 0 50px;
}
HTML:
<div class="main">
<div class="scroll">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
这段代码非常适合我
<html>
<body style="display: flex;">
<div style="position: absolute; width: 100%; height: 100%; background-color: aliceblue">
<table style="width: 100%; height: 100%;">
<tr>
<td style="width: 70%; background-color: green"></td>
<td style="background-color: red"></td>
</tr>
</table>
</div>
</body>
你可以把display: flex到div#主,align-self: stretch到div#导航。
<div id="header"></div>
<div id="main">
<div id="navigation"></div>
<div id="content"></div>
</div>
<div id="footer"></div>
// CSS file
#main {
display: flex;
}
#navigation {
align-self: stretch;
}