|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +# 🙋🏻 HTML Input Attributes |
| 6 | + |
| 7 | +HTML input attributes help refine and control user input by adding constraints and additional behaviors to form fields. This guide provides an overview of essential input attributes and their practical applications. |
| 8 | + |
| 9 | +## 🏷 Commonly Used Input Attributes |
| 10 | + |
| 11 | +### 1. `placeholder` |
| 12 | +Provides a short hint that describes the expected value of the input field. |
| 13 | + |
| 14 | +```html title="placeholder-example.html" |
| 15 | +<input type="text" placeholder="Enter your name"> |
| 16 | +``` |
| 17 | + |
| 18 | +<BrowserWindow url="http://.../placeholder-example.html" minHeight="300px"> |
| 19 | + <input type="text" placeholder="Enter your name" /> |
| 20 | +</BrowserWindow> |
| 21 | + |
| 22 | +### 2. `required` |
| 23 | +Specifies that the input field must be filled out before submitting the form. |
| 24 | + |
| 25 | +```html title="required-example.html" |
| 26 | +<input type="email" required> |
| 27 | +``` |
| 28 | + |
| 29 | +### 3. `disabled` |
| 30 | +Disables the input field, preventing user interaction. |
| 31 | + |
| 32 | +```html title="disabled-example.html" |
| 33 | +<input type="text" value="Read-only field" disabled> |
| 34 | +``` |
| 35 | + |
| 36 | +<BrowserWindow url="http://.../disabled-example.html" minHeight="300px"> |
| 37 | + <input type="text" value="Read-only field" disabled /> |
| 38 | +</BrowserWindow> |
| 39 | + |
| 40 | +### 4. `readonly` |
| 41 | +Makes the input field non-editable, but its value can still be copied. |
| 42 | + |
| 43 | +```html title="readonly-example.html" |
| 44 | +<input type="text" value="This is readonly" readonly> |
| 45 | +``` |
| 46 | + |
| 47 | +<BrowserWindow url="http://.../readonly-example.html" minHeight="300px"> |
| 48 | + <input type="text" value="This is readonly" readonly /> |
| 49 | +</BrowserWindow> |
| 50 | + |
| 51 | +### 5. `maxlength` |
| 52 | +Limits the number of characters allowed in the input field. |
| 53 | + |
| 54 | +```html title="maxlength-example.html" |
| 55 | +<input type="text" maxlength="10" placeholder="Max 10 characters"> |
| 56 | +``` |
| 57 | + |
| 58 | +<BrowserWindow url="http://.../maxlength-example.html" minHeight="300px"> |
| 59 | + <input type="text" maxlength="10" placeholder="Max 10 characters" /> |
| 60 | +</BrowserWindow> |
| 61 | + |
| 62 | +### 6. `min` and `max` |
| 63 | +Defines the minimum and maximum values for numeric, date, or range input types. |
| 64 | + |
| 65 | +```html title="min-max-example.html" |
| 66 | +<input type="number" min="1" max="100"> |
| 67 | +<input type="date" min="2023-01-01" max="2024-12-31"> |
| 68 | +``` |
| 69 | + |
| 70 | +<BrowserWindow url="http://.../min-max-example.html" minHeight="300px"> |
| 71 | + <> |
| 72 | + <input type="number" min="1" max="100" /> |
| 73 | + <input type="date" min="2023-01-01" max="2024-12-31" /> |
| 74 | + </> |
| 75 | +</BrowserWindow> |
| 76 | + |
| 77 | +### 7. `pattern` |
| 78 | +Specifies a regular expression that the input value must match. |
| 79 | + |
| 80 | +```html title="pattern-example.html" |
| 81 | +<input type="text" pattern="[A-Za-z]{3,}" title="Enter at least 3 letters"> |
| 82 | +``` |
| 83 | + |
| 84 | +<BrowserWindow url="http://.../pattern-example.html" minHeight="300px"> |
| 85 | + <input type="text" pattern="[A-Za-z]{3,}" title="Enter at least 3 letters" /> |
| 86 | +</BrowserWindow> |
| 87 | + |
| 88 | +### 8. `autofocus` |
| 89 | +Automatically focuses the input field when the page loads. |
| 90 | + |
| 91 | +```html title="autofocus-example.html" |
| 92 | +<input type="text" autofocus> |
| 93 | +``` |
| 94 | + |
| 95 | +<BrowserWindow url="http://.../autofocus-example.html" minHeight="300px"> |
| 96 | + <input type="text" autofocus /> |
| 97 | +</BrowserWindow> |
| 98 | + |
| 99 | +### 9. `step` |
| 100 | +Defines the increment step for `number`, `date`, `time`, and `range` input types. |
| 101 | + |
| 102 | +```html title="step-example.html" |
| 103 | +<input type="number" min="0" max="100" step="5"> |
| 104 | +``` |
| 105 | + |
| 106 | +<BrowserWindow url="http://.../step-example.html" minHeight="300px"> |
| 107 | + <input type="number" min="0" max="100" step="5" /> |
| 108 | +</BrowserWindow> |
| 109 | + |
| 110 | +## 🛠 Advanced Input Attributes |
| 111 | + |
| 112 | +### 1. `list` |
| 113 | +Specifies a list of predefined options suggested to the user while typing. This attribute works with the `<datalist>` element. |
| 114 | + |
| 115 | +```html title="list-datalist-example.html" |
| 116 | +<input type="text" list="browsers" placeholder="Choose a browser"> |
| 117 | +<datalist id="browsers"> |
| 118 | + <option value="Chrome"> |
| 119 | + <option value="Firefox"> |
| 120 | + <option value="Safari"> |
| 121 | + <option value="Edge"> |
| 122 | +</datalist> |
| 123 | +``` |
| 124 | + |
| 125 | +<BrowserWindow url="http://.../list-datalist-example.html" minHeight="300px"> |
| 126 | + <> |
| 127 | + <input type="text" list="browsers" placeholder="Choose a browser" /> |
| 128 | + <datalist id="browsers"> |
| 129 | + <option value="Chrome" /> |
| 130 | + <option value="Firefox" /> |
| 131 | + <option value="Safari" /> |
| 132 | + <option value="Edge" /> |
| 133 | + </datalist> |
| 134 | + </> |
| 135 | +</BrowserWindow> |
| 136 | + |
| 137 | +### 2. `multiple` |
| 138 | +Allows the user to select multiple options in input types like `email` and `file`. |
| 139 | + |
| 140 | +```html title="multiple-example.html" |
| 141 | +<input type="email" multiple placeholder="Enter multiple emails"> |
| 142 | +<input type="file" multiple> |
| 143 | +``` |
| 144 | + |
| 145 | +<BrowserWindow url="http://.../multiple-example.html" minHeight="300px"> |
| 146 | + <> |
| 147 | + <input type="email" multiple placeholder="Enter multiple emails" /> |
| 148 | + <input type="file" multiple /> |
| 149 | + </> |
| 150 | +</BrowserWindow> |
| 151 | + |
| 152 | +### 3. `size` |
| 153 | +Sets the width of the input field in terms of character count. |
| 154 | + |
| 155 | +```html title="size-example.html" |
| 156 | +<input type="text" size="30"> |
| 157 | +``` |
| 158 | + |
| 159 | +<BrowserWindow url="http://.../size-example.html" minHeight="300px"> |
| 160 | + <input type="text" size="30" /> |
| 161 | +</BrowserWindow> |
| 162 | + |
| 163 | +## 📝 Summary |
| 164 | + |
| 165 | +Using the right input attributes in your HTML forms ensures better user experience, validation, and input control. Understanding these attributes allows developers to create more interactive and reliable web forms. |
0 commit comments