东莞网站建设服务首,莱芜杂谈莱芜在线论坛,网站开发的交付文档,网络营销策略内容我们在制作网页中用divcss或者称xhtmlcss都会遇到一些很诡异的情况#xff0c;明明布局正确#xff0c;但是整个画面却混乱起来了#xff0c;有时候在IE6下看的很正常的#xff0c;到ie7或者火狐下看时#xff0c;就一片混乱了#xff0c;无论怎么计算#xff0c;就是不…我们在制作网页中用divcss或者称xhtmlcss都会遇到一些很诡异的情况明明布局正确但是整个画面却混乱起来了有时候在IE6下看的很正常的到ie7或者火狐下看时就一片混乱了无论怎么计算就是不能将排版改正过来。其实这一切都是浮动搞得鬼也就是css中的float要解决情况就需要使用clear:both了。 CSS手册上是这样说明的该属性的值指出了不允许有浮动对象的边。这个属性是用来控制float属性在文档流的物理位置的。 当属性设置float浮动时其所在的物理位置已经脱离文档流了但是大多时候我们希望文档流能识别float浮动或者是希望float浮动后面的元素不被float浮动所影响这个时候我们就需要用clear:both;来清除。 程序代码:
p stylefloat:left;width:200px;这个是第1列/p p stylefloat:left;width:400px;这个是第2列/p p这个是第3列。/p
如果不用清除浮动那么第3列文字就会和第1、2列文字在一起 所以我们在第3个这列加一个清除浮动 clear:both; 通常我们往往会将“清除浮动”单独定义一个CSS样式如: 程序代码 .clear { clear: both; } 然后使用div classclear/div来专门进行“清除浮动”。 不过也有不赞同意见是div classclear/div可以不写直接在下层清除就可以了。 比如本来好好的 程序代码 p stylefloat:left;width:200px;这个是第1列/p p stylefloat:left;width:400px;这个是第2列/p p styleclear:both;这个是第3列。/p 非要整成 程序代码 p stylefloat:left;width:200px;这个是第1列/p p stylefloat:left;width:400px;这个是第2列/p div classclear/div p这个是第3列。/p 这点看来div classclear/div确实不需要写。 不过很显然我们在网页设计时还有一种很普遍的情况 程序代码 style typetext/css #main {background-color: #3399CC;width: 600px;padding: 20px;} #sidebar {background-color: #FF6600; float: left;width: 130px;} #container {float: right;width: 420px;background-color: #FFFF33;} /style div idmain div idsidebar第一段内容 第一段内容 第一段内容/div div idcontainer第二段内容 第二段内容 第二段内容/div /div p styleclear:both;第三段内容/p 该页面测试在IE下效果正合所要蓝色块内部有红色和黄色两个色块内容同时在蓝色块以下是第三段文本。 不过FF的效果可不是这样的。我们不能单单想在下一层清除就能完成我们的工作我们必须在浮动元素所在标签闭合之前及时进行“清除”。 程序代码 style typetext/css #main {background-color: #3399CC;width: 600px;padding: 20px;} #sidebar {background-color: #FF6600; float: left;width: 130px;} #container {float: right;width: 420px;background-color: #FFFF33;} .clear {clear: both;} /style div idmain div idsidebar第一段内容 第一段内容 第一段内容/div div idcontainer第二段内容 第二段内容 第二段内容/div div classclear/div /div p第三段内容/p 对于因多加的div classclear/div标签会引起IE和FF高度变化通过如下方法解决 程序代码 clear { clear: both; height:1px; margin-top:-1px; overflow:hidden; } 程序代码 style typetext/css #main {background-color: #3399CC;width: 600px;padding: 20px;} #sidebar {background-color: #FF6600; float: left;width: 130px;} #container {float: right;width: 420px;background-color: #FFFF33;} .clear {clear: both;height:1px;margin-top:-1px;overflow:hidden;} /style div idmain div idsidebar第一段内容 第一段内容 第一段内容/div div idcontainer第二段内容 第二段内容 第二段内容/div div classclear/div /div p第三段内容/p