The size attribute is used to specify the visible width of an input field in terms of characters. It helps control the display size of form elements.
- Defines the visible width of text input fields.
- The width is measured in character units, not pixels.
- In <select> tags, it specifies the number of visible options.
Syntax:
<input size="number"> Attribute values: It contains the numeric value which specifies the width of an input field in terms of the number of characters. Its default value is 20.
Example: Implementation of HTML <input> size Attribute.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>HTML input size Attribute</title>
</head>
<!--Driver Code Ends-->
<body>
<input type="text"
value="This is the default size.">
<br>
<br>
<input type="text"
size="50"
value="This is user specified size with value equals 50">
</body>
<!--Driver Code Starts-->
</html>
<!--Driver Code Ends-->
- The first <input> element lacks a size attribute, so it displays with the default width for text input fields.
- The second <input> element has size="50", making it wider, accommodating up to 50 characters within the visible area.
- Demonstrates how the size attribute affects the width of input fields, allowing users to input text comfortably based on the specified size.
HTML <input> size Attribute Use Cases
- Width Control in Text Inputs: The size attribute specifies the visible width of an <input type="text"> field in terms of characters.
- Input Field Sizing in HTML5: It helps define the display width of text input elements without using CSS.
- Visible Options in <select> Tags: In <select> elements, the size attribute determines how many options are visible at once.