Why does Firefox use the IE box model for input el

2019-04-12 07:07发布

问题:

Try the following simple example:

<html>
<head>
<style>
  div,
  input {
    border: 1px solid #000;
    margin: 2px;
    padding: 3px;
    width: 100px;
  }
</style>
</head>
<body>
  <div>div</div>
  <input value="input" />
</body>
</html>

Notice the div and the input gets different widths. In fact, the width of the input is 92px. For the input, Firefox calculates the width outside the border and padding, the same way IE does for everything. Shouldn't it treat input elements the same as everything else, and add the padding and border to the width?

回答1:

As phihag says, it's block vs inline.

However, a flaw with your example: you're not using a DOCTYPE.

That means the browser is free to render as it sees fit.

Stick <!DOCTYPE html> at the top to use the nice and compact HTML5 DOCTYPE, which will prevent browsers from going into Quirks Mode.



回答2:

The width issue is a result of brain-dead IE-compatible quirks mode. See Peter Boughton's answer(doctype) for remedy.

Additionally, div is a block-level element, but input isn't. Add display:block; to your rule to achieve the div rendering in both cases, or display:inline; to format both elements as inline. However, Firefox will forgive top-level inline elements and render them appropriately.