I have recently been doing some researching on the problem described by OP for a similar question on SO. It seems that a bug in Firefox is causing the rendering of this so called "not-padding-but-looks-like-padding" on textarea
elements.
Usually this extra padding is not really an issue, but it becomes an issue when you want to keep two elements the same width, and you care about getting its content to wrap the same way in both elements.
Getting textarea
's to wrap content the same as e.g. div
elements in Firefox
It seems to be impossible to get rid of this 1.5px
wide padding on the textarea
in Firefox, so if you want to ensure that the content wrapping inside a div
in Firefox behaves exactly the same as the content wrapping inside a textarea
in Firefox, the best approach seems to be to add an additional 1.5px
of padding on the right and the left hand side inside the div
, but only in Firefox. You can accomplish this by setting the following vendor specific prefixed CSS properties on your div
:
-moz-box-sizing: border-box;
-moz-padding-end: 1.5px;
-moz-padding-start: 1.5px;
The first ensures that the padding set on the div
does not increase the width of the div
, and the next two ensure that 1.5px
of padding will be set on the right and the left hand side of the div
.
This approach does not affect the rendering of the div
's in any other browsers, it doesn't need to, as textarea
's in other browsers don't render any extra padding. But it ensures that there are no content wrapping differences between div
's and textarea
's inside Firefox as long as they share the same font-family
and font-size
properties and so on.
Here's a jsFiddle for demonstration purposes.
Getting textarea
's to wrap content consistently across browsers
If you only wanted to ensure that a textarea
in Firefox has the same width and wrapping behaviour as a textarea
in other browsers, you can set its box-sizing
to border-box
, add a padding
on both sides of 5.5px
and set -moz-padding-end
and -moz-padding-start
to 0px
.
textarea {
padding: 0 5.5px 0 5.5px;
-moz-padding-end: 0px;
-moz-padding-start: 0px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
Here's a jsFiddle showing this approach.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…