提问人:user549757 提问时间:3/9/2011 最后编辑:Sunderam Dubeyuser549757 更新时间:10/11/2023 访问量:1795641
如何禁用 textarea 的 resizable 属性?
How do I disable the resizable property of a textarea?
答:
以下 CSS 规则禁用 textarea
元素的大小调整行为:
textarea {
resize: none;
}
要在某些(但不是全部)中禁用它,有几个选项。textarea
您可以在 tag() 中使用 attribute:class
<textarea class="textarea1">
.textarea1 {
resize: none;
}
要禁用属性设置为(即):textarea
name
foo
<textarea name="foo"></textarea>
textarea[name=foo] {
resize: none;
}
或者,使用属性(即):id
<textarea id="foo"></textarea>
#foo {
resize: none;
}
W3C 页面列出了调整大小限制的可能值:none、both、horizontal、vertical 和 inherit:
textarea {
resize: vertical; /* user can resize vertically, but width is fixed */
}
查看一个不错的兼容性页面,看看目前有哪些浏览器支持此功能。正如 Jon Hulka 所评论的,可以在 CSS 中使用 max-width、max-height、min-width 和 min-height 进一步约束尺寸。
超级重要知道:
除非 overflow 属性不是可见属性,否则此属性不执行任何操作,这是大多数元素的默认值。所以一般来说,要使用它,你必须设置类似 overflow: scroll;
引用 Sara Cope,http://css-tricks.com/almanac/properties/r/resize/
评论
在 CSS 中:
textarea {
resize: none;
}
CSS 3 为 UI 元素提供了一个新属性,允许您执行此操作。该属性是 resize 属性。因此,您可以将以下内容添加到样式表中,以禁用所有 textarea 元素的大小调整:
textarea { resize: none; }
这是一个 CSS 3 属性;使用兼容性图表查看浏览器兼容性。
就个人而言,我会发现在文本区域元素上禁用调整大小非常烦人。这是设计人员试图“破坏”用户客户端的情况之一。如果设计无法容纳较大的文本区域,则可能需要重新考虑设计的工作方式。任何用户都可以添加到他们的用户样式表中以覆盖您的首选项。textarea { resize: both !important; }
评论
texarea
我发现了两件事:
第一
textarea{resize: none}
这是一个尚未发布的 CSS 3,与 Firefox 4(及更高版本)、Chrome 和 Safari 兼容。
另一个格式功能是 overflow: auto
去掉右边的滚动条,同时考虑到 dir 属性。
代码和不同的浏览器
基本 HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<textarea style="overflow:auto;resize:none" rows="13" cols="20"></textarea>
</body>
</html>
某些浏览器
- IE浏览器 8
- 火狐浏览器 17.0.1
- 铬
<textarea style="resize:none" rows="10" placeholder="Enter Text" ></textarea>
添加使其工作:!important
width:325px !important; height:120px !important; outline:none !important;
outline
只是为了避免某些浏览器上的蓝色轮廓。
评论
!important
resize: none
!important
如果你需要深度支持,你可以使用一种老式的技术:
textarea {
max-width: /* desired fixed width */ px;
min-width: /* desired fixed width */ px;
min-height: /* desired fixed height */ px;
max-height: /* desired fixed height */ px;
resize: none; /* If you wnt to hide the handle in the lower right corner */;
}
评论
resize:none
这可以在 HTML 中轻松完成:
<textarea name="textinput" draggable="false"></textarea>
这对我有用。默认值用于属性。true
draggable
评论
CSS 3 可以解决这个问题。不幸的是,现在只有 60% 的二手浏览器支持它。
对于 Internet Explorer 和 iOS,您无法关闭调整大小,但可以通过设置其 和 来限制维度。textarea
width
height
/* One can also turn on/off specific axis. Defaults to both on. */
textarea { resize:vertical; } /* none|horizontal|vertical|both */
若要禁用 resize 属性,请使用以下 CSS 属性:
resize: none;
您可以将其作为内联样式属性应用,如下所示:
<textarea style="resize: none;"></textarea>
或在元素标签之间,如下所示:
<style>...</style>
textarea { resize: none; }
要禁用所有 s 的调整大小:textarea
textarea {
resize: none;
}
要禁用特定 的调整大小,请添加属性或 并将其设置为某些内容。在本例中,它被命名为textarea
name
id
noresize
[HTML全文]
<textarea name='noresize' id='noresize'> </textarea>
CSS的
/* Using the attribute name */
textarea[name=noresize] {
resize: none;
}
/* Or using the id */
#noresize {
resize: none;
}
您也可以尝试使用jQuery
$('textarea').css("resize", "none");
评论
您只需在 CSS 中使用:即可。resize: none;
resize 属性指定元素是否可调整大小 由用户。
注意:resize 属性适用于其计算溢出的元素 value 是“可见”以外的东西。
此外,Internet Explorer 目前不支持调整大小。
以下是用于调整大小的不同属性:
无调整大小:
textarea {
resize: none;
}
以两种方式调整大小(垂直和水平):
textarea {
resize: both;
}
垂直调整大小:
textarea {
resize: vertical;
}
水平调整大小:
textarea {
resize: horizontal;
}
此外,如果您有 CSS 或 HTML,它将阻止您的文本区域调整大小,并提供更广泛的浏览器支持。width
height
您可以像这样简单地禁用 textarea 属性:
textarea {
resize: none;
}
要禁用垂直或水平调整大小,请使用
resize: vertical;
或
resize: horizontal;
使用 ,您可以为其指定自定义大小并禁用调整大小功能。@style
(resize: none;)
@Html.TextAreaFor(model => model.YourProperty, new { @style = "width: 90%; height: 180px; resize: none;" })
评论
我创建了一个小型演示来展示调整属性大小的工作原理。我希望它能对你和其他人有所帮助。
.resizeable {
resize: both;
}
.noResizeable {
resize: none;
}
.resizeable_V {
resize: vertical;
}
.resizeable_H {
resize: horizontal;
}
<textarea class="resizeable" rows="5" cols="20" name="resizeable" title="This is Resizable.">
This is Resizable. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
</textarea>
<textarea class="noResizeable" rows="5" title="This will not Resizable. " cols="20" name="resizeable">
This will not Resizable. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
</textarea>
<textarea class="resizeable_V" title="This is Vertically Resizable." rows="5" cols="20" name="resizeable">
This is Vertically Resizable. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
</textarea>
<textarea class="resizeable_H" title="This is Horizontally Resizable." rows="5" cols="20" name="resizeable">
This is Horizontally Resizable. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book.
</textarea>
textarea {
resize: none;
}
上面的代码将禁用项目中所有元素的 resizable 属性。如果你希望这很好,否则你会想对你的textarea元素使用一个特定的类。<textarea/>
.not-resizable {
resize: none;
}
在 HTML 中
<textarea class="not-resizable"></textarea>
使用此属性resize: none;
textarea {
resize: none;
}
您需要在component.css
textarea {
resize: none;
}
您可以使用 textarea {resize: none;} 禁用 textarea 的可调整大小属性
textarea {
resize: none;
}
如果有人正在寻找一种在纯 Javascript 中实现这一目标的方法:
textArea = document.createElement("textarea");
textArea_input.setAttribute("style","resize:none");
注意:以这种方式设置 style 属性将覆盖之前的所有 css 样式声明。
是的,您可以使用 CSS 禁用 textarea 的 resizable 属性。以下代码将禁用 id 为 text area 的 resizable 属性:my-textarea
CSS代码:-
textarea#my-textarea {
resize: none;
}
您还可以通过将以下代码添加到 CSS 样式表来禁用页面上所有 resizable 属性:textareas
CSS代码:-
textarea {
resize: none;
}
该属性可以有三个值:resize
none
:无法调整文本区域的大小。horizontal
:文本区域只能水平调整大小。vertical
:文本区域只能垂直调整大小。
该属性的缺省值为 。resize
none
除了使用 CSS 之外,您还可以使用 JavaScript 禁用 textarea 的 resizable 属性。以下代码将禁用 id 为 text area 的 resizable 属性:my-textarea
JavaScript代码:-
const textarea = document.getElementById('my-textarea');
textarea.style.resize = 'none';
此代码将覆盖已应用于 textarea 的任何 CSS 样式。
若要减小文本区域的大小,可以在 HTML 代码中使用 and 属性。rows 属性指定 textarea 中的位数,该属性指定列数。例如,以下代码将创建一个包含 10 行和 50 列的 textarea:rows
cols
rows
cols
HTML代码:-
<textarea rows="10" cols="50"></textarea>
您还可以使用 CSS 来设置文本区域的大小。以下代码将 id 为 100px 和 200px 的 textarea 的高度和宽度分别设置为 100px 和 200px:my-textarea
CSS代码:-
textarea#my-textarea {
height: 100px;
width: 200px;
}
评论