0

我有一个<div>包含另一个<div>,它又包含一系列<div>可以切换的 s。我已将前两个<div>s 设置为 have height: 100%;。所以现在当我有太多<div>的 s 可见并且它不适合屏幕时, main<div>不会扩展,让 bg 成为 body 的颜色。

这是(简化的)的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%;
}

这就是它在jsfiddle中的样子。

这是问题的屏幕截图: 问题截图 我尝试将以下代码添加到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

4

3 回答 3

2

您可能想要添加overflow:auto;#wrapper. 提琴手

要么,要么清除你div

编辑:如果你想让它包含整个文本,而不是可滚动的,你可以将其设置#wrapper为。提琴手height:auto;

于 2013-10-08T21:31:17.593 回答
1

我想我会使用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 对此的解释

于 2013-10-08T22:09:47.883 回答
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%;    
  ...
于 2013-10-08T21:46:31.740 回答