当隐藏的div内部可见时,无法扩展div高度

时间:2013-10-08 21:19:43

标签: jquery css html height

我有一个<div>,其中包含另一个<div>,而<div>又包含一系列可以切换的<div>height: 100%;。我已将前两个<div>设置为<div>。所以,现在当我有太多html可见并且它不适合屏幕时,主<div id="wrapper"> <div class="col" id="col1"></div> <div class="col" id="col2"></div> <div class="col" id="col3"></div> </div> 不会延伸,使bg成为身体的颜色。

以下是(简化的)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

这就是jsfiddle中的样子。

这是问题的截图: screenshot of the problem 我尝试将以下代码添加到var top = $('#col2:last-child').position().top; var bottom = top + $('#col2:last-child').height(); $('#wrapper').css('height', bottom); Uncaught TypeError: Cannot read property 'top' of undefined 函数内)以解决问题:

CSS

,在chrome中,在控制台中给出了以下错误:

{{1}}

如果可以的话,我真的想在{{1}}内解决这个问题。

3 个答案:

答案 0 :(得分:2)

您可能希望将overflow:auto;添加到#wrapperJsFiddle

要么是这样,要么清除div

编辑:如果你想让它包裹整个文本而不是可滚动,你可以将#wrapper设置为height:auto;JsFiddle

答案 1 :(得分:1)

我认为我会使用display: inline-block;而不是float: left;个人偏好,但在可能的情况下,我会尽量避免弄乱文档流程(这会导致问题,例如您所看到的)。

<强> Working Example

#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's explanation for that

答案 2 :(得分:0)

这就是你追求的吗? 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%;    
  ...