The autocapitalize Attribute
The autocapitalize Attribute
Section titled “The autocapitalize Attribute”The autocapitalize attribute controls whether and how text input is automatically capitalized on mobile devices and virtual keyboards. It’s particularly useful for improving the user experience when entering names, sentences, or other text that benefits from automatic capitalization.
Syntax
Section titled “Syntax”<element autocapitalize="value">Content</element>Values
Section titled “Values”| Value | Description | Use Case |
|---|---|---|
off or none | No automatic capitalization | Usernames, email addresses, codes |
on or sentences | Capitalize first letter of each sentence | Comments, messages, paragraphs |
words | Capitalize first letter of each word | Names, titles, addresses |
characters | Capitalize all characters | Acronyms, codes, license plates |
Examples
Section titled “Examples”Form Input Examples
Section titled “Form Input Examples” Result
Contact Form with Smart Capitalization
Section titled “Contact Form with Smart Capitalization” Result
Address Form
Section titled “Address Form” Result
Social Media Post
Section titled “Social Media Post” Result
How It Works
Section titled “How It Works”Mobile Keyboard Behavior
Section titled “Mobile Keyboard Behavior”On mobile devices with virtual keyboards:
none/off: Lowercase keyboard, no shiftsentences: First letter capitalized, then lowercasewords: Shift after each spacecharacters: Caps lock active
Desktop Behavior
Section titled “Desktop Behavior”On desktop browsers:
- The attribute is typically ignored
- Users control capitalization manually
- No automatic capitalization occurs
Best Practices
Section titled “Best Practices”1. Match Field Purpose
Section titled “1. Match Field Purpose”Choose the right value based on expected input:
<!-- Personal names: capitalize each word --><input type="text" name="fullName" autocapitalize="words">
<!-- Email: no capitalization --><input type="email" autocapitalize="none">
<!-- Comments/messages: sentence case --><textarea autocapitalize="sentences"></textarea>
<!-- Codes/IDs: all caps --><input type="text" name="productCode" autocapitalize="characters">
<!-- Usernames: no capitalization --><input type="text" name="username" autocapitalize="off">2. Combine with Other Input Attributes
Section titled “2. Combine with Other Input Attributes”<input type="text" autocapitalize="none" autocorrect="off" spellcheck="false" placeholder="username">3. Consider User Experience
Section titled “3. Consider User Experience”<!-- Good: Street address --><input type="text" name="street" autocapitalize="words" placeholder="123 Main Street">
<!-- Good: City name --><input type="text" name="city" autocapitalize="words" placeholder="San Francisco">
<!-- Good: State abbreviation --><input type="text" name="state" autocapitalize="characters" maxlength="2" placeholder="CA">4. Use with Input Types
Section titled “4. Use with Input Types”<!-- Email: Always use autocapitalize="none" --><input type="email" autocapitalize="none">
<!-- URL: No capitalization needed --><input type="url" autocapitalize="none">
<!-- Search: Depends on use case --><input type="search" autocapitalize="sentences">
<!-- Tel: Not affected --><input type="tel">
<!-- Password: Usually autocapitalize="none" --><input type="password" autocapitalize="none">5. Contenteditable Elements
Section titled “5. Contenteditable Elements”<!-- Works on contenteditable too --><div contenteditable="true" autocapitalize="sentences"> Type your content here...</div>Common Use Cases
Section titled “Common Use Cases”Names and Titles
Section titled “Names and Titles”<input type="text" name="firstName" autocapitalize="words" placeholder="John"><input type="text" name="lastName" autocapitalize="words" placeholder="Smith"><input type="text" name="jobTitle" autocapitalize="words" placeholder="Software Engineer">Addresses
Section titled “Addresses”<input type="text" name="street" autocapitalize="words" placeholder="Main Street"><input type="text" name="city" autocapitalize="words" placeholder="New York"><input type="text" name="state" autocapitalize="characters" placeholder="NY"><input type="text" name="zipCode" autocapitalize="characters">Credentials
Section titled “Credentials”<input type="text" name="username" autocapitalize="none" placeholder="johndoe123"><input type="password" name="password" autocapitalize="none">Text Content
Section titled “Text Content”<!-- Blog post --><textarea name="content" autocapitalize="sentences"></textarea>
<!-- Comment --><textarea name="comment" autocapitalize="sentences" placeholder="Share your thoughts..."></textarea>
<!-- Review --><textarea name="review" autocapitalize="sentences" placeholder="Write your review..."></textarea>Codes and IDs
Section titled “Codes and IDs”<input type="text" name="promocode" autocapitalize="characters" placeholder="SAVE20"><input type="text" name="licensekey" autocapitalize="characters"><input type="text" name="confirmationCode" autocapitalize="characters">JavaScript API
Section titled “JavaScript API”Setting Dynamically
Section titled “Setting Dynamically”const input = document.getElementById('myInput');
// Set autocapitalizeinput.autocapitalize = 'words';input.setAttribute('autocapitalize', 'words');
// Get current valueconsole.log(input.autocapitalize); // "words"
// Removeinput.removeAttribute('autocapitalize');Conditional Capitalization
Section titled “Conditional Capitalization”// Set based on input typeconst emailInputs = document.querySelectorAll('input[type="email"]');emailInputs.forEach(input => { input.autocapitalize = 'none';});
// Set based on field nameconst nameFields = document.querySelectorAll('input[name*="name"]');nameFields.forEach(input => { input.autocapitalize = 'words';});Accessibility Considerations
Section titled “Accessibility Considerations”Screen Readers
Section titled “Screen Readers”The autocapitalize attribute:
- Does not affect screen reader behavior
- Is purely a keyboard input helper
- Should be combined with proper labels and ARIA
<label for="fullName">Full Name</label><input type="text" id="fullName" autocapitalize="words" aria-required="true" aria-describedby="nameHint"><span id="nameHint">Enter your first and last name</span>User Control
Section titled “User Control”Users can still:
- Override with Caps Lock
- Type lowercase if desired
- Use mobile keyboard’s shift key manually
Common Pitfalls
Section titled “Common Pitfalls”Type Attribute Interactions
Section titled “Type Attribute Interactions”<!-- Email type usually overrides autocapitalize --><input type="email" autocapitalize="words"><!-- Browser likely ignores autocapitalize for email -->
<!-- Password type may ignore autocapitalize --><input type="password" autocapitalize="sentences"><!-- Capitalization may not work -->Browser Support
Section titled “Browser Support”| Browser | Support | Notes |
|---|---|---|
| iOS Safari | 5+ | Full support |
| Android Chrome | All | Full support |
| Desktop Chrome | Ignored | No effect |
| Desktop Firefox | Ignored | No effect |
| Desktop Safari | Ignored | No effect |
Related Attributes
Section titled “Related Attributes”autocorrect- Enable/disable autocorrection (non-standard, iOS only)spellcheck- Control spell checkinginputmode- Hint for virtual keyboard typeenterkeyhint- Label for Enter key on virtual keyboards