dropdowns.md 39 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: Dropdowns
Mark Otto's avatar
Mark Otto committed
4
description: Toggle contextual overlays for displaying lists of links and more with the Bootstrap dropdown plugin.
5
group: components
Mark Otto's avatar
Mark Otto committed
6
toc: true
Mark Otto's avatar
Mark Otto committed
7
8
---

9
10
## Overview

XhmikosR's avatar
XhmikosR committed
11
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. They're made interactive with the included Bootstrap dropdown JavaScript plugin. They're toggled by clicking, not by hovering; this is [an intentional design decision](http://markdotto.com/2012/02/27/bootstrap-explained-dropdowns/).
Mark Otto's avatar
Mark Otto committed
12

XhmikosR's avatar
XhmikosR committed
13
Dropdowns are built on a third party library, [Popper.js](https://popper.js.org/), which provides dynamic positioning and viewport detection. Be sure to include [popper.min.js]({{< param "cdn.popper" >}}) before Bootstrap's JavaScript or use `bootstrap.bundle.min.js` / `bootstrap.bundle.js` which contains Popper.js. Popper.js isn't used to position dropdowns in navbars though as dynamic positioning isn't required.
14

Mark Otto's avatar
Mark Otto committed
15
## Accessibility
16

17
The [<abbr title="Web Accessibility Initiative">WAI</abbr> <abbr title="Accessible Rich Internet Applications">ARIA</abbr>](https://www.w3.org/TR/wai-aria/) standard defines an actual [`role="menu"` widget](https://www.w3.org/WAI/PF/aria/roles#menu), but this is specific to application-like menus which trigger actions or functions. <abbr title="Accessible Rich Internet Applications">ARIA</abbr> menus can only contain menu items, checkbox menu items, radio button menu items, radio button groups, and sub-menus.
18
19
20
21

Bootstrap's dropdowns, on the other hand, are designed to be generic and applicable to a variety of situations and markup structures. For instance, it is possible to create dropdowns that contain additional inputs and form controls, such as search fields or login forms. For this reason, Bootstrap does not expect (nor automatically add) any of the `role` and `aria-` attributes required for true <abbr title="Accessible Rich Internet Applications">ARIA</abbr> menus. Authors will have to include these more specific attributes themselves.

However, Bootstrap does add built-in support for most standard keyboard menu interactions, such as the ability to move through individual `.dropdown-item` elements using the cursor keys and close the menu with the <kbd>ESC</kbd> key.
Mark Otto's avatar
Mark Otto committed
22
23
24

## Examples

25
Wrap the dropdown's toggle (your button or link) and the dropdown menu within `.dropdown`, or another element that declares `position: relative;`. Dropdowns can be triggered from `<a>` or `<button>` elements to better fit your potential needs. The examples shown here use semantic `<ul>` elements where appropriate, but custom markup is supported.
26

27
### Single button
28
29

Any single `.btn` can be turned into a dropdown toggle with some markup changes. Here's how you can put them to work with either `<button>` elements:
Mark Otto's avatar
Mark Otto committed
30

XhmikosR's avatar
XhmikosR committed
31
{{< example >}}
32
<div class="dropdown">
33
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-expanded="false">
34
    Dropdown button
Mark Otto's avatar
Mark Otto committed
35
  </button>
36
37
38
39
40
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
  </ul>
41
</div>
XhmikosR's avatar
XhmikosR committed
42
{{< /example >}}
43
44
45

And with `<a>` elements:

XhmikosR's avatar
XhmikosR committed
46
{{< example >}}
47
<div class="dropdown">
48
  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-expanded="false">
49
50
51
    Dropdown link
  </a>

52
53
54
55
56
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
  </ul>
Mark Otto's avatar
Mark Otto committed
57
</div>
XhmikosR's avatar
XhmikosR committed
58
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
59

60
61
62
63
The best part is you can do this with any button variant, too:

<div class="bd-example">
  <div class="btn-group">
64
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Primary</button>
65
66
67
68
69
70
71
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
72
73
  </div><!-- /btn-group -->
  <div class="btn-group">
74
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Secondary</button>
75
76
77
78
79
80
81
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
82
83
  </div><!-- /btn-group -->
  <div class="btn-group">
84
    <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Success</button>
85
86
87
88
89
90
91
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
92
93
  </div><!-- /btn-group -->
  <div class="btn-group">
94
    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Info</button>
95
96
97
98
99
100
101
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
102
103
  </div><!-- /btn-group -->
  <div class="btn-group">
104
    <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Warning</button>
105
106
107
108
109
110
111
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
112
113
  </div><!-- /btn-group -->
  <div class="btn-group">
114
    <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Danger</button>
115
116
117
118
119
120
121
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
122
123
124
  </div><!-- /btn-group -->
</div>

XhmikosR's avatar
XhmikosR committed
125
{{< highlight html >}}
126
127
<!-- Example single danger button -->
<div class="btn-group">
128
  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
129
130
    Action
  </button>
131
132
133
134
135
136
137
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
    <li><hr class="dropdown-divider"></li>
    <li><a class="dropdown-item" href="#">Separated link</a></li>
  </ul>
138
</div>
XhmikosR's avatar
XhmikosR committed
139
{{< /highlight >}}
140

141
### Split button
142
143
144
145
146
147
148

Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of `.dropdown-toggle-split` for proper spacing around the dropdown caret.

We use this extra class to reduce the horizontal `padding` on either side of the caret by 25% and remove the `margin-left` that's added for regular button dropdowns. Those extra changes keep the caret centered in the split button and provide a more appropriately sized hit area next to the main button.

<div class="bd-example">
  <div class="btn-group">
149
    <button type="button" class="btn btn-primary">Primary</button>
150
    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
151
152
      <span class="sr-only">Toggle Dropdown</span>
    </button>
153
154
155
156
157
158
159
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
160
161
  </div><!-- /btn-group -->
  <div class="btn-group">
162
    <button type="button" class="btn btn-secondary">Secondary</button>
163
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
164
165
      <span class="sr-only">Toggle Dropdown</span>
    </button>
166
167
168
169
170
171
172
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
173
174
175
  </div><!-- /btn-group -->
  <div class="btn-group">
    <button type="button" class="btn btn-success">Success</button>
176
    <button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
177
178
      <span class="sr-only">Toggle Dropdown</span>
    </button>
179
180
181
182
183
184
185
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
186
187
188
  </div><!-- /btn-group -->
  <div class="btn-group">
    <button type="button" class="btn btn-info">Info</button>
189
    <button type="button" class="btn btn-info dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
190
191
      <span class="sr-only">Toggle Dropdown</span>
    </button>
192
193
194
195
196
197
198
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
199
200
201
  </div><!-- /btn-group -->
  <div class="btn-group">
    <button type="button" class="btn btn-warning">Warning</button>
202
    <button type="button" class="btn btn-warning dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
203
204
      <span class="sr-only">Toggle Dropdown</span>
    </button>
205
206
207
208
209
210
211
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
212
213
214
  </div><!-- /btn-group -->
  <div class="btn-group">
    <button type="button" class="btn btn-danger">Danger</button>
215
    <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
216
217
      <span class="sr-only">Toggle Dropdown</span>
    </button>
218
219
220
221
222
223
224
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
225
226
227
  </div><!-- /btn-group -->
</div>

XhmikosR's avatar
XhmikosR committed
228
{{< highlight html >}}
229
230
231
<!-- Example split danger button -->
<div class="btn-group">
  <button type="button" class="btn btn-danger">Action</button>
232
  <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
233
234
    <span class="sr-only">Toggle Dropdown</span>
  </button>
235
236
237
238
239
240
241
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="#">Action</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item" href="#">Something else here</a></li>
    <li><hr class="dropdown-divider"></li>
    <li><a class="dropdown-item" href="#">Separated link</a></li>
  </ul>
242
</div>
XhmikosR's avatar
XhmikosR committed
243
{{< /highlight >}}
244
245
246
247
248
249
250
251

## Sizing

Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.

<div class="bd-example">
  <div class="btn-toolbar" role="toolbar">
    <div class="btn-group">
252
      <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
253
254
        Large button
      </button>
255
256
257
258
259
260
261
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#">Separated link</a></li>
      </ul>
262
    </div><!-- /btn-group -->
263
    <div class="btn-group ml-2">
264
      <button type="button" class="btn btn-lg btn-secondary">Large split button</button>
265
      <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
266
267
        <span class="sr-only">Toggle Dropdown</span>
      </button>
268
269
270
271
272
273
274
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#">Separated link</a></li>
      </ul>
275
276
277
278
    </div><!-- /btn-group -->
  </div><!-- /btn-toolbar -->
  <div class="btn-toolbar" role="toolbar">
    <div class="btn-group">
279
      <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
280
281
        Small button
      </button>
282
283
284
285
286
287
288
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#">Separated link</a></li>
      </ul>
289
    </div><!-- /btn-group -->
290
    <div class="btn-group ml-2">
291
      <button type="button" class="btn btn-sm btn-secondary">Small split button</button>
292
      <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
293
294
        <span class="sr-only">Toggle Dropdown</span>
      </button>
295
296
297
298
299
300
301
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#">Separated link</a></li>
      </ul>
302
303
    </div><!-- /btn-group -->
  </div><!-- /btn-toolbar -->
304
</div>
305

XhmikosR's avatar
XhmikosR committed
306
{{< highlight html >}}
307
308
<!-- Large button groups (default and split) -->
<div class="btn-group">
309
  <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
310
311
    Large button
  </button>
312
  <ul class="dropdown-menu">
313
    ...
314
  </ul>
315
316
</div>
<div class="btn-group">
317
  <button class="btn btn-secondary btn-lg" type="button">
ysds's avatar
ysds committed
318
    Large split button
319
  </button>
320
  <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
321
322
    <span class="sr-only">Toggle Dropdown</span>
  </button>
323
  <ul class="dropdown-menu">
324
    ...
325
  </ul>
326
327
328
329
</div>

<!-- Small button groups (default and split) -->
<div class="btn-group">
330
  <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
331
332
    Small button
  </button>
333
  <ul class="dropdown-menu">
334
    ...
335
  </ul>
336
337
</div>
<div class="btn-group">
338
  <button class="btn btn-secondary btn-sm" type="button">
ysds's avatar
ysds committed
339
    Small split button
340
  </button>
341
  <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
342
343
    <span class="sr-only">Toggle Dropdown</span>
  </button>
344
  <ul class="dropdown-menu">
345
    ...
346
  </ul>
347
</div>
XhmikosR's avatar
XhmikosR committed
348
{{< /highlight >}}
349

350
351
352
## Directions

### Dropup
353
354
355
356

Trigger dropdown menus above elements by adding `.dropup` to the parent element.

<div class="bd-example">
357
  <div class="btn-group dropup">
358
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
359
360
      Dropup
    </button>
361
362
363
364
365
366
367
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
368
369
370
371
372
  </div>
  <div class="btn-group dropup">
    <button type="button" class="btn btn-secondary">
      Split dropup
    </button>
373
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
374
375
      <span class="sr-only">Toggle Dropdown</span>
    </button>
376
377
378
379
380
381
382
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
383
384
385
  </div>
</div>

XhmikosR's avatar
XhmikosR committed
386
{{< highlight html >}}
387
388
<!-- Default dropup button -->
<div class="btn-group dropup">
389
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
ysds's avatar
ysds committed
390
    Dropup
391
  </button>
392
  <ul class="dropdown-menu">
393
    <!-- Dropdown menu links -->
394
  </ul>
395
396
397
398
399
400
401
</div>

<!-- Split dropup button -->
<div class="btn-group dropup">
  <button type="button" class="btn btn-secondary">
    Split dropup
  </button>
402
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
403
404
    <span class="sr-only">Toggle Dropdown</span>
  </button>
405
  <ul class="dropdown-menu">
406
    <!-- Dropdown menu links -->
407
  </ul>
408
</div>
XhmikosR's avatar
XhmikosR committed
409
{{< /highlight >}}
410

411
### Dropright
412
413
414
415
416

Trigger dropdown menus at the right of the elements by adding `.dropright` to the parent element.

<div class="bd-example">
  <div class="btn-group dropright">
417
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
418
419
      Dropright
    </button>
420
421
422
423
424
425
426
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
427
428
429
430
431
  </div>
  <div class="btn-group dropright">
    <button type="button" class="btn btn-secondary">
      Split dropright
    </button>
432
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
ysds's avatar
ysds committed
433
      <span class="sr-only">Toggle Dropright</span>
434
    </button>
435
436
437
438
439
440
441
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
442
443
444
  </div>
</div>

XhmikosR's avatar
XhmikosR committed
445
{{< highlight html >}}
446
447
<!-- Default dropright button -->
<div class="btn-group dropright">
448
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
ysds's avatar
ysds committed
449
    Dropright
450
  </button>
451
  <ul class="dropdown-menu">
452
    <!-- Dropdown menu links -->
453
  </ul>
454
455
456
457
458
459
460
</div>

<!-- Split dropright button -->
<div class="btn-group dropright">
  <button type="button" class="btn btn-secondary">
    Split dropright
  </button>
461
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
462
463
    <span class="sr-only">Toggle Dropright</span>
  </button>
464
  <ul class="dropdown-menu">
465
    <!-- Dropdown menu links -->
466
  </ul>
467
</div>
XhmikosR's avatar
XhmikosR committed
468
{{< /highlight >}}
469

470
### Dropleft
471
472
473
474
475

Trigger dropdown menus at the left of the elements by adding `.dropleft` to the parent element.

<div class="bd-example">
  <div class="btn-group dropleft">
476
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
477
478
      Dropleft
    </button>
479
480
481
482
483
484
485
    <ul class="dropdown-menu">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
486
487
488
  </div>
  <div class="btn-group">
    <div class="btn-group dropleft" role="group">
489
      <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
490
491
        <span class="sr-only">Toggle Dropleft</span>
      </button>
492
493
494
495
496
497
498
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
        <li><hr class="dropdown-divider"></li>
        <li><a class="dropdown-item" href="#">Separated link</a></li>
      </ul>
499
500
501
502
503
504
505
    </div>
    <button type="button" class="btn btn-secondary">
      Split dropleft
    </button>
  </div>
</div>

XhmikosR's avatar
XhmikosR committed
506
{{< highlight html >}}
507
508
<!-- Default dropleft button -->
<div class="btn-group dropleft">
509
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
510
511
    Dropleft
  </button>
512
  <ul class="dropdown-menu">
513
    <!-- Dropdown menu links -->
514
  </ul>
515
516
517
518
519
</div>

<!-- Split dropleft button -->
<div class="btn-group">
  <div class="btn-group dropleft" role="group">
520
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
521
522
      <span class="sr-only">Toggle Dropleft</span>
    </button>
523
    <ul class="dropdown-menu">
524
      <!-- Dropdown menu links -->
525
    </ul>
526
527
528
529
530
  </div>
  <button type="button" class="btn btn-secondary">
    Split dropleft
  </button>
</div>
XhmikosR's avatar
XhmikosR committed
531
{{< /highlight >}}
532

533
## Menu items
534

535
Historically dropdown menu contents *had* to be links, but that's no longer the case with v4. Now you can optionally use `<button>` elements in your dropdowns instead of just `<a>`s.
536

XhmikosR's avatar
XhmikosR committed
537
{{< example >}}
538
<div class="dropdown">
539
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-expanded="false">
540
541
    Dropdown
  </button>
542
543
544
545
546
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
547
</div>
XhmikosR's avatar
XhmikosR committed
548
{{< /example >}}
549

550
551
You can also create non-interactive dropdown items with `.dropdown-item-text`. Feel free to style further with custom CSS or text utilities.

XhmikosR's avatar
XhmikosR committed
552
{{< example >}}
553
554
555
556
557
558
<ul class="dropdown-menu">
  <li><span class="dropdown-item-text">Dropdown item text</span></li>
  <li><a class="dropdown-item" href="#">Action</a></li>
  <li><a class="dropdown-item" href="#">Another action</a></li>
  <li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
XhmikosR's avatar
XhmikosR committed
559
{{< /example >}}
560
561
562
563
564

### Active

Add `.active` to items in the dropdown to **style them as active**.

XhmikosR's avatar
XhmikosR committed
565
{{< example >}}
566
567
568
569
570
<ul class="dropdown-menu">
  <li><a class="dropdown-item" href="#">Regular link</a></li>
  <li><a class="dropdown-item active" href="#">Active link</a></li>
  <li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
XhmikosR's avatar
XhmikosR committed
571
{{< /example >}}
572
573
574
575
576

### Disabled

Add `.disabled` to items in the dropdown to **style them as disabled**.

XhmikosR's avatar
XhmikosR committed
577
{{< example >}}
578
579
580
581
582
<ul class="dropdown-menu">
  <li><a class="dropdown-item" href="#">Regular link</a></li>
  <li><a class="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true">Disabled link</a></li>
  <li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
XhmikosR's avatar
XhmikosR committed
583
{{< /example >}}
584

585
## Menu alignment
Mark Otto's avatar
Mark Otto committed
586
587
588

By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add `.dropdown-menu-right` to a `.dropdown-menu` to right align the dropdown menu.

XhmikosR's avatar
XhmikosR committed
589
{{< callout info >}}
590
**Heads up!** Dropdowns are positioned thanks to Popper.js (except when they are contained in a navbar).
XhmikosR's avatar
XhmikosR committed
591
{{< /callout >}}
Mark Otto's avatar
Mark Otto committed
592

XhmikosR's avatar
XhmikosR committed
593
{{< example >}}
594
<div class="btn-group">
595
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
596
    Right-aligned menu
597
  </button>
598
599
600
601
602
  <ul class="dropdown-menu dropdown-menu-right">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
Mark Otto's avatar
Mark Otto committed
603
</div>
XhmikosR's avatar
XhmikosR committed
604
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
605

606
607
608
609
610
611
### Responsive alignment

If you want to use responsive alignment, disable dynamic positioning by adding the `data-display="static"` attribute and use the responsive variation classes.

To align **right** the dropdown menu with the given breakpoint or larger, add `.dropdown-menu{-sm|-md|-lg|-xl}-right`.

XhmikosR's avatar
XhmikosR committed
612
{{< example >}}
613
<div class="btn-group">
614
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-expanded="false">
615
616
    Left-aligned but right aligned when large screen
  </button>
617
618
619
620
621
  <ul class="dropdown-menu dropdown-menu-lg-right">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
622
</div>
XhmikosR's avatar
XhmikosR committed
623
{{< /example >}}
624
625
626

To align **left** the dropdown menu with the given breakpoint or larger, add `.dropdown-menu-right` and `.dropdown-menu{-sm|-md|-lg|-xl}-left`.

XhmikosR's avatar
XhmikosR committed
627
{{< example >}}
628
<div class="btn-group">
629
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-expanded="false">
630
631
    Right-aligned but left aligned when large screen
  </button>
632
633
634
635
636
  <ul class="dropdown-menu dropdown-menu-right dropdown-menu-lg-left">
    <li><button class="dropdown-item" type="button">Action</button></li>
    <li><button class="dropdown-item" type="button">Another action</button></li>
    <li><button class="dropdown-item" type="button">Something else here</button></li>
  </ul>
637
</div>
XhmikosR's avatar
XhmikosR committed
638
{{< /example >}}
639
640
641

Note that you don't need to add a `data-display="static"` attribute to dropdown buttons in navbars, since Popper.js isn't used in navbars.

642
643
644
## Menu content

### Headers
Mark Otto's avatar
Mark Otto committed
645
646
647

Add a header to label sections of actions in any dropdown menu.

XhmikosR's avatar
XhmikosR committed
648
{{< example >}}
649
650
651
652
653
<ul class="dropdown-menu">
  <li><h6 class="dropdown-header">Dropdown header</h6></li>
  <li><a class="dropdown-item" href="#">Action</a></li>
  <li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
XhmikosR's avatar
XhmikosR committed
654
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
655

656
### Dividers
Mark Otto's avatar
Mark Otto committed
657
658
659

Separate groups of related menu items with a divider.

XhmikosR's avatar
XhmikosR committed
660
{{< example >}}
661
662
663
664
665
666
667
<ul class="dropdown-menu">
  <li><a class="dropdown-item" href="#">Action</a></li>
  <li><a class="dropdown-item" href="#">Another action</a></li>
  <li><a class="dropdown-item" href="#">Something else here</a></li>
  <li><hr class="dropdown-divider"></li>
  <li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
XhmikosR's avatar
XhmikosR committed
668
{{< /example >}}
Mark Otto's avatar
Mark Otto committed
669

670
671
### Text

XhmikosR's avatar
XhmikosR committed
672
Place any freeform text within a dropdown menu with text and use [spacing utilities]({{< docsref "/utilities/spacing" >}}). Note that you'll likely need additional sizing styles to constrain the menu width.
673

XhmikosR's avatar
XhmikosR committed
674
{{< example >}}
675
676
677
678
679
680
681
682
<div class="dropdown-menu p-4 text-muted" style="max-width: 200px;">
  <p>
    Some example text that's free-flowing within the dropdown menu.
  </p>
  <p class="mb-0">
    And this is more example text.
  </p>
</div>
XhmikosR's avatar
XhmikosR committed
683
{{< /example >}}
684
685

### Forms
686

XhmikosR's avatar
XhmikosR committed
687
Put a form within a dropdown menu, or make it into a dropdown menu, and use [margin or padding utilities]({{< docsref "/utilities/spacing" >}}) to give it the negative space you require.
688

XhmikosR's avatar
XhmikosR committed
689
{{< example >}}
690
691
<div class="dropdown-menu">
  <form class="px-4 py-3">
692
    <div class="mb-3">
693
694
695
      <label for="exampleDropdownFormEmail1">Email address</label>
      <input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
    </div>
696
    <div class="mb-3">
697
698
699
      <label for="exampleDropdownFormPassword1">Password</label>
      <input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
    </div>
700
    <div class="mb-3">
701
702
703
704
705
706
      <div class="form-check">
        <input type="checkbox" class="form-check-input" id="dropdownCheck">
        <label class="form-check-label" for="dropdownCheck">
          Remember me
        </label>
      </div>
707
708
709
710
711
712
713
    </div>
    <button type="submit" class="btn btn-primary">Sign in</button>
  </form>
  <div class="dropdown-divider"></div>
  <a class="dropdown-item" href="#">New around here? Sign up</a>
  <a class="dropdown-item" href="#">Forgot password?</a>
</div>
XhmikosR's avatar
XhmikosR committed
714
{{< /example >}}
715

XhmikosR's avatar
XhmikosR committed
716
{{< example >}}
717
<form class="dropdown-menu p-4">
718
  <div class="mb-3">
719
720
721
    <label for="exampleDropdownFormEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="email@example.com">
  </div>
722
  <div class="mb-3">
XhmikosR's avatar
XhmikosR committed
723
724
    <label for="exampleDropdownFormPassword2">Password</label>
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
725
  </div>
726
  <div class="mb-3">
727
728
729
730
731
732
    <div class="form-check">
      <input type="checkbox" class="form-check-input" id="dropdownCheck2">
      <label class="form-check-label" for="dropdownCheck2">
        Remember me
      </label>
    </div>
733
734
735
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>
XhmikosR's avatar
XhmikosR committed
736
{{< /example >}}
737

738
739
740
741
## Dropdown options

Use `data-offset` or `data-reference` to change the location of the dropdown.

XhmikosR's avatar
XhmikosR committed
742
{{< example >}}
743
744
<div class="d-flex">
  <div class="dropdown mr-1">
745
    <button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuOffset" data-toggle="dropdown" aria-expanded="false" data-offset="10,20">
746
747
      Offset
    </button>
748
749
750
751
752
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuOffset">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
753
754
755
  </div>
  <div class="btn-group">
    <button type="button" class="btn btn-secondary">Reference</button>
756
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" id="dropdownMenuReference" data-toggle="dropdown" aria-expanded="false" data-reference="parent">
757
758
      <span class="sr-only">Toggle Dropdown</span>
    </button>
759
760
761
762
763
764
765
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuReference">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
      <li><hr class="dropdown-divider"></li>
      <li><a class="dropdown-item" href="#">Separated link</a></li>
    </ul>
766
767
  </div>
</div>
XhmikosR's avatar
XhmikosR committed
768
{{< /example >}}
769

770
771
## Usage

Mark Otto's avatar
Mark Otto committed
772
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the `.show` class on the parent list item. The `data-toggle="dropdown"` attribute is relied on for closing dropdown menus at an application level, so it's a good idea to always use it.
773

XhmikosR's avatar
XhmikosR committed
774
{{< callout info >}}
775
On touch-enabled devices, opening a dropdown adds empty `mouseover` handlers to the immediate children of the `<body>` element. This admittedly ugly hack is necessary to work around a [quirk in iOS' event delegation](https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html), which would otherwise prevent a tap anywhere outside of the dropdown from triggering the code that closes the dropdown. Once the dropdown is closed, these additional empty `mouseover` handlers are removed.
XhmikosR's avatar
XhmikosR committed
776
{{< /callout >}}
777
778
779
780
781

### Via data attributes

Add `data-toggle="dropdown"` to a link or button to toggle a dropdown.

XhmikosR's avatar
XhmikosR committed
782
{{< highlight html >}}
783
<div class="dropdown">
784
  <button id="dLabel" type="button" data-toggle="dropdown" aria-expanded="false">
785
786
    Dropdown trigger
  </button>
787
  <ul class="dropdown-menu" aria-labelledby="dLabel">
788
    ...
789
  </ul>
790
</div>
XhmikosR's avatar
XhmikosR committed
791
{{< /highlight >}}
792
793
794
795
796

### Via JavaScript

Call the dropdowns via JavaScript:

XhmikosR's avatar
XhmikosR committed
797
{{< highlight js >}}
798
799
800
801
var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
  return new bootstrap.Dropdown(dropdownToggleEl)
})
XhmikosR's avatar
XhmikosR committed
802
{{< /highlight >}}
803

XhmikosR's avatar
XhmikosR committed
804
{{< callout info >}}
805
##### `data-toggle="dropdown"` still required
806
807

Regardless of whether you call your dropdown via JavaScript or instead use the data-api, `data-toggle="dropdown"` is always required to be present on the dropdown's trigger element.
XhmikosR's avatar
XhmikosR committed
808
{{< /callout >}}
809
810
811

### Options

812
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-offset=""`.
813

Mark Otto's avatar
Mark Otto committed
814
<table class="table">
815
816
817
818
819
820
821
822
823
824
825
  <thead>
    <tr>
      <th style="width: 100px;">Name</th>
      <th style="width: 100px;">Type</th>
      <th style="width: 50px;">Default</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>offset</td>
826
      <td>number | string | function</td>
827
      <td>0</td>
828
829
830
831
832
      <td>
        <p>Offset of the dropdown relative to its target.</p>
        <p>When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.</p>
        <p>For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</p>
      </td>
833
    </tr>
834
835
836
837
838
839
    <tr>
      <td>flip</td>
      <td>boolean</td>
      <td>true</td>
      <td>Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..flip.enabled">flip docs</a>.</td>
    </tr>
840
841
842
843
844
845
    <tr>
      <td>boundary</td>
      <td>string | element</td>
      <td>'scrollParent'</td>
      <td>Overflow constraint boundary of the dropdown menu. Accepts the values of <code>'viewport'</code>, <code>'window'</code>, <code>'scrollParent'</code>, or an HTMLElement reference (JavaScript only). For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
    </tr>
846
847
848
849
850
851
    <tr>
      <td>reference</td>
      <td>string | element</td>
      <td>'toggle'</td>
      <td>Reference element of the dropdown menu. Accepts the values of <code>'toggle'</code>, <code>'parent'</code>, or an HTMLElement reference. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#referenceObject">referenceObject docs</a>.</td>
    </tr>
852
853
854
    <tr>
      <td>display</td>
      <td>string</td>
855
      <td>'dynamic'</td>
856
      <td>By default, we use Popper.js for dynamic positioning. Disable this with <code>static</code>.</td>
857
    </tr>
858
859
860
861
    <tr>
      <td>popperConfig</td>
      <td>null | object</td>
      <td>null</td>
XhmikosR's avatar
XhmikosR committed
862
      <td>To change Bootstrap's default Popper.js config, see <a href="https://popper.js.org/popper-documentation.html#Popper.Defaults">Popper.js's configuration</a></td>
863
    </tr>
864
865
  </tbody>
</table>
866

867
868
Note when `boundary` is set to any value other than `'scrollParent'`, the style `position: static` is applied to the `.dropdown` container.

869
870
### Methods

871
<table class="table">
Mark Otto's avatar
Mark Otto committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
  <thead>
    <tr>
      <th>Method</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>toggle</code></td>
      <td>
        Toggles the dropdown menu of a given navbar or tabbed navigation.
      </td>
    </tr>
    <tr>
      <td><code>show</code></td>
      <td>
        Shows the dropdown menu of a given navbar or tabbed navigation.
      </td>
    </tr>
    <tr>
      <td><code>hide</code></td>
      <td>
        Hides the dropdown menu of a given navbar or tabbed navigation.
      </td>
    </tr>
    <tr>
      <td><code>update</code></td>
      <td>
        Updates the position of an element's dropdown.
      </td>
    </tr>
    <tr>
      <td><code>dispose</code></td>
      <td>
        Destroys an element's dropdown.
      </td>
    </tr>
    <tr>
910
      <td><code>getInstance</code></td>
Mark Otto's avatar
Mark Otto committed
911
912
913
914
915
916
      <td>
        Static method which allows you to get the dropdown instance associated with a DOM element.
      </td>
    </tr>
  </tbody>
</table>
917
918
919
920

### Events

All dropdown events are fired at the `.dropdown-menu`'s parent element and have a `relatedTarget` property, whose value is the toggling anchor element.
Mark Otto's avatar
Mark Otto committed
921
922
`hide.bs.dropdown` and `hidden.bs.dropdown` events have a `clickEvent` property (only when the original Event type is `click`) that contains an Event Object for the click event.

923
<table class="table">
Mark Otto's avatar
Mark Otto committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  <thead>
    <tr>
      <th>Method</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <code>show.bs.dropdown</code>
      </td>
      <td>
        Fires immediately when the show instance method is called.
      </td>
    </tr>
    <tr>
      <td>
        <code>shown.bs.dropdown</code>
      </td>
      <td>
        Fired when the dropdown has been made visible to the user and CSS transitions have completed.
      </td>
    </tr>
    <tr>
      <td>
        <code>hide.bs.dropdown</code>
      </td>
      <td>
        Fires immediately when the hide instance method has been called.
      </td>
    </tr>
    <tr>
      <td>
        <code>hidden.bs.dropdown</code>
      </td>
      <td>
        Fired when the dropdown has finished being hidden from the user and CSS transitions have completed.
      </td>
    </tr>
  </tbody>
</table>
965

XhmikosR's avatar
XhmikosR committed
966
{{< highlight js >}}
967
968
var myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', function () {
XhmikosR's avatar
XhmikosR committed
969
  // do something...
970
})
XhmikosR's avatar
XhmikosR committed
971
{{< /highlight >}}