---
title: "不同浏览器上按钮的不同显示效果和解决方法"
author: "peterlij"
date: "2013-10-08T22:16:01Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2013-10-08T22:19:35Z"
id: 1953463
url: "https://peterlij.iteye.com/blog/1953463"
---

IE和FireFox在显示按钮时，具有不同的行为。这个行为在单独使用按钮时表现不太明显，但是当把一个很大的按钮放置到另外一个更小的包容元素时候，就表现的特别明显。比如放置到一个div元素当中，如下所示：

 

```
<html>
  <head>
    <style>
      .buttonContainer {
        position:absolute;
        overflow:visible;
      }
      
      .backbutton {
        font-size: 20px;
        font-family: Arial;
        font-weight: bold;
        font-style: normal;
        font-variant: normal;
      }
    </style>
  </head>
  <body>
    <div id="widget1" class="buttonContainer" style="left: 4px; top: 48px; width: 31px; height: 11px; z-index: 1; cursor: pointer; border: 1px solid red;">
      <div style="height: 100%; width: 100%; border: 1px solid green;">
        <button style="height: 100%; width: 100%;" class="backbutton">Back</button>
      </div>
    </div>
    
    <div id="widget2" class="buttonContainer" style="left: 4px; top: 100px; width: 31px; height: 11px; z-index: 1; cursor: pointer; border: 1px solid red;">
      <div style="height: 100%; width: 100%; border: 1px solid green;">
        <input type="button" value="Back" style="height: 100%; width: 100%;" class="backbutton"/>
      </div>
    </div>
  </body>
</html>
```

在FireFox和IE上分别执行上述代码后的结果如下：

 

     **IE                                                   FireFox**  
    ![](http://dl2.iteye.com/upload/attachment/0089/9532/5e4e6240-25a5-3ab2-b23a-9dd8f7662391.jpg)                                             ![](http://dl2.iteye.com/upload/attachment/0089/9534/214d1504-baa6-37fd-8da9-8a12f6ed92d8.jpg)

可以看到，虽然按钮的高度都设置为相对于包容元素的100%，但是在IE中按钮自动撑高了包容元素，而在Firefox中，按钮超出包容元素的部分被截断了。

为了解决上述问题，可以将按钮的高度设置为auto，如下所示：

 

```
<button style="height: auto; width: 100%;" class="backbutton">Back</button>
<input type="button" value="Back" style="height: auto; width: 100%;" class="backbutton"/>
```

在FireFox和IE上分别执行上述代码后的结果如下：

     **IE                                        FireFox**  
    ![](http://dl2.iteye.com/upload/attachment/0089/9537/ab50ddf2-2f5b-33c8-b8ab-4e3a3d67a42b.jpg)                                 ![](http://dl2.iteye.com/upload/attachment/0089/9539/4af323fc-342a-3034-8e84-ef196a2a2ade.jpg)

此时，当包容元素没有设置边框时候，IE和FireFox的显示效果就比较类似了。

 

另外，FireFox中的按钮看起来总是比用其他方式模拟的按钮大(比如A标签)，也比IE中的按钮大，原因是FireFox的按钮中有一个伪元素(Pseudo-elements)，可能用来当按钮获得焦点时候，显示按钮的虚边框。而这个伪元素会占据额外的高度和宽度。如[Fix Firefox Button Padding](http://ovms.co/2010/06/fix-firefox-button-padding/)中描述的，我们可以通过设置特定于Firefox的css来解决这个问题。

 

```
      /* Used within FF chrome to target CSS to specific URLs: being FF-specific,
       it is also useful for targeting FF-only code */
      @-moz-document url-prefix(http://) {
        button::-moz-focus-inner, input[type="button"]::-moz-focus-inner, input[type="submit"]::-moz-focus-inner, input[type="reset"]::-moz-focus-inner {
          padding: 0 !important;
          border: 0 none !important;
        }
      }
```

有时候，上述设置过后，仍然会发现按钮比其他模拟元素大，此时可以像[Firefox Button Height Fix](http://davidwalsh.name/firefox-buttons)中描述的一样，根据页面具体情况，设置按钮的margin属性如下：

 

 

```
input::-moz-focus-inner { 
	border: 0; 
	padding: 0; 
	margin-top:-2px; 
	margin-bottom: -2px; 
}
```

 

 

另外为了使各个浏览器中的按钮显示尽量一致，我们可以设置按钮的overflow属性为visible，width属性为auto，如[Styling HTML Submit Buttons](http://www.seowebtips.com/styling-html-submit-buttons/)中描述的：

 

```
.submit {
  cursor:pointer;
  overflow:visible; /* ie6/7 width fix */
  width:auto; /* ie6/7 width fix */
  padding:1px 5px;
  background:#ddd;
  color:#333;
  font:10pt arial, sans-serif;
  border:1px solid #aaa;
  }
```

我们也可以全部使用图片按钮来代替普通按钮，如[-moz-focus-inner](http://stackoverflow.com/questions/1433232/moz-focus-inner)中描述的。但是当需要国际化时候，则需要很多的图片来支持不同的语言，这个显然不太合适。

 

其他比较有意思的关于按钮和CSS的文章：

[css3-patterned-buttons](https://github.com/catalinred/css3-patterned-buttons)

[15 css tricks that must be learned](http://blog.themeforest.net/general/15-css-tricks-that-must-be-learned/)