如何禁用文本区域的可调整大小属性?
I want to disable the resizable property of a textarea
.
Currently, I can resize a textarea
by clicking on the bottom right corner of the textarea
and dragging the mouse. How can I disable this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
以下 CSS 规则禁用
textarea
元素的大小调整行为:要禁用某些(但不是全部)
textarea
,可以使用 几个选项。您可以在标记中使用
class
属性 (要禁用特定的
textarea
,请使用 < code>name 属性设置为foo
(即):
或者,使用 < code>id 属性(即
):
W3C 页面 列出了调整大小限制的可能值:无、两者、水平、垂直和继承:
查看一个不错的 兼容性页面 查看当前有哪些浏览器支持此功能。正如 Jon Hulka 所评论的,尺寸可以在 CSS 中使用 max-width、max-height、进一步限制,最小宽度和最小高度。
The following CSS rule disables resizing behavior for
textarea
elements:To disable it for some (but not all)
textarea
s, there are a couple of options.You can use
class
attribute in your tag(<textarea class="textarea1">
):To disable a specific
textarea
with thename
attribute set tofoo
(i.e.,<textarea name="foo"></textarea>
):Or, using an
id
attribute (i.e.,<textarea id="foo"></textarea>
):The W3C page lists possible values for resizing restrictions: none, both, horizontal, vertical, and inherit:
Review a decent compatibility page to see what browsers currently support this feature. As Jon Hulka has commented, the dimensions can be further restrained in CSS using max-width, max-height, min-width, and min-height.
在CSS中:
In CSS:
我发现了两件事:
首先
这是一个CSS 3,尚未发布,与Firefox 4(及更高版本)、Chrome 和 Safari。
另一个格式功能是
overflow: auto
来获取去掉右侧滚动条,考虑到 dir 属性。代码和不同浏览器
基本 HTML
某些浏览器
I found two things:
First
This is a CSS 3, which is not released yet, compatible with Firefox 4 (and later), Chrome, and Safari.
Another format feature is to
overflow: auto
to get rid of the right scrollbar, taking into account the dir attribute.Code and different browsers
Basic HTML
Some browsers
CSS 3 为 UI 元素提供了一个新属性,允许您执行此操作。该属性是调整大小属性。因此,您可以将以下内容添加到样式表中以禁用所有文本区域元素的大小调整:
这是一个 CSS 3 属性;使用兼容性图表查看浏览器兼容性。
就我个人而言,我发现在文本区域元素上禁用调整大小非常烦人。这是设计者试图“破坏”用户客户端的情况之一。如果您的设计无法容纳更大的文本区域,您可能需要重新考虑设计的工作方式。任何用户都可以添加
textarea { resize: Both !important; }
添加到他们的用户样式表中以覆盖您的首选项。CSS 3 has a new property for UI elements that will allow you to do this. The property is the resize property. So you would add the following to your stylesheet to disable resizing of all textarea elements:
This is a CSS 3 property; use a compatibility chart to see browser compatibility.
Personally, I would find it very annoying to have resizing disabled on textarea elements. This is one of those situations where the designer is trying to "break" the user's client. If your design can't accommodate a larger textarea, you might want to reconsider how your design works. Any user can add
textarea { resize: both !important; }
to their user stylesheet to override your preference.如果您需要深度支持,您可以使用老式技术:
If you need deep support, you can use an old school technique:
这可以很容易地在 HTML 中完成:
这对我有用。
draggable
属性的默认值为true
。This can be done in HTML easily:
This works for me. The default value is
true
for thedraggable
attribute.您只需在 CSS 中使用:
resize: none;
。此外,目前 Internet Explorer 还不支持调整大小。
以下是调整大小的不同属性:
不调整大小:
双向调整大小(垂直和水平):
垂直调整大小:
水平调整大小:
此外,如果您的 CSS 或 HTML 中有
width
和height
,它将阻止您的文本区域调整大小,并具有更广泛的浏览器支持。You simply use:
resize: none;
in your CSS.Also resize not supported in Internet Explorer at the moment.
Here are different properties for resize:
No Resize:
Resize both ways (vertically & horizontally):
Resize vertically:
Resize horizontally:
Also if you have
width
andheight
in your CSS or HTML, it will prevent your textarea be resized, with a broader browsers support.使用此属性
resize: none;
Use this property
resize: none;
您可以简单地禁用 textarea 属性,如下所示:
要禁用垂直或水平调整大小,请使用
或
You can simply disable the textarea property like this:
To disable vertical or horizontal resizing, use
or
要禁用调整大小属性,请使用以下 CSS 属性:
您可以将其应用为内联样式属性,如下所示:
或在
元素标签之间应用,如下所示:
<前><代码>文本区域{
调整大小:无;
}
To disable the resize property, use the following CSS property:
You can either apply this as an inline style property like so:
or in between
<style>...</style>
element tags like so:您需要在
component.css
中设置以下 CSS 代码you need to set the below CSS code in your
component.css
上面的代码将禁用项目中所有
在你的 HTML 中
The code above will disable the resizable property of all
<textarea/>
elements in your project. If you want that that is fine, otherwise you would want to use a specific class for your textarea elements.In your HTML
我创建了一个小演示来展示调整属性大小的工作原理。我希望它对您和其他人也有帮助。
I have created a small demo to show how resize properties work. I hope it will help you and others as well.
CSS 3 可以解决这个问题。不幸的是,目前仅 60% 的常用浏览器支持它。
对于 Internet Explorer 和 iOS,您无法关闭调整大小,但您可以通过设置其
宽度
和高度
来限制textarea
尺寸。查看演示
CSS 3 can solve this problem. Unfortunately it's only supported on 60% of used browsers nowadays.
For Internet Explorer and iOS you can't turn off resizing, but you can limit the
textarea
dimension by setting itswidth
andheight
.See Demo
要禁用所有
textarea
的大小调整:要禁用特定
textarea
的大小调整,请添加属性、name
或id< /code> 并将其设置为某些内容。在本例中,它被命名为
noresize
HTML
CSS
To disable resize for all
textarea
s:To disable resize for a specific
textarea
, add an attribute,name
, or anid
and set it to something. In this case, it is namednoresize
HTML
CSS
您可以使用 textarea {resize: none;}
Demo
You can disable resizeable property of textarea using textarea {resize: none;}
Demo
使用
@style
,您可以为其指定自定义大小并禁用调整大小功能(resize: none;)
。With
@style
, you can give it a custom size and disable the resize feature(resize: none;)
.你也可以尝试使用 jQuery
You can try with jQuery also
添加
!important
使其起作用:outline
只是为了避免某些浏览器上出现蓝色轮廓。Adding
!important
makes it work:outline
is just to avoid the blue outline on certain browsers.如果有人正在寻找一种用普通 Javascript 实现此目的的方法:
注意:以这种方式设置 style 属性将覆盖您之前的所有 css 样式声明。
If anyone is looking for a way to this in plain Javascript:
Note: Setting the style property this way will overwrite all your prior css style declarations.