0

之前,这一切都是我的页面,我正在创建一个布局,使用 css@media查询,根据浏览器的像素检测和调整页面....

一切都很好,但如果你看到:

<div id='x'>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
</div>

您会注意到,当从桌面查看时,它已经溢出了唯一div的父级。<div id='sep'></div>

你可以查看页面的源代码,或者这里是完整的代码:

<html>
<head><style>
body{margin:0px; padding:0px;}
#a{height:150px; background-color:#ffffff; border-bottom:1px solid #CCC;}
#x{min-height:350px; margin-top: 10px; background-color:#CCC;}
#y{min-height:30px; margin-top: 10px; background-color:#CCC;}

@media all and (min-width:700px){

#b{width:100%;  min-height:400px;}
    #sep{width:960px; margin:auto;}
    #x{width:65%; float:left;}
    #y{width:32%; float:right; }
#c{background-color:#656565; height:80px;}
.for-mob{display:none;}
.for-desc{background-color:green;}
}
@media all and (max-width:700px){
.for-mob{background-color:green;}
.for-desc{display:none;}
#b{width:100%; min-height:400px; }
#x{width:100%; display:block; }
#y{width:100%; display:block; }
#c{background-color:#656565; height:100px;}

}
</style></head>


<body>

<div id='a'> <h1>I am header </h1>
</div> 

<div id='b'>
<div id='sep'>

    <div id='x'>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    <h1>I am content </h1>
    </div>
    <div id='y'>
    <h1>I am sidebar </h1>
    </div>
</div> 
</div> 
<div class='for-desc'><h1>If you shrink the browser, I become invisible</h1> </div>
<div class='for-mob'><h1>If you maximize the browser, I become invisible</h1></div>

<div id='c'>
<h1> I am a simple footer </h1>
</div> 
</body>
4

2 回答 2

1

由于您没有为 指定任何高度#sep,这意味着您希望它随内容扩展。您可以添加overflow: hidden它以使其扩展

#sep {
  margin: auto;
  overflow: hidden;
  width: 960px;
}

如果此答案不能回答您的问题,请在问题中说明您的要求:)

于 2013-05-06T20:55:44.160 回答
0

#b对包含浮动 div(或)的非浮动 div 应用 clearfix #sep

#sep:after {
    content:"";
    display:table;
    clear:both;
}

收集自http://www.css-101.org/articles/clearfix/latest-new-clearfix-so-far.php

于 2013-05-06T20:58:07.910 回答