Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Dropdowns
4
description: Toggle contextual overlays for displaying lists of links and more with the Bootstrap dropdown plugin.
5
group: components
6
toc: true
7
---
8
 
9
## Overview
10
 
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](https://markdotto.com/2012/02/27/bootstrap-explained-dropdowns/).
12
 
13
Dropdowns are built on a third party library, [Popper](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. Popper isn't used to position dropdowns in navbars though as dynamic positioning isn't required.
14
 
15
If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
16
 
17
## Accessibility
18
 
19
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/TR/wai-aria/#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.
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.
22
 
23
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.
24
 
25
## Examples
26
 
27
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.
28
 
29
### Single button
30
 
31
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:
32
 
33
{{< example >}}
34
<div class="dropdown">
35
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
36
    Dropdown button
37
  </button>
38
  <div class="dropdown-menu">
39
    <a class="dropdown-item" href="#">Action</a>
40
    <a class="dropdown-item" href="#">Another action</a>
41
    <a class="dropdown-item" href="#">Something else here</a>
42
  </div>
43
</div>
44
{{< /example >}}
45
 
46
And with `<a>` elements:
47
 
48
{{< example >}}
49
<div class="dropdown">
50
  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-expanded="false">
51
    Dropdown link
52
  </a>
53
 
54
  <div class="dropdown-menu">
55
    <a class="dropdown-item" href="#">Action</a>
56
    <a class="dropdown-item" href="#">Another action</a>
57
    <a class="dropdown-item" href="#">Something else here</a>
58
  </div>
59
</div>
60
{{< /example >}}
61
 
62
The best part is you can do this with any button variant, too:
63
 
64
<div class="bd-example">
65
  <div class="btn-group">
66
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Primary</button>
67
    <div class="dropdown-menu">
68
      <a class="dropdown-item" href="#">Action</a>
69
      <a class="dropdown-item" href="#">Another action</a>
70
      <a class="dropdown-item" href="#">Something else here</a>
71
      <div class="dropdown-divider"></div>
72
      <a class="dropdown-item" href="#">Separated link</a>
73
    </div>
74
  </div><!-- /btn-group -->
75
  <div class="btn-group">
76
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Secondary</button>
77
    <div class="dropdown-menu">
78
      <a class="dropdown-item" href="#">Action</a>
79
      <a class="dropdown-item" href="#">Another action</a>
80
      <a class="dropdown-item" href="#">Something else here</a>
81
      <div class="dropdown-divider"></div>
82
      <a class="dropdown-item" href="#">Separated link</a>
83
    </div>
84
  </div><!-- /btn-group -->
85
  <div class="btn-group">
86
    <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Success</button>
87
    <div class="dropdown-menu">
88
      <a class="dropdown-item" href="#">Action</a>
89
      <a class="dropdown-item" href="#">Another action</a>
90
      <a class="dropdown-item" href="#">Something else here</a>
91
      <div class="dropdown-divider"></div>
92
      <a class="dropdown-item" href="#">Separated link</a>
93
    </div>
94
  </div><!-- /btn-group -->
95
  <div class="btn-group">
96
    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Info</button>
97
    <div class="dropdown-menu">
98
      <a class="dropdown-item" href="#">Action</a>
99
      <a class="dropdown-item" href="#">Another action</a>
100
      <a class="dropdown-item" href="#">Something else here</a>
101
      <div class="dropdown-divider"></div>
102
      <a class="dropdown-item" href="#">Separated link</a>
103
    </div>
104
  </div><!-- /btn-group -->
105
  <div class="btn-group">
106
    <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Warning</button>
107
    <div class="dropdown-menu">
108
      <a class="dropdown-item" href="#">Action</a>
109
      <a class="dropdown-item" href="#">Another action</a>
110
      <a class="dropdown-item" href="#">Something else here</a>
111
      <div class="dropdown-divider"></div>
112
      <a class="dropdown-item" href="#">Separated link</a>
113
    </div>
114
  </div><!-- /btn-group -->
115
  <div class="btn-group">
116
    <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Danger</button>
117
    <div class="dropdown-menu">
118
      <a class="dropdown-item" href="#">Action</a>
119
      <a class="dropdown-item" href="#">Another action</a>
120
      <a class="dropdown-item" href="#">Something else here</a>
121
      <div class="dropdown-divider"></div>
122
      <a class="dropdown-item" href="#">Separated link</a>
123
    </div>
124
  </div><!-- /btn-group -->
125
</div>
126
 
127
```html
128
<!-- Example single danger button -->
129
<div class="btn-group">
130
  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
131
    Action
132
  </button>
133
  <div class="dropdown-menu">
134
    <a class="dropdown-item" href="#">Action</a>
135
    <a class="dropdown-item" href="#">Another action</a>
136
    <a class="dropdown-item" href="#">Something else here</a>
137
    <div class="dropdown-divider"></div>
138
    <a class="dropdown-item" href="#">Separated link</a>
139
  </div>
140
</div>
141
```
142
 
143
### Split button
144
 
145
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.
146
 
147
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.
148
 
149
<div class="bd-example">
150
  <div class="btn-group">
151
    <button type="button" class="btn btn-primary">Primary</button>
152
    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
153
      <span class="sr-only">Toggle Dropdown</span>
154
    </button>
155
    <div class="dropdown-menu">
156
      <a class="dropdown-item" href="#">Action</a>
157
      <a class="dropdown-item" href="#">Another action</a>
158
      <a class="dropdown-item" href="#">Something else here</a>
159
      <div class="dropdown-divider"></div>
160
      <a class="dropdown-item" href="#">Separated link</a>
161
    </div>
162
  </div><!-- /btn-group -->
163
  <div class="btn-group">
164
    <button type="button" class="btn btn-secondary">Secondary</button>
165
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
166
      <span class="sr-only">Toggle Dropdown</span>
167
    </button>
168
    <div class="dropdown-menu">
169
      <a class="dropdown-item" href="#">Action</a>
170
      <a class="dropdown-item" href="#">Another action</a>
171
      <a class="dropdown-item" href="#">Something else here</a>
172
      <div class="dropdown-divider"></div>
173
      <a class="dropdown-item" href="#">Separated link</a>
174
    </div>
175
  </div><!-- /btn-group -->
176
  <div class="btn-group">
177
    <button type="button" class="btn btn-success">Success</button>
178
    <button type="button" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
179
      <span class="sr-only">Toggle Dropdown</span>
180
    </button>
181
    <div class="dropdown-menu">
182
      <a class="dropdown-item" href="#">Action</a>
183
      <a class="dropdown-item" href="#">Another action</a>
184
      <a class="dropdown-item" href="#">Something else here</a>
185
      <div class="dropdown-divider"></div>
186
      <a class="dropdown-item" href="#">Separated link</a>
187
    </div>
188
  </div><!-- /btn-group -->
189
  <div class="btn-group">
190
    <button type="button" class="btn btn-info">Info</button>
191
    <button type="button" class="btn btn-info dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
192
      <span class="sr-only">Toggle Dropdown</span>
193
    </button>
194
    <div class="dropdown-menu">
195
      <a class="dropdown-item" href="#">Action</a>
196
      <a class="dropdown-item" href="#">Another action</a>
197
      <a class="dropdown-item" href="#">Something else here</a>
198
      <div class="dropdown-divider"></div>
199
      <a class="dropdown-item" href="#">Separated link</a>
200
    </div>
201
  </div><!-- /btn-group -->
202
  <div class="btn-group">
203
    <button type="button" class="btn btn-warning">Warning</button>
204
    <button type="button" class="btn btn-warning dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
205
      <span class="sr-only">Toggle Dropdown</span>
206
    </button>
207
    <div class="dropdown-menu">
208
      <a class="dropdown-item" href="#">Action</a>
209
      <a class="dropdown-item" href="#">Another action</a>
210
      <a class="dropdown-item" href="#">Something else here</a>
211
      <div class="dropdown-divider"></div>
212
      <a class="dropdown-item" href="#">Separated link</a>
213
    </div>
214
  </div><!-- /btn-group -->
215
  <div class="btn-group">
216
    <button type="button" class="btn btn-danger">Danger</button>
217
    <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
218
      <span class="sr-only">Toggle Dropdown</span>
219
    </button>
220
    <div class="dropdown-menu">
221
      <a class="dropdown-item" href="#">Action</a>
222
      <a class="dropdown-item" href="#">Another action</a>
223
      <a class="dropdown-item" href="#">Something else here</a>
224
      <div class="dropdown-divider"></div>
225
      <a class="dropdown-item" href="#">Separated link</a>
226
    </div>
227
  </div><!-- /btn-group -->
228
</div>
229
 
230
```html
231
<!-- Example split danger button -->
232
<div class="btn-group">
233
  <button type="button" class="btn btn-danger">Action</button>
234
  <button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
235
    <span class="sr-only">Toggle Dropdown</span>
236
  </button>
237
  <div class="dropdown-menu">
238
    <a class="dropdown-item" href="#">Action</a>
239
    <a class="dropdown-item" href="#">Another action</a>
240
    <a class="dropdown-item" href="#">Something else here</a>
241
    <div class="dropdown-divider"></div>
242
    <a class="dropdown-item" href="#">Separated link</a>
243
  </div>
244
</div>
245
```
246
 
247
## Sizing
248
 
249
Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.
250
 
251
<div class="bd-example">
252
  <div class="btn-group">
253
    <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
254
      Large button
255
    </button>
256
    <div class="dropdown-menu">
257
      <a class="dropdown-item" href="#">Action</a>
258
      <a class="dropdown-item" href="#">Another action</a>
259
      <a class="dropdown-item" href="#">Something else here</a>
260
      <div class="dropdown-divider"></div>
261
      <a class="dropdown-item" href="#">Separated link</a>
262
    </div>
263
  </div>
264
  <div class="btn-group">
265
    <button type="button" class="btn btn-lg btn-secondary">Large split button</button>
266
    <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
267
      <span class="sr-only">Toggle Dropdown</span>
268
    </button>
269
    <div class="dropdown-menu">
270
      <a class="dropdown-item" href="#">Action</a>
271
      <a class="dropdown-item" href="#">Another action</a>
272
      <a class="dropdown-item" href="#">Something else here</a>
273
      <div class="dropdown-divider"></div>
274
      <a class="dropdown-item" href="#">Separated link</a>
275
    </div>
276
  </div>
277
</div>
278
 
279
```html
280
<!-- Large button groups (default and split) -->
281
<div class="btn-group">
282
  <button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
283
    Large button
284
  </button>
285
  <div class="dropdown-menu">
286
    ...
287
  </div>
288
</div>
289
<div class="btn-group">
290
  <button class="btn btn-secondary btn-lg" type="button">
291
    Large split button
292
  </button>
293
  <button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
294
    <span class="sr-only">Toggle Dropdown</span>
295
  </button>
296
  <div class="dropdown-menu">
297
    ...
298
  </div>
299
</div>
300
```
301
 
302
<div class="bd-example">
303
  <div class="btn-group">
304
    <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
305
      Small button
306
    </button>
307
    <div class="dropdown-menu">
308
      <a class="dropdown-item" href="#">Action</a>
309
      <a class="dropdown-item" href="#">Another action</a>
310
      <a class="dropdown-item" href="#">Something else here</a>
311
      <div class="dropdown-divider"></div>
312
      <a class="dropdown-item" href="#">Separated link</a>
313
    </div>
314
  </div>
315
  <div class="btn-group">
316
    <button type="button" class="btn btn-sm btn-secondary">Small split button</button>
317
    <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
318
      <span class="sr-only">Toggle Dropdown</span>
319
    </button>
320
    <div class="dropdown-menu">
321
      <a class="dropdown-item" href="#">Action</a>
322
      <a class="dropdown-item" href="#">Another action</a>
323
      <a class="dropdown-item" href="#">Something else here</a>
324
      <div class="dropdown-divider"></div>
325
      <a class="dropdown-item" href="#">Separated link</a>
326
    </div>
327
  </div>
328
</div>
329
 
330
```html
331
<!-- Small button groups (default and split) -->
332
<div class="btn-group">
333
  <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
334
    Small button
335
  </button>
336
  <div class="dropdown-menu">
337
    ...
338
  </div>
339
</div>
340
<div class="btn-group">
341
  <button class="btn btn-secondary btn-sm" type="button">
342
    Small split button
343
  </button>
344
  <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
345
    <span class="sr-only">Toggle Dropdown</span>
346
  </button>
347
  <div class="dropdown-menu">
348
    ...
349
  </div>
350
</div>
351
```
352
 
353
## Directions
354
 
355
### Dropup
356
 
357
Trigger dropdown menus above elements by adding `.dropup` to the parent element.
358
 
359
<div class="bd-example">
360
  <div class="btn-group dropup">
361
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
362
      Dropup
363
    </button>
364
    <div class="dropdown-menu">
365
      <a class="dropdown-item" href="#">Action</a>
366
      <a class="dropdown-item" href="#">Another action</a>
367
      <a class="dropdown-item" href="#">Something else here</a>
368
      <div class="dropdown-divider"></div>
369
      <a class="dropdown-item" href="#">Separated link</a>
370
    </div>
371
  </div>
372
  <div class="btn-group dropup">
373
    <button type="button" class="btn btn-secondary">
374
      Split dropup
375
    </button>
376
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
377
      <span class="sr-only">Toggle Dropdown</span>
378
    </button>
379
    <div class="dropdown-menu">
380
      <a class="dropdown-item" href="#">Action</a>
381
      <a class="dropdown-item" href="#">Another action</a>
382
      <a class="dropdown-item" href="#">Something else here</a>
383
      <div class="dropdown-divider"></div>
384
      <a class="dropdown-item" href="#">Separated link</a>
385
    </div>
386
  </div>
387
</div>
388
 
389
```html
390
<!-- Default dropup button -->
391
<div class="btn-group dropup">
392
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
393
    Dropup
394
  </button>
395
  <div class="dropdown-menu">
396
    <!-- Dropdown menu links -->
397
  </div>
398
</div>
399
 
400
<!-- Split dropup button -->
401
<div class="btn-group dropup">
402
  <button type="button" class="btn btn-secondary">
403
    Split dropup
404
  </button>
405
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
406
    <span class="sr-only">Toggle Dropdown</span>
407
  </button>
408
  <div class="dropdown-menu">
409
    <!-- Dropdown menu links -->
410
  </div>
411
</div>
412
```
413
 
414
### Dropright
415
 
416
Trigger dropdown menus at the right of the elements by adding `.dropright` to the parent element.
417
 
418
<div class="bd-example">
419
  <div class="btn-group dropright">
420
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
421
      Dropright
422
    </button>
423
    <div class="dropdown-menu">
424
      <a class="dropdown-item" href="#">Action</a>
425
      <a class="dropdown-item" href="#">Another action</a>
426
      <a class="dropdown-item" href="#">Something else here</a>
427
      <div class="dropdown-divider"></div>
428
      <a class="dropdown-item" href="#">Separated link</a>
429
    </div>
430
  </div>
431
  <div class="btn-group dropright">
432
    <button type="button" class="btn btn-secondary">
433
      Split dropright
434
    </button>
435
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
436
      <span class="sr-only">Toggle Dropright</span>
437
    </button>
438
    <div class="dropdown-menu">
439
      <a class="dropdown-item" href="#">Action</a>
440
      <a class="dropdown-item" href="#">Another action</a>
441
      <a class="dropdown-item" href="#">Something else here</a>
442
      <div class="dropdown-divider"></div>
443
      <a class="dropdown-item" href="#">Separated link</a>
444
    </div>
445
  </div>
446
</div>
447
 
448
```html
449
<!-- Default dropright button -->
450
<div class="btn-group dropright">
451
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
452
    Dropright
453
  </button>
454
  <div class="dropdown-menu">
455
    <!-- Dropdown menu links -->
456
  </div>
457
</div>
458
 
459
<!-- Split dropright button -->
460
<div class="btn-group dropright">
461
  <button type="button" class="btn btn-secondary">
462
    Split dropright
463
  </button>
464
  <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
465
    <span class="sr-only">Toggle Dropright</span>
466
  </button>
467
  <div class="dropdown-menu">
468
    <!-- Dropdown menu links -->
469
  </div>
470
</div>
471
```
472
 
473
### Dropleft
474
 
475
Trigger dropdown menus at the left of the elements by adding `.dropleft` to the parent element.
476
 
477
<div class="bd-example">
478
  <div class="btn-group dropleft">
479
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
480
      Dropleft
481
    </button>
482
    <div class="dropdown-menu">
483
      <a class="dropdown-item" href="#">Action</a>
484
      <a class="dropdown-item" href="#">Another action</a>
485
      <a class="dropdown-item" href="#">Something else here</a>
486
      <div class="dropdown-divider"></div>
487
      <a class="dropdown-item" href="#">Separated link</a>
488
    </div>
489
  </div>
490
  <div class="btn-group">
491
    <div class="btn-group dropleft">
492
      <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
493
        <span class="sr-only">Toggle Dropleft</span>
494
      </button>
495
      <div class="dropdown-menu">
496
        <a class="dropdown-item" href="#">Action</a>
497
        <a class="dropdown-item" href="#">Another action</a>
498
        <a class="dropdown-item" href="#">Something else here</a>
499
        <div class="dropdown-divider"></div>
500
        <a class="dropdown-item" href="#">Separated link</a>
501
      </div>
502
    </div>
503
    <button type="button" class="btn btn-secondary">
504
      Split dropleft
505
    </button>
506
  </div>
507
</div>
508
 
509
```html
510
<!-- Default dropleft button -->
511
<div class="btn-group dropleft">
512
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
513
    Dropleft
514
  </button>
515
  <div class="dropdown-menu">
516
    <!-- Dropdown menu links -->
517
  </div>
518
</div>
519
 
520
<!-- Split dropleft button -->
521
<div class="btn-group">
522
  <div class="btn-group dropleft">
523
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
524
      <span class="sr-only">Toggle Dropleft</span>
525
    </button>
526
    <div class="dropdown-menu">
527
      <!-- Dropdown menu links -->
528
    </div>
529
  </div>
530
  <button type="button" class="btn btn-secondary">
531
    Split dropleft
532
  </button>
533
</div>
534
```
535
 
536
## Menu items
537
 
538
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.
539
 
540
{{< example >}}
541
<div class="dropdown">
542
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
543
    Dropdown
544
  </button>
545
  <div class="dropdown-menu">
546
    <button class="dropdown-item" type="button">Action</button>
547
    <button class="dropdown-item" type="button">Another action</button>
548
    <button class="dropdown-item" type="button">Something else here</button>
549
  </div>
550
</div>
551
{{< /example >}}
552
 
553
You can also create non-interactive dropdown items with `.dropdown-item-text`. Feel free to style further with custom CSS or text utilities.
554
 
555
{{< example >}}
556
<div class="dropdown-menu">
557
  <span class="dropdown-item-text">Dropdown item text</span>
558
  <a class="dropdown-item" href="#">Action</a>
559
  <a class="dropdown-item" href="#">Another action</a>
560
  <a class="dropdown-item" href="#">Something else here</a>
561
</div>
562
{{< /example >}}
563
 
564
### Active
565
 
566
Add `.active` to items in the dropdown to **style them as active**.
567
 
568
{{< example >}}
569
<div class="dropdown-menu">
570
  <a class="dropdown-item" href="#">Regular link</a>
571
  <a class="dropdown-item active" href="#">Active link</a>
572
  <a class="dropdown-item" href="#">Another link</a>
573
</div>
574
{{< /example >}}
575
 
576
### Disabled
577
 
578
Add `.disabled` to items in the dropdown to **style them as disabled**.
579
 
580
{{< example >}}
581
<div class="dropdown-menu">
582
  <a class="dropdown-item" href="#">Regular link</a>
583
  <a class="dropdown-item disabled">Disabled link</a>
584
  <a class="dropdown-item" href="#">Another link</a>
585
</div>
586
{{< /example >}}
587
 
588
## Menu alignment
589
 
590
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.
591
 
592
{{< callout info >}}
593
**Heads up!** Dropdowns are positioned thanks to Popper (except when they are contained in a navbar).
594
{{< /callout >}}
595
 
596
{{< example >}}
597
<div class="btn-group">
598
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
599
    Right-aligned menu
600
  </button>
601
  <div class="dropdown-menu dropdown-menu-right">
602
    <button class="dropdown-item" type="button">Action</button>
603
    <button class="dropdown-item" type="button">Another action</button>
604
    <button class="dropdown-item" type="button">Something else here</button>
605
  </div>
606
</div>
607
{{< /example >}}
608
 
609
### Responsive alignment
610
 
611
If you want to use responsive alignment, disable dynamic positioning by adding the `data-display="static"` attribute and use the responsive variation classes.
612
 
613
To align **right** the dropdown menu with the given breakpoint or larger, add `.dropdown-menu{-sm|-md|-lg|-xl}-right`.
614
 
615
{{< example >}}
616
<div class="btn-group">
617
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-expanded="false">
618
    Left-aligned but right aligned when large screen
619
  </button>
620
  <div class="dropdown-menu dropdown-menu-lg-right">
621
    <button class="dropdown-item" type="button">Action</button>
622
    <button class="dropdown-item" type="button">Another action</button>
623
    <button class="dropdown-item" type="button">Something else here</button>
624
  </div>
625
</div>
626
{{< /example >}}
627
 
628
To align **left** the dropdown menu with the given breakpoint or larger, add `.dropdown-menu-right` and `.dropdown-menu{-sm|-md|-lg|-xl}-left`.
629
 
630
{{< example >}}
631
<div class="btn-group">
632
  <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" data-display="static" aria-expanded="false">
633
    Right-aligned but left aligned when large screen
634
  </button>
635
  <div class="dropdown-menu dropdown-menu-right dropdown-menu-lg-left">
636
    <button class="dropdown-item" type="button">Action</button>
637
    <button class="dropdown-item" type="button">Another action</button>
638
    <button class="dropdown-item" type="button">Something else here</button>
639
  </div>
640
</div>
641
{{< /example >}}
642
 
643
Note that you don't need to add a `data-display="static"` attribute to dropdown buttons in navbars, since Popper isn't used in navbars.
644
 
645
## Menu content
646
 
647
### Headers
648
 
649
Add a header to label sections of actions in any dropdown menu.
650
 
651
{{< example >}}
652
<div class="dropdown-menu">
653
  <h6 class="dropdown-header">Dropdown header</h6>
654
  <a class="dropdown-item" href="#">Action</a>
655
  <a class="dropdown-item" href="#">Another action</a>
656
</div>
657
{{< /example >}}
658
 
659
### Dividers
660
 
661
Separate groups of related menu items with a divider.
662
 
663
{{< example >}}
664
<div class="dropdown-menu">
665
  <a class="dropdown-item" href="#">Action</a>
666
  <a class="dropdown-item" href="#">Another action</a>
667
  <a class="dropdown-item" href="#">Something else here</a>
668
  <div class="dropdown-divider"></div>
669
  <a class="dropdown-item" href="#">Separated link</a>
670
</div>
671
{{< /example >}}
672
 
673
### Text
674
 
675
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.
676
 
677
{{< example >}}
678
<div class="dropdown-menu p-4 text-muted" style="max-width: 200px;">
679
  <p>
680
    Some example text that's free-flowing within the dropdown menu.
681
  </p>
682
  <p class="mb-0">
683
    And this is more example text.
684
  </p>
685
</div>
686
{{< /example >}}
687
 
688
### Forms
689
 
690
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.
691
 
692
{{< example >}}
693
<div class="dropdown-menu">
694
  <form class="px-4 py-3">
695
    <div class="form-group">
696
      <label for="exampleDropdownFormEmail1">Email address</label>
697
      <input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
698
    </div>
699
    <div class="form-group">
700
      <label for="exampleDropdownFormPassword1">Password</label>
701
      <input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
702
    </div>
703
    <div class="form-group">
704
      <div class="form-check">
705
        <input type="checkbox" class="form-check-input" id="dropdownCheck">
706
        <label class="form-check-label" for="dropdownCheck">
707
          Remember me
708
        </label>
709
      </div>
710
    </div>
711
    <button type="submit" class="btn btn-primary">Sign in</button>
712
  </form>
713
  <div class="dropdown-divider"></div>
714
  <a class="dropdown-item" href="#">New around here? Sign up</a>
715
  <a class="dropdown-item" href="#">Forgot password?</a>
716
</div>
717
{{< /example >}}
718
 
719
{{< example >}}
720
<form class="dropdown-menu p-4">
721
  <div class="form-group">
722
    <label for="exampleDropdownFormEmail2">Email address</label>
723
    <input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="email@example.com">
724
  </div>
725
  <div class="form-group">
726
    <label for="exampleDropdownFormPassword2">Password</label>
727
    <input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
728
  </div>
729
  <div class="form-group">
730
    <div class="form-check">
731
      <input type="checkbox" class="form-check-input" id="dropdownCheck2">
732
      <label class="form-check-label" for="dropdownCheck2">
733
        Remember me
734
      </label>
735
    </div>
736
  </div>
737
  <button type="submit" class="btn btn-primary">Sign in</button>
738
</form>
739
{{< /example >}}
740
 
741
## Dropdown options
742
 
743
Use `data-offset` or `data-reference` to change the location of the dropdown.
744
 
745
{{< example >}}
746
<div class="d-flex">
747
  <div class="dropdown mr-1">
748
    <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-expanded="false" data-offset="10,20">
749
      Offset
750
    </button>
751
    <div class="dropdown-menu">
752
      <a class="dropdown-item" href="#">Action</a>
753
      <a class="dropdown-item" href="#">Another action</a>
754
      <a class="dropdown-item" href="#">Something else here</a>
755
    </div>
756
  </div>
757
  <div class="btn-group">
758
    <button type="button" class="btn btn-secondary">Reference</button>
759
    <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false" data-reference="parent">
760
      <span class="sr-only">Toggle Dropdown</span>
761
    </button>
762
    <div class="dropdown-menu">
763
      <a class="dropdown-item" href="#">Action</a>
764
      <a class="dropdown-item" href="#">Another action</a>
765
      <a class="dropdown-item" href="#">Something else here</a>
766
      <div class="dropdown-divider"></div>
767
      <a class="dropdown-item" href="#">Separated link</a>
768
    </div>
769
  </div>
770
</div>
771
{{< /example >}}
772
 
773
## Usage
774
 
775
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the `.show` class on the parent `.dropdown-menu`. 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.
776
 
777
{{< callout info >}}
778
On touch-enabled devices, opening a dropdown adds empty (`$.noop`) `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.
779
{{< /callout >}}
780
 
781
### Via data attributes
782
 
783
Add `data-toggle="dropdown"` to a link or button to toggle a dropdown.
784
 
785
```html
786
<div class="dropdown">
787
  <button type="button" data-toggle="dropdown" aria-expanded="false">
788
    Dropdown trigger
789
  </button>
790
  <div class="dropdown-menu">
791
    ...
792
  </div>
793
</div>
794
```
795
 
796
### Via JavaScript
797
 
798
Call the dropdowns via JavaScript:
799
 
800
```js
801
$('.dropdown-toggle').dropdown()
802
```
803
 
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.
808
{{< /callout >}}
809
 
810
### Options
811
 
812
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-offset=""`.
813
 
814
<table class="table table-bordered table-striped">
815
  <thead>
816
    <tr>
817
      <th style="width: 100px;">Name</th>
818
      <th style="width: 100px;">Type</th>
819
      <th style="width: 50px;">Default</th>
820
      <th>Description</th>
821
    </tr>
822
  </thead>
823
  <tbody>
824
    <tr>
825
      <td>offset</td>
826
      <td>number | string | function</td>
827
      <td>0</td>
828
      <td>
829
        <p>Offset of the dropdown relative to its target.</p>
830
        <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>
831
        <p>For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..offset.offset">offset docs</a>.</p>
832
      </td>
833
    </tr>
834
    <tr>
835
      <td>flip</td>
836
      <td>boolean</td>
837
      <td>true</td>
838
      <td>Allow Dropdown to flip in case of an overlapping on the reference element. For more information refer to Popper's <a href="https://popper.js.org/docs/v1/#modifiers..flip.enabled">flip docs</a>.</td>
839
    </tr>
840
    <tr>
841
      <td>boundary</td>
842
      <td>string | element</td>
843
      <td>'scrollParent'</td>
844
      <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's <a href="https://popper.js.org/docs/v1/#modifiers..preventOverflow.boundariesElement">preventOverflow docs</a>.</td>
845
    </tr>
846
    <tr>
847
      <td>reference</td>
848
      <td>string | element</td>
849
      <td>'toggle'</td>
850
      <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's <a href="https://popper.js.org/docs/v1/#referenceObject">referenceObject docs</a>.</td>
851
    </tr>
852
    <tr>
853
      <td>display</td>
854
      <td>string</td>
855
      <td>'dynamic'</td>
856
      <td>By default, we use Popper for dynamic positioning. Disable this with <code>static</code>.</td>
857
    </tr>
858
    <tr>
859
      <td>popperConfig</td>
860
      <td>null | object</td>
861
      <td>null</td>
862
      <td>To change Bootstrap's default Popper config, see <a href="https://popper.js.org/docs/v1/#Popper.Defaults">Popper's configuration</a></td>
863
    </tr>
864
  </tbody>
865
</table>
866
 
867
Note when `boundary` is set to any value other than `'scrollParent'`, the style `position: static` is applied to the `.dropdown` container.
868
 
869
### Methods
870
 
871
| Method | Description |
872
| --- | --- |
873
| `$().dropdown('toggle')` | Toggles the dropdown menu of a given navbar or tabbed navigation. |
874
| `$().dropdown('show')` | Shows the dropdown menu of a given navbar or tabbed navigation. |
875
| `$().dropdown('hide')` | Hides the dropdown menu of a given navbar or tabbed navigation. |
876
| `$().dropdown('update')` | Updates the position of an element's dropdown. |
877
| `$().dropdown('dispose')` | Destroys an element's dropdown. |
878
 
879
### Events
880
 
881
All dropdown events are fired at the `.dropdown-menu`'s parent element and have a `relatedTarget` property, whose value is the toggling anchor element.
882
`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.
883
 
884
| Event | Description |
885
| --- | --- |
886
| `show.bs.dropdown` | This event fires immediately when the show instance method is called. |
887
| `shown.bs.dropdown` | This event is fired when the dropdown has been made visible to the user (will wait for CSS transitions, to complete). |
888
| `hide.bs.dropdown` | This event is fired immediately when the hide instance method has been called. |
889
| `hidden.bs.dropdown`| This event is fired when the dropdown has finished being hidden from the user (will wait for CSS transitions, to complete). |
890
 
891
```js
892
$('#myDropdown').on('show.bs.dropdown', function () {
893
  // do something...
894
})
895
```