如何在不增加宽度的情况下向文本区域添加填充? [英] How can I add padding to a textarea without causing the width to increase?

查看:123
本文介绍了如何在不增加宽度的情况下向文本区域添加填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在设置textarea元素的宽度和通过CSS使用填充方面遇到麻烦.填充值似乎会更改文本区域的宽度,而我不希望这样做.

I've been having trouble setting a textarea element's width and using padding via CSS. The padding value seems to change the width of the textarea, which I'd rather it not do.

这是我的HTML代码:

This is my HTML code:

<div id="body">
    <textarea id="editor"></textarea>
</div>

还有我的CSS代码:

#body {
    height:100%;
    width:100%;
    display:block;
}

#editor {
    height:100%;
    width:100%;
    display:block;
    padding-left:350px;
    padding-right:350px;
}

但是,填充值似乎并不像人们期望的那样起作用. textarea的宽度在两个方向上都增加了350px,而不是在元素的边界与其内容之间定义空间.

However, the padding values do not appear to work as one would expect. The width of the textarea is increased by 350px in both directions, rather than defining space between the borders of the element and its content.

我已经考虑过通过将边距设置为"0px auto"来使文本区域居中,但是我希望用户仍然能够在鼠标悬停在空白区域之一上时滚动浏览文本区域.出于同样的原因,我不能添加另一个div作为包装器,因为用户将无法沿着空白区域滚动,而只能沿着无边距的文本区域滚动.

I've considered just centering the textarea by setting the margins at "0px auto", but I would like the user to still be able to scroll through the textarea if the mouse is hovering over one of the empty margins. For the same reason I can't add another div to act as a wrapper, as the user wouldn't be able to scroll along the empty areas but only along the margin-less textarea.

有人可以帮忙吗?

推荐答案

CSS框模型将宽度"定义为内容的宽度,不包括边框,内边距和边距.

The CSS box model defines "width" as the width of the content, excluding border, padding and margin.

幸运的是,CSS3具有一个新的 box-sizing 属性,可让您修改此行为,改为使用以下命令在指定的宽度中包括填充等:

Fortunately, CSS3 has a new box-sizing property that lets you modify this behaviour to instead include padding etc. in the specified width using:

box-sizing: border-box;

根据上面的链接,大多数现代的浏览器(包括IE> = 8)都支持此属性,但是在每个浏览器中它们都有不同的名称.

According to the link above, most modern browsers (including IE >= 8) support this property, but they have different names in each browser.

这篇关于如何在不增加宽度的情况下向文本区域添加填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