HTML <input> width 属性

定义和用法

width 属性规定 <input> 元素的宽度。

注意:width 属性仅与 <input type="image"> 一起使用。

提示:为图片指定高度和宽度是一个好习惯。如果设置了这些属性,当页面加载时会为图片预留需要的空间。而如果没有这些属性,则浏览器就无法了解图像的尺寸,也就无法为其预留合适的空间。情况是当页面和图片加载时,页面布局会发生变化。

实例

定义图像作为提交按钮,带有 height 和 width 属性:

<form action="/action_page.php">
  <label for="fname">名字:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Surname:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="image" src="img_submit.gif" alt="submit" width="48" height="48">
</form>

Try it yourself

Syntax

<input width="pixels">

Attribute value

Value Description
pixels Specifies the width of the <input> element in pixels (e.g., height="100").

Browser support

The numbers in the table indicate the first browser version that fully supports this attribute.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support 16.0 Support Support

Note:width The attribute is a new attribute of the <input> tag in HTML5.