input-group.md 13.7 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. Remember to place `<label>`s 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
<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>
66
  <input type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm">
67
68
69
70
71
72
</div>

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

<div class="input-group input-group-lg">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroup-sizing-lg">Large</span>
  </div>
80
  <input type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg">
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
  <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">
102
      <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
<div class="input-group">
  <div class="input-group-prepend">
117
    <span class="input-group-text">First and last name</span>
118
  </div>
119
120
  <input type="text" aria-label="First name" class="form-control">
  <input type="text" aria-label="Last name" 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
  <input type="text" class="form-control" aria-label="Dollar amount (with dot and two decimal places)">
136
137
138
</div>

<div class="input-group">
139
  <input type="text" class="form-control" aria-label="Dollar amount (with dot and two decimal places)">
140
141
142
  <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
<div class="input-group mb-3">
  <div class="input-group-prepend">
153
    <button class="btn btn-outline-secondary" type="button" id="button-addon1">Button</button>
Mark Otto's avatar
Mark Otto committed
154
  </div>
155
  <input type="text" class="form-control" placeholder="" aria-label="Example text with button addon" aria-describedby="button-addon1">
156
157
158
</div>

<div class="input-group mb-3">
159
  <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username" aria-describedby="button-addon2">
160
  <div class="input-group-append">
161
    <button class="btn btn-outline-secondary" type="button" id="button-addon2">Button</button>
Mark Otto's avatar
Mark Otto committed
162
163
  </div>
</div>
164
165

<div class="input-group mb-3">
166
  <div class="input-group-prepend" id="button-addon3">
167
168
169
    <button class="btn btn-outline-secondary" type="button">Button</button>
    <button class="btn btn-outline-secondary" type="button">Button</button>
  </div>
170
  <input type="text" class="form-control" placeholder="" aria-label="Example text with two button addons" aria-describedby="button-addon3">
171
172
173
</div>

<div class="input-group">
174
175
  <input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username with two button addons" aria-describedby="button-addon4">
  <div class="input-group-append" id="button-addon4">
176
177
    <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" aria-label="Example select with button addon">
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" aria-label="Example select with button addon">
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
  <div class="input-group-prepend">
317
    <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
318
  </div>
ysds's avatar
ysds committed
319
  <div class="custom-file">
320
    <input type="file" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
ysds's avatar
ysds committed
321
322
    <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
  <div class="custom-file">
    <input type="file" class="custom-file-input" id="inputGroupFile02">
328
    <label class="custom-file-label" for="inputGroupFile02" aria-describedby="inputGroupFileAddon02">Choose file</label>
ysds's avatar
ysds committed
329
  </div>
330
  <div class="input-group-append">
331
    <span class="input-group-text" id="inputGroupFileAddon02">Upload</span>
332
  </div>
333
334
335
</div>

<div class="input-group mb-3">
336
  <div class="input-group-prepend">
337
    <button class="btn btn-outline-secondary" type="button" id="inputGroupFileAddon03">Button</button>
338
  </div>
ysds's avatar
ysds committed
339
  <div class="custom-file">
340
    <input type="file" class="custom-file-input" id="inputGroupFile03" aria-describedby="inputGroupFileAddon03">
ysds's avatar
ysds committed
341
342
    <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
  <div class="custom-file">
347
    <input type="file" class="custom-file-input" id="inputGroupFile04" aria-describedby="inputGroupFileAddon04">
ysds's avatar
ysds committed
348
349
    <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
  </div>
350
  <div class="input-group-append">
351
    <button class="btn btn-outline-secondary" type="button" id="inputGroupFileAddon04">Button</button>
352
  </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.