---
title: "Firefox下margin-top问题"
author: "minsifansi"
date: "2010-06-22T15:17:33Z"
tags: []
categories: ["Web前端"]
slug: "firefox-margin-top"
draft: false
lastmod: "2010-06-22T15:17:33Z"
id: 696510
url: "https://minsifansi.iteye.com/blog/696510"
---

**（一）margin-top失效**  
   先看下面代码：

&lt;div&gt;  
&lt;div class="box1" &gt;float:left&lt;/div&gt;  
&lt;div class="box2"&gt;clear:both; margin-top:20px;&lt;/div&gt;  
&lt;/div&gt;

两个层box1和box2，box1具有浮动属性，box2没有，这时候设置box2的上边距 margin-top没有效果。

 

&lt;!DOCTYPE &lt;a href="http://www.blue1000.com/bkhtml/c22/" target="\_blank" title="HTML/JavaScript教程"&gt;HTML&lt;/a&gt; PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html &lt;a href="http://www.blue1000.com/bkhtml/c21/" target="\_blank" title="XML教程"&gt;XML&lt;/a&gt;ns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
&lt;title&gt;Firefox下margin-top问题&lt;/title&gt;  
&lt;style type="text/&lt;a href="http://www.blue1000.com/bkhtml/c23/" target="\_blank" title="CSS教程"&gt;CSS&lt;/a&gt;"&gt;  
&lt;!--  
\* {  
margin:0;  
padding:0;  
}  
.box1{  
float:left;  
width:500px;  
height:100px;  
background:#999;  
}  
.box2{  
margin-top:20px;  
width:500px;  
height:50px;  
background:#000;  
color:#fff;  
clear:both;  
}  
\--&gt;  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div&gt;  
&lt;div class="box1" &gt;float:left&lt;/div&gt;  
&lt;div class="box2"&gt;clear:both; margin-top:20px;&lt;/div&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;

 

网上能找到的两种比较靠谱的解释：1：“在css2.1中，水平的margin不会被折叠；垂直margin可能在一些盒模型中被折叠…”2：当第一个层浮动，而第二个没浮动层的margin会被[压缩](http://www.blue1000.com/bkhtml/c171/ "压缩工具教程") ，详见--浮动元素后非浮动元素的margin的处理（ 地址 ）。

得到解决问题思路：要浮动一起浮动，要就一起不浮动。

解决办法：

1.box2增加float属性  
2.box1与box2之间增加一层"&lt;div style="clear:both;"&gt;&lt;/div&gt;"

 

**（二）子元素设置margin-top作用于父容器**

&lt;div class="box" style="height:100px;background:red;"&gt;  
  &lt;div class="box2"&gt;clear:both; margin-top:20px;height:50px;width:500px;background:#000;&lt;/div&gt;  
&lt;/div&gt;

当给box2设置margin-top时，在FF下仅作用于父容器。

 

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;  
&lt;title&gt;Firefox下margin-top问题&lt;/title&gt;  
&lt;style type="text/css"&gt;  
&lt;!--  
\* {  
margin:0;  
padding:0;  
}  
.box2{  
margin-top:20px;  
width:500px;  
height:50px;  
background:#000;  
color:#fff;  
}  
\--&gt;  
&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div class="box" style="height:100px;background:red;"&gt;  
&lt;div class="box2"&gt;clear:both; margin-top:20px;height:50px;width:500px;background:#000;&lt;/div&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;

 

解决办法：

1.给父容器box加overflow:hidden;属性  
2.父容器box加border除none以外的属性  
3.用父容器box的padding-top代替margin-top