[英]Cannot extend div height when hidden divs within are made visible
我有一个<div>
,它包含另一个<div>
,而该<div>
又包含一系列<div>
,可以进行切换。 我将前两个<div>
设置为具有height: 100%;
。 因此,现在当我看到太多<div>
且无法放入屏幕时,主<div>
不会扩展,而使bg成为主体的颜色。
(简化的) html
如下所示:
<div id="wrapper">
<div class="col" id="col1"></div>
<div class="col" id="col2"></div>
<div class="col" id="col3"></div>
</div>
和相关的CSS
:
body, html {
background-color: darkgray;
margin: 0;
height: 100%;
width: 100%;
}
#wrapper {
width: 80%;
height: 100%;
margin: 0 auto;
margin-top: 0;
margin-bottom: 0;
padding: 0;
background-color: #666;
color: white;
min-width: 722px;
max-width: 1119px;
font-family: 'Roboto Condensed', sans-serif;
box-shadow: 0px 0px 25px black;
}
.col {
/*background-color: red;*/
height: 100%;
float:left;
margin-top: 0;
margin-right: 0.6667%;
margin-left: 0.6667%;
}
#col1 {
width: 24%;
}
#col2 {
width: 48%;
box-shadow: 0px 0px 5px black;
}
#col3 {
width: 24%;
}
这是问题的屏幕截图: 我试图将以下代码添加到
jQuery
(在.click
函数中)以解决问题:
var top = $('#col2:last-child').position().top;
var bottom = top + $('#col2:last-child').height();
$('#wrapper').css('height', bottom);
在chrome中,这会在控制台中给我以下错误:
Uncaught TypeError: Cannot read property 'top' of undefined
如果可以的话,我真的很想在CSS
解决此问题。
我将使用display: inline-block;
而不是float: left;
我认为是个人喜好,但是在可能的情况下,我会尽量避免弄乱文档流(这可能会引起类似您所看到的问题)。
#wrapper {
width: 80%;
margin: 0 auto;
padding: 10px;
background-color: #666;
color: white;
min-width: 722px;
max-width: 1119px;
font-family:'Roboto Condensed', sans-serif;
box-shadow: 0px 0px 25px black;
}
.col {
width: 24%;
/*background-color: red;*/
display:inline-block;
margin-top: 0;
vertical-align:top;
}
#col2 {
width: 48%;
box-shadow: 0px 0px 5px black;
}
p {
text-align: justify;
margin-left: 5%;
margin-right: 5%;
}
img {
margin-top: 10px;
display: inline-block;
padding: 5%;
}
ul {
text-align: justify;
margin-left: -5%;
margin-right: 5%;
}
#col2 h1 {
line-height: 1em;
text-align: left;
padding: 2.5%;
margin-top: 0;
margin-bottom: 0;
background-color: #444;
border-bottom: solid 1px black;
cursor: pointer;
text-shadow:0px 0px 6px black;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#col2 ul {
list-style: none;
margin-left:-2.5%;
}
.col em {
color:#000;
cursor: pointer;
}
.col em:hover {
text-decoration: underline;
}
#homepageButton {
width: 50px;
height: 50px;
left: 15px;
bottom: 0;
position: fixed;
background-color: #444;
margin-bottom: 0;
box-shadow: 0px 0px 5px black;
padding: 5px;
text-align: center;
}
#homepageButton a {
text-decoration: none;
color: #fff;
text-shadow:0px 0px 6px black;
}
#homepageButton a:hover {
text-decoration: underline;
}
* {
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
box-sizing:border-box;
}
我还添加了box-sizing: border-box;
一切...请参阅: Paul Irish对此的解释
这是你所追求的吗? http://jsfiddle.net/sodiumnitrate/HbjtG/1/
我在包装器中添加了一个.clearfix
,并强制包装器达到最高高度,以使其至少占据整个视口,但如果内容需要它,则可以增大包装器。
/* clearfix */
.clearfix:before,
.clearfix:after {
content: ".";
display: block;
height: 0;
overflow: hidden;
}
.clearfix:after {
clear: both;
}
.clearfix {
zoom: 1; /* IE < 8 */
}
#wrapper {
/* force full height */
min-height: 100%;
height: auto !important;
height: 100%;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.