如何强制 Firefox 渲染文本区域填充与 div 中的填充相同?

发布于 2024-10-06 13:01:11 字数 531 浏览 0 评论 0原文

我试图在 IE8、Firefox 和 Safari 的文本区域内提供一致的每行宽度(以像素为单位),以便文本内容尽可能可预测且一致地换行。

Firefox 做了一些有点奇怪的事情:与其他两个浏览器以及类似配备的 div 块相比,它有一个额外的像素填充,占用了文本区域的内容空间。

当将该类应用于文本区域和 div 时,差异是可见的,div 中的文本接触红色背景的左外边缘,但文本区域中的文本具有 1 px 类似填充的偏移量,尽管填充为零:

.testbox{
    padding:0;
    margin:0;
    border:0;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

与 div 相比,其他填充值最终会显示一个额外的偏移像素。

关于是否有办法欺骗 Firefox 将文本区域渲染为 div,或者调整文本区域的非填充但看起来像填充属性,有什么想法吗?

I'm attempting to provide a consistent width per line in pixels inside of a textarea across IE8, Firefox and Safari, so that text content wraps lines as predictably and consistently as possible.

Firefox is doing something a little bit odd: it has an extra pixel of padding eating out of the content space of the textarea vs the other two browsers, and vs a similarly equipped div block.

When applying this class to both a textarea and a div the difference is visible, with the text in the div touching the outer left edge of the red background but the text in the textarea have 1 px padding-like offset in spite of padding being zero:

.testbox{
    padding:0;
    margin:0;
    border:0;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

Other values for padding wind up displaying one extra pixel of offset vs a div.

Any ideas on if there's a way to trick Firefox to render a textarea as if it were a div, or to adjust this not-padding-but-looks-like-padding property for a textarea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北方的巷 2024-10-13 13:01:11

我最近一直在对OP描述的问题进行一些研究关于SO的类似问题。似乎 Firefox 中的一个错误导致在 textarea 元素上呈现所谓的“not-padding-but-looks-like-padding”。

通常,这种额外的填充并不是真正的问题,但是当您想要使两个元素保持相同的宽度,并且您关心使其内容以相同的方式包装在两个元素中时,它就会成为问题。

textarea 的内容与 Firefox 中的 div 元素相同

似乎不可能摆脱这个 1.5px 宽填充在 Firefox 中的 textarea 上,因此如果您想确保 Firefox 中 div 内包装的内容与 textarea< 内包装的内容完全相同/code> 在 Firefox 中,最好的方法似乎是在 div 内的右侧和左侧添加额外的 1.5px 填充,但仅限于 Firefox 。您可以通过在 div 上设置以下供应商特定的前缀 CSS 属性来实现此目的:

-moz-box-sizing: border-box;
-moz-padding-end: 1.5px; 
-moz-padding-start: 1.5px; 

第一个确保 div 上设置的填充不会增加 的宽度>div,接下来的两个确保在 div 的右侧和左侧设置 1.5px 的填充。

这种方法不会影响 div 在任何其他浏览器中的渲染,也不需要,因为 textarea 在其他浏览器中不会渲染任何内容额外的填充。但它确保 Firefox 中的 divtextarea 之间不存在内容包装差异,只要它们共享相同的 font-familyfont-size 属性等等。

这是用于演示目的的 jsFiddle

textarea 在浏览器中一致地换行内容

如果您只想确保 Firefox 中的 textareatextarea 具有相同的宽度和换行行为code>在其他浏览器中,可以将其box-sizing设置为border-box,在5.5两侧添加一个padding px 并将 -moz-padding-end-moz-padding-start 设置为 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;
}

这是一个 jsFiddle 展示了这种方法。

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.

生生不灭 2024-10-13 13:01:11

哇,我还不知道答案,但我确实尝试了一些东西,它看起来就像一个文本区域,当你向它应用边框、边距和填充时,不会改变它的宽度,而是将边框等放在里面。试试这个:

.testbox {
    padding: 10;
    margin: 10;
    border: 5px solid black;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

你可以通过使用类似这样的东西来解决这个问题:

<div class="testbox">
    <textarea class="testarea"></textarea>
</div>

css:

.testbox {
    padding: 0;
    margin: 0;
    border: 0;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

.testarea {
    padding: 0;
    margin: 0 -1px;
    border: 0;
    background: red;
    width: 100%;
    height: 100%;
    font-size: 12px;
    line-height: 16px;
}

这似乎也适用于 IE,除了 -1px 之外,它会使布局关闭(一个)。

Wow, I don't know the answer yet but I did try some stuff, and it appears as though a textarea, when you apply borders, margins and padding to it, doesn't change its width but puts the borders etc. on the inside. Try this:

.testbox {
    padding: 10;
    margin: 10;
    border: 5px solid black;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

You could work around this by using something like this:

<div class="testbox">
    <textarea class="testarea"></textarea>
</div>

css:

.testbox {
    padding: 0;
    margin: 0;
    border: 0;
    background: red;
    width: 40px;
    height: 40px;
    font-size: 12px;
    line-height: 16px;
}

.testarea {
    padding: 0;
    margin: 0 -1px;
    border: 0;
    background: red;
    width: 100%;
    height: 100%;
    font-size: 12px;
    line-height: 16px;
}

This also seems to work in IE, except for the -1px, which throws the layout off (by one).

温柔戏命师 2024-10-13 13:01:11

这是 Firefox 中的一个错误几天前已修复。该修复将在 Firefox 29 中发布。

我已经尝试过最新的 nightly build,textara bug 消失了!

This is a bug in firefox which got fixed a few days ago. The fix will be released with Firefox 29.

I already tried the latest nightly build and the textara bug is gone!

2024-10-13 13:01:11

我面临着同样的问题,虽然我的解决方案对于那个像素来说似乎向后弯曲太多,但它解决了问题,这里是:为了统一宽度,因为这种奇怪的行为,我没有使用 div,而是使用了禁用的具有白色背景和默认光标的文本区域可充当 div 的模仿。

I was facing the same problem and although my solution seemed like bending backwards too much for that one pixle, but it fixed the problem, here goes: To unify the width because of this weird behavior, Instead of using a div, i used a disabled textarea with a white background and a default cursor to act as a mimic the div.

三人与歌 2024-10-13 13:01:11

我遇到了类似的问题,带有背景图像和填充的链接标签在 Firefox 上显示效果不佳。当多行时,填充和背景似乎适用于文本行,而不是文本块。我测试了一些东西,最终使用了“display:block;”在元素 css 上。为我工作。

I was having a similar problem, a link tag with a background image and padding did not display well on firefox. The padding and background seemed to apply to the line of text, not the block of text, when multiline. I tested out a few things, and ended up using a "display:block;" on the element css. Worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文