How do we add the width in characters for in HTML?

The size attribute in HTML input elements specifies the visible width of an input field in terms of characters. This attribute controls how wide the input field appears on the screen, making it useful for creating forms with appropriately sized input fields based on expected content length.

The size attribute works with text-based input types including text, search, tel, url, email, and password. While it affects the display width, it does not limit the actual number of characters a user can enter − that requires the maxlength attribute.

Syntax

Following is the syntax for the size attribute in HTML input elements −

<input type="text" size="number">

Where number represents the width of the input field in characters. The default size is typically 20 characters if not specified.

Basic Usage of Size Attribute

Example − Different Input Field Sizes

In the following example, we demonstrate different input field sizes −

<!DOCTYPE html>
<html>
<head>
   <title>Input Size Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Input Field Sizes</h2>
   <p>First Name: <input type="text" size="15" value="John"></p>
   <p>Last Name: <input type="text" size="25" value="Doe"></p>
   <p>Default Size: <input type="text" value="Default width"></p>
</body>
</html>

The output displays input fields with different widths based on their size values −

Input Field Sizes

First Name: [John          ]  (15 characters wide)
Last Name:  [Doe                    ]  (25 characters wide)
Default Size: [Default width            ]  (default width)

Using Size with Form Elements

Example − Form with Placeholder and Size

Consider the following example using placeholder text with a specific size −

<!DOCTYPE html>
<html>
<head>
   <title>Form with Size Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Registration Form</h2>
   <form>
      <div style="margin-bottom: 10px;">
         <label for="fullName">Enter Full Name: </label>
         <input type="text" id="fullName" name="fullName" 
                placeholder="Enter your complete name" size="30">
      </div>
      <div style="margin-bottom: 10px;">
         <label for="email">Email Address: </label>
         <input type="email" id="email" name="email" 
                placeholder="your.email@example.com" size="35">
      </div>
      <div>
         <button type="submit">Submit</button>
      </div>
   </form>
</body>
</html>

The output shows a form with input fields sized appropriately for their expected content −

Registration Form

Enter Full Name: [Enter your complete name          ]  (30 chars)
Email Address:   [your.email@example.com             ]  (35 chars)
                 [Submit]

Combining Size with Length Constraints

Example − Size with Min and Max Length

The following example demonstrates using size with minlength and maxlength attributes −

<!DOCTYPE html>
<html>
<head>
   <title>Size with Length Validation</title>
   <style>
      .form-group { margin-bottom: 15px; }
      label { display: inline-block; width: 120px; }
      input:invalid { border: 2px solid red; }
      input:valid { border: 2px solid green; }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>User Registration</h2>
   <form>
      <div class="form-group">
         <label for="username">Username: </label>
         <input type="text" id="username" name="username" 
                required size="15" minlength="4" maxlength="12"
                placeholder="4-12 characters">
      </div>
      <div class="form-group">
         <label for="password">Password: </label>
         <input type="password" id="password" name="password" 
                required size="20" minlength="6" maxlength="20"
                placeholder="6-20 characters">
      </div>
      <div class="form-group">
         <label for="phone">Phone: </label>
         <input type="tel" id="phone" name="phone" 
                size="12" maxlength="10"
                placeholder="10 digits">
      </div>
      <div>
         <button type="submit">Register</button>
      </div>
   </form>
</body>
</html>

This form shows how size works with validation − the input fields have appropriate visual width while enforcing character limits −

User Registration

Username: [4-12 characters ]  (15 chars wide, 4-12 limit)
Password: [6-20 characters      ]  (20 chars wide, 6-20 limit)  
Phone:    [10 digits  ]  (12 chars wide, 10 char limit)
          [Register]
Size vs Length Attributes Comparison size attribute Controls visual width Measured in characters Does NOT limit input Default: ~20 characters Example: size="15" maxlength attribute Limits actual input length Measured in characters Does NOT affect width No default limit Example: maxlength="50"

Different Input Types with Size

The size attribute works with various input types. Following table shows the compatibility −

Input Type Size Attribute Support Common Use Case
text Yes General text input fields
email Yes Email address fields (size="30")
password Yes Password fields (size="15-20")
search Yes Search boxes (size="25")
tel Yes Phone number fields (size="12")
url Yes Website URL fields (size="40")
number No Use CSS width instead
date No Fixed browser-specific width

Example − Multiple Input Types

Following example demonstrates the size attribute with different input types −

<!DOCTYPE html>
<html>
<head>
   <title>Size with Different Input Types</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Contact Form</h2>
   <form>
      <p><label>Search: </label>
         <input type="search" size="25" placeholder="Search products..."></p>
      <p><label>Website: </label>
         <input type="url" size="40" placeholder="https://www.example.com"></p>
      <p><label>Phone: </label>
         <input type="tel" size="12" placeholder="123-456-7890"></p>
      <p><label>Email: </label>
         <input type="email" size="30" placeholder="user@domain.com"></p>
      <p><button type="submit">Submit</button></p>
   </form>
</body>
</html>

Each input type is sized appropriately for its expected content length −

Contact Form

Search:  [Search products...         ]  (25 chars)
Website: [https://www.example.com                 ]  (40 chars)
Phone:   [123-456-7890]  (12 chars)
Email:   [user@domain.com               ]  (30 chars)
         [Submit]

Conclusion

The size attribute in HTML input elements controls the visual width of text-based input fields in character units. While it affects display width, it does not limit input length − use maxlength for that purpose. Choose size values based on expected content length to create user-friendly forms with appropriately sized input fields.

Updated on: 2026-03-16T21:38:53+05:30

464 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements