input-group.md 13.1 KB
Newer Older
Mark Otto's avatar
Mark Otto committed
1
---
Mark Otto's avatar
Mark Otto committed
2
layout: docs
Mark Otto's avatar
Mark Otto committed
3
title: Input group
Mark Otto's avatar
clarify    
Mark Otto committed
4
description: Easily extend form controls by adding text, buttons, or button groups on either side of textual inputs, custom selects, and custom file inputs.
5
group: components
Mark Otto's avatar
Mark Otto committed
6
toc: true
Mark Otto's avatar
Mark Otto committed
7
8
---

Mark Otto's avatar
Mark Otto committed
9
## Basic example
Mark Otto's avatar
Mark Otto committed
10

11
Place one add-on or button on either side of an input. You may also place one on both sides of an input. **We do not support multiple form-controls in a single input group** and `<label>`s must come outside the input group.
Mark Otto's avatar
Mark Otto committed
12

m5o's avatar
m5o committed
13
{% capture example %}
14
15
16
17
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon1">@</span>
  </div>
18
  <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
Mark Otto's avatar
Mark Otto committed
19
</div>
20
21

<div class="input-group mb-3">
22
  <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2">
23
24
25
  <div class="input-group-append">
    <span class="input-group-text" id="basic-addon2">@example.com</span>
  </div>
Mark Otto's avatar
Mark Otto committed
26
</div>
27

Mark Otto's avatar
Mark Otto committed
28
<label for="basic-url">Your vanity URL</label>
29
30
31
32
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon3">https://example.com/users/</span>
  </div>
Mark Otto's avatar
Mark Otto committed
33
34
  <input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
35
36
37
38
39

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text">$</span>
  </div>
40
  <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
41
42
43
  <div class="input-group-append">
    <span class="input-group-text">.00</span>
  </div>
44
</div>
Mark Otto's avatar
Mark Otto committed
45
46
47
48
49
50
51

<div class="input-group">
  <div class="input-group-prepend">
    <span class="input-group-text">With textarea</span>
  </div>
  <textarea class="form-control" aria-label="With textarea"></textarea>
</div>
m5o's avatar
m5o committed
52
53
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
54

Mark Otto's avatar
Mark Otto committed
55
56
57
58
## Sizing

Add the relative form sizing classes to the `.input-group` itself and contents within will automatically resize—no need for repeating the form control size classes on each element.

59
60
**Sizing on the individual input group elements isn't supported.**

m5o's avatar
m5o committed
61
{% capture example %}
62
63
64
65
66
67
68
69
70
71
72
73
<div class="input-group input-group-sm mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroup-sizing-sm">Small</span>
  </div>
  <input type="text" class="form-control" aria-label="Small" aria-describedby="inputGroup-sizing-sm">
</div>

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroup-sizing-default">Default</span>
  </div>
  <input type="text" class="form-control" aria-label="Default" aria-describedby="inputGroup-sizing-default">
Mark Otto's avatar
Mark Otto committed
74
</div>
75
76
77
78
79
80

<div class="input-group input-group-lg">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroup-sizing-lg">Large</span>
  </div>
  <input type="text" class="form-control" aria-label="Large" aria-describedby="inputGroup-sizing-sm">
Mark Otto's avatar
Mark Otto committed
81
</div>
m5o's avatar
m5o committed
82
83
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
84

85
## Checkboxes and radios
Mark Otto's avatar
Mark Otto committed
86

Mark Otto's avatar
Mark Otto committed
87
Place any checkbox or radio option within an input group's addon instead of text.
Mark Otto's avatar
Mark Otto committed
88

m5o's avatar
m5o committed
89
{% capture example %}
90
91
92
93
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <div class="input-group-text">
      <input type="checkbox" aria-label="Checkbox for following text input">
Mark Otto's avatar
Mark Otto committed
94
95
    </div>
  </div>
96
97
98
99
100
101
102
  <input type="text" class="form-control" aria-label="Text input with checkbox">
</div>

<div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
    <input type="radio" aria-label="Radio button for following text input">
Mark Otto's avatar
Mark Otto committed
103
104
    </div>
  </div>
105
106
  <input type="text" class="form-control" aria-label="Text input with radio button">
</div>
m5o's avatar
m5o committed
107
108
{% endcapture %}
{% include example.html content=example %}
109
110
111
112
113

## Multiple inputs

While multiple `<input>`s are supported visually, validation styles are only available for input groups with a single `<input>`.

m5o's avatar
m5o committed
114
{% capture example %}
115
116
117
118
119
120
<div class="input-group">
  <div class="input-group-prepend">
    <span class="input-group-text" id="">First and last name</span>
  </div>
  <input type="text" class="form-control">
  <input type="text" class="form-control">
Mark Otto's avatar
Mark Otto committed
121
</div>
m5o's avatar
m5o committed
122
123
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
124

125
126
127
128
## Multiple addons

Multiple add-ons are supported and can be mixed with checkbox and radio input versions.

m5o's avatar
m5o committed
129
{% capture example %}
130
131
132
133
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text">$</span>
    <span class="input-group-text">0.00</span>
134
  </div>
135
136
137
138
139
140
141
142
  <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
</div>

<div class="input-group">
  <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)">
  <div class="input-group-append">
    <span class="input-group-text">$</span>
    <span class="input-group-text">0.00</span>
143
144
  </div>
</div>
m5o's avatar
m5o committed
145
146
{% endcapture %}
{% include example.html content=example %}
147

Mark Otto's avatar
Mark Otto committed
148
## Button addons
Mark Otto's avatar
Mark Otto committed
149

m5o's avatar
m5o committed
150
{% capture example %}
151
152
153
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary" type="button">Button</button>
Mark Otto's avatar
Mark Otto committed
154
  </div>
155
156
157
158
159
160
161
  <input type="text" class="form-control" placeholder="" aria-label="" aria-describedby="basic-addon1">
</div>

<div class="input-group mb-3">
  <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2">
  <div class="input-group-append">
    <button class="btn btn-outline-secondary" type="button">Button</button>
Mark Otto's avatar
Mark Otto committed
162
163
  </div>
</div>
164
165
166
167
168
169
170
171
172
173
174
175
176
177

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary" type="button">Button</button>
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
  <input type="text" class="form-control" placeholder="" aria-label="" aria-describedby="basic-addon1">
</div>

<div class="input-group">
  <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="basic-addon2">
  <div class="input-group-append">
    <button class="btn btn-outline-secondary" type="button">Button</button>
    <button class="btn btn-outline-secondary" type="button">Button</button>
178
179
  </div>
</div>
m5o's avatar
m5o committed
180
181
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
182

Mark Otto's avatar
Mark Otto committed
183
184
## Buttons with dropdowns

m5o's avatar
m5o committed
185
{% capture example %}
186
187
188
189
190
191
192
193
194
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <div role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
Mark Otto's avatar
Mark Otto committed
195
196
    </div>
  </div>
197
198
199
200
201
202
203
204
205
206
207
208
209
  <input type="text" class="form-control" aria-label="Text input with dropdown button">
</div>

<div class="input-group">
  <input type="text" class="form-control" aria-label="Text input with dropdown button">
  <div class="input-group-append">
    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <div role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
Mark Otto's avatar
Mark Otto committed
210
    </div>
Mark Otto's avatar
Mark Otto committed
211
212
  </div>
</div>
m5o's avatar
m5o committed
213
214
{% endcapture %}
{% include example.html content=example %}
Mark Otto's avatar
Mark Otto committed
215

Mark Otto's avatar
Mark Otto committed
216
217
## Segmented buttons

