HTML - cols 属性



HTML 的cols属性用于设置或指定文本区域元素的可见宽度。如果它用在文本区域元素中。

如果我们没有为 cols 属性指定任何值,它将自动考虑默认值 20。除了 cols 属性外,我们还可以使用 CSS width 属性来设置此元素的可见宽度。

cols 属性与<textarea>和<frameset>标签一起使用,由于<frameset>标签在 HTML5 中不受支持,因此我们只将其与<textarea>一起使用。

语法

<textarea cols = "value"></textarea>

应用于

下面列出的元素允许使用 HTML cols 属性

元素 描述
<textarea> HTML <textarea>标签用于输入多行文本。
<frameset> HTML <frameset>标签用于指定框架集中行和列的数量及其像素。

HTML cols 属性示例

以下代码演示了 cols 属性的使用

指定宽度的 cols 属性

在以下示例中,我们在文本区域字段中使用 HTML “cols” 属性来指定此元素的可见宽度。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'cols' attribute</title>
</head>

<body>
   <!--HTML 'cols' attribute-->
   <p>Example of the HTML 'cols' attribute</p>
   <form>
      <label>Message: </label>
      <br>
      <textarea 
         cols="30" 
         rows="10" 
         placeholder="Write your message...">
      </textarea>
      <br>
      <button>Submit</button>
   </form>
</body>

</html>

具有默认值的 cols 属性

在此示例中,我们在文本区域元素中使用“cols”属性,但我们没有为其分配任何值。但它将在网页上显示 20 cols 值(即默认值)。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'cols' attribute</title>
</head>

<body>
   <!--HTML 'cols' attribute-->
   <p>Example of the HTML 'cols' attribute</p>
   <form>
      <label>Address</label>
      <br>
      <textarea cols="" rows="5"></textarea>
      <br>
      <button>Submit</button>
   </form>
</body>

</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
cols
html_attributes_reference.htm
广告

© . All rights reserved.