发新话题
打印

求助 DIV浮动排版错误!

求助 DIV浮动排版错误!

代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
body{ margin:0 auto; font-size:12px; font-family:Verdana; line-height:1.5;}
ul,dl,dd,h1,h2,h3,h4,h5,h6,form,p { padding:0; margin:0;}
ul { list-style:none;}
img {border:0px;}
a {color:#05a; text-decoration:none;}
a:hover { color:#f00;}
.clearfloat {clear:both;height:0;font-size: 1px;line-height: 0px;}

/*body*/
#container { width:900px; margin:0 auto;}


/*header*/
#header { height:70px; background:#ccffcc; margin-bottom:8px;}
#nav { height:30px; background:#ccffcc; margin-bottom:8px;}

/*main*/
#maincontent{margin-bottom:8px; overflow:auto; zoom:1;}
#main { float:left; width:664px; height:500px; background:#ffff99;}
#side { float:right; width:200px; height:500px; background:#ffcc99;}

/*footer*/
#footer { height:70px; background:#ccffcc;}
</style>
</head>

<body>
<div id="container">
  <div id="header">此处显示id header内容</div>
<div class="clearfloat"></div>
  <div id="nav">此处显示id nav 的内容</div>
<div class="clearfloat"></div>
  <div id="maincontent">
    <div id="main">此处显示id main的内容</div>
<div class="clearfloat"></div>
    <div id="side">此处显示id side的内容</div>
<div class="clearfloat"></div>
  </div>
  <div id="footer">此处显示id footer的内容</div>
</div>
</body>
</html>

效果如下:
附件: 您所在的用户组无法下载或查看附件
main 版块与 side版块,应该对齐,但是目前 side版块向下了!

求解,CSS 那里出错了!
问题原因:<div class="clearfloat"></div> 乱用
解决:
将以下代码
<div id="maincontent">
    <div id="main">此处显示id main的内容</div>
<div class="clearfloat"></div>
    <div id="side">此处显示id side的内容</div>
<div class="clearfloat"></div>
  </div>

改为
<div id="maincontent">
    <div id="main">此处显示id main的内容</div>
    <div id="side">此处显示id side的内容</div>
<div class="clearfloat"></div>
  </div>
其实清楚浮动的我通常只写一个属性
clear{clear:both;}
这样就够了。

我还是给管理员的代码讲一下原因吧。
为什么在主要内容与侧边之外还要套一个块,那是因为现在的显示屏比较大,如果是全屏的内容,就有可能要出现滚动条,这样不利于访问者,同时也不美观。大多的网站中的主要使用的宽度是1000px或是980px,在保证了网站内容会在显示器的中央显示后,你才可以把内容再分为左侧主要内容和右侧的侧边栏。
在侧边的代码结束后加上一个空的块,来清除前面的浮动。这样保险一点。或者你把布局计算得非常清楚,因为没有空间了,下面的内容也同样不会浮动上来。
发新话题