m5o's avatar
m5o committed
218
{% capture example %}
219
220
221
222
223
224
225
226
227
228
229
230
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <button type="button" class="btn btn-outline-secondary">Action</button>
    <button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <span class="sr-only">Toggle Dropdown</span>
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <div role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
Mark Otto's avatar
Mark Otto committed
231
232
    </div>
  </div>
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  <input type="text" class="form-control" aria-label="Text input with segmented dropdown button">
</div>

<div class="input-group">
  <input type="text" class="form-control" aria-label="Text input with segmented dropdown button">
  <div class="input-group-append">
    <button type="button" class="btn btn-outline-secondary">Action</button>
    <button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      <span class="sr-only">Toggle Dropdown</span>
    </button>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <div role="separator" class="dropdown-divider"></div>
      <a class="dropdown-item" href="#">Separated link</a>
Mark Otto's avatar
Mark Otto committed
249
    </div>
Mark Otto's avatar
Mark Otto committed
250
251
  </div>
</div>
m5o's avatar
m5o committed
252
253
{% endcapture %}
{% include example.html content=example %}
254

255
256
257
258
259
260
## Custom forms

Input groups include support for custom selects and custom file inputs. Browser default versions of these are not supported.

### Custom select

m5o's avatar
m5o committed
261
{% capture example %}
262
<div class="input-group mb-3">
263
264
265
  <div class="input-group-prepend">
    <label class="input-group-text" for="inputGroupSelect01">Options</label>
  </div>
266
  <select class="custom-select" id="inputGroupSelect01">
267
268
269
270
271
272
273
274
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</div>

<div class="input-group mb-3">
275
  <select class="custom-select" id="inputGroupSelect02">
276
277
278
279
280
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
281
282
283
  <div class="input-group-append">
    <label class="input-group-text" for="inputGroupSelect02">Options</label>
  </div>
284
285
286
</div>

<div class="input-group mb-3">
287
288
289
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
290
  <select class="custom-select" id="inputGroupSelect03">
291
292
293
294
295
296
297
298
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</div>

<div class="input-group">
299
  <select class="custom-select" id="inputGroupSelect04">
300
301
302
303
304
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
305
306
307
  <div class="input-group-append">
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
308
</div>
m5o's avatar
m5o committed
309
310
{% endcapture %}
{% include example.html content=example %}
311
312
313

### Custom file input

m5o's avatar
m5o committed
314
{% capture example %}
315
<div class="input-group mb-3">
316
317
318
  <div class="input-group-prepend">
    <span class="input-group-text">Upload</span>
  </div>
ysds's avatar
ysds committed
319
320
321
322
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile01">
    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
  </div>
323
324
325
</div>

<div class="input-group mb-3">
ysds's avatar
ysds committed
326
327
328
329
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile02">
    <label class="custom-file-label" for="inputGroupFile02">Choose file</label>
  </div>
330
331
332
  <div class="input-group-append">
    <span class="input-group-text" id="">Upload</span>
  </div>
333
334
335
</div>

<div class="input-group mb-3">
336
337
338
  <div class="input-group-prepend">
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
ysds's avatar
ysds committed
339
340
341
342
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile03">
    <label class="custom-file-label" for="inputGroupFile03">Choose file</label>
  </div>
343
344
345
</div>

<div class="input-group">
ysds's avatar
ysds committed
346
347
348
349
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile04">
    <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
  </div>
350
351
352
  <div class="input-group-append">
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
353
</div>
m5o's avatar
m5o committed
354
355
{% endcapture %}
{% include example.html content=example %}
356

357
358
359
360
## Accessibility

Screen readers will have trouble with your forms if you don't include a label for every input. For these input groups, ensure that any additional label or functionality is conveyed to assistive technologies.

361
The exact technique to be used (`<label>` elements hidden using the `.sr-only` class, or use of the `aria-label` and `aria-labelledby` attributes, possibly in combination with `aria-describedby`) and what additional information will need to be conveyed will vary depending on the exact type of interface widget you're implementing. The examples in this section provide a few suggested, case-specific approaches.