size

The size attribute in HTML determines the visible width of an input field or the number of options shown in a select menu, affecting how much content is displayed.

size attribute

The size attribute controls the width of <input> elements and the height of <select>elements.

Syntax

index.html
<tagname size="number">

Example





index.html
<label for="username">Name:</label><br>
  <input type="text" id="name" size="40" placeholder="Enter your name"><br><br>
<label for="username">Username:</label><br>
  <input type="text" id="username" size="20" placeholder="Enter your username">

Values

  • number
    • For an input element, it sets the width based on the number of characters. For a select element, it determines how many options are visible at once.

Key Points

  • For <input>: Sets the number of characters visible for text inputs.
  • For <select>: Determines how many options are visible at once.
  • Values: Must be a positive integer.
  • Default: No size specified means default width/height, which CSS can override.
  • Constraint Validation: No effect on validation.

See also

Conclusion

The size attribute controls the visible dimensions of input fields and select menus. It sets the width for input elements based on character count and determines how many options are visible for select menus. While it doesn’t affect validation, it helps manage content display.