95b393f7af45812fcb5577977f56a071af0f1438.svn-base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. ---
  2. layout: docs
  3. title: Modal
  4. group: components
  5. ---
  6. Modals are streamlined, but flexible, dialog prompts with the minimum required functionality and smart defaults.
  7. ## Contents
  8. * Will be replaced with the ToC, excluding the "Contents" header
  9. {:toc}
  10. **Due to how HTML5 defines its semantics, the `autofocus` HTML attribute has no effect in Bootstrap modals.** To achieve the same effect, use some custom JavaScript:
  11. {% highlight js %}
  12. $('#myModal').on('shown.bs.modal', function () {
  13. $('#myInput').focus()
  14. })
  15. {% endhighlight %}
  16. {% callout warning %}
  17. #### Multiple open modals not supported
  18. Be sure not to open a modal while another is still visible. Showing more than one modal at a time requires custom code.
  19. {% endcallout %}
  20. {% callout warning %}
  21. #### Modal markup placement
  22. Always try to place a modal's HTML code in a top-level position in your document to avoid other components affecting the modal's appearance and/or functionality.
  23. {% endcallout %}
  24. {% callout warning %}
  25. #### Mobile device caveats
  26. There are some caveats regarding using modals on mobile devices. [See our browser support docs](../getting-started/#support-fixed-position-keyboards) for details.
  27. {% endcallout %}
  28. ### Static example
  29. A rendered modal with header, body, and set of actions in the footer.</p>
  30. <div class="bd-example bd-example-modal">
  31. <div class="modal">
  32. <div class="modal-dialog" role="document">
  33. <div class="modal-content">
  34. <div class="modal-header">
  35. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  36. <span aria-hidden="true">&times;</span>
  37. <span class="sr-only">Close</span>
  38. </button>
  39. <h4 class="modal-title">Modal title</h4>
  40. </div>
  41. <div class="modal-body">
  42. <p>One fine body&hellip;</p>
  43. </div>
  44. <div class="modal-footer">
  45. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  46. <button type="button" class="btn btn-primary">Save changes</button>
  47. </div>
  48. </div><!-- /.modal-content -->
  49. </div><!-- /.modal-dialog -->
  50. </div><!-- /.modal -->
  51. </div>
  52. {% highlight html %}
  53. <div class="modal fade">
  54. <div class="modal-dialog" role="document">
  55. <div class="modal-content">
  56. <div class="modal-header">
  57. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  58. <span aria-hidden="true">&times;</span>
  59. <span class="sr-only">Close</span>
  60. </button>
  61. <h4 class="modal-title">Modal title</h4>
  62. </div>
  63. <div class="modal-body">
  64. <p>One fine body&hellip;</p>
  65. </div>
  66. <div class="modal-footer">
  67. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  68. <button type="button" class="btn btn-primary">Save changes</button>
  69. </div>
  70. </div><!-- /.modal-content -->
  71. </div><!-- /.modal-dialog -->
  72. </div><!-- /.modal -->
  73. {% endhighlight %}
  74. ### Live demo
  75. Toggle a modal via JavaScript by clicking the button below. It will slide down and fade in from the top of the page.
  76. <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  77. <div class="modal-dialog" role="document">
  78. <div class="modal-content">
  79. <div class="modal-header">
  80. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  81. <span aria-hidden="true">&times;</span>
  82. <span class="sr-only">Close</span>
  83. </button>
  84. <h4 class="modal-title" id="myModalLabel">Modal title</h4>
  85. </div>
  86. <div class="modal-body">
  87. <h4>Text in a modal</h4>
  88. <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>
  89. <h4>Popover in a modal</h4>
  90. <p>This <a href="javascript:;" role="button" class="btn btn-secondary popover-test" title="A Title" data-content="And here's some amazing content. It's very engaging. right?">button</a> should trigger a popover on click.</p>
  91. <h4>Tooltips in a modal</h4>
  92. <p><a href="javascript:;" class="tooltip-test" title="Tooltip">This link</a> and <a href="javascript:;" class="tooltip-test" title="Tooltip">that link</a> should have tooltips on hover.</p>
  93. <hr>
  94. <h4>Overflowing text to show scroll behavior</h4>
  95. <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
  96. <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
  97. <p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
  98. <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
  99. <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
  100. <p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
  101. <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
  102. <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
  103. <p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
  104. </div>
  105. <div class="modal-footer">
  106. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  107. <button type="button" class="btn btn-primary">Save changes</button>
  108. </div>
  109. </div><!-- /.modal-content -->
  110. </div><!-- /.modal-dialog -->
  111. </div>
  112. <div class="bd-example" style="padding-bottom: 24px;">
  113. <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  114. Launch demo modal
  115. </button>
  116. </div>
  117. {% highlight html %}
  118. <!-- Button trigger modal -->
  119. <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  120. Launch demo modal
  121. </button>
  122. <!-- Modal -->
  123. <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  124. <div class="modal-dialog" role="document">
  125. <div class="modal-content">
  126. <div class="modal-header">
  127. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  128. <span aria-hidden="true">&times;</span>
  129. <span class="sr-only">Close</span>
  130. </button>
  131. <h4 class="modal-title" id="myModalLabel">Modal title</h4>
  132. </div>
  133. <div class="modal-body">
  134. ...
  135. </div>
  136. <div class="modal-footer">
  137. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  138. <button type="button" class="btn btn-primary">Save changes</button>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. {% endhighlight %}
  144. {% callout warning %}
  145. #### Make modals accessible
  146. Be sure to add `role="dialog"` and `aria-labelledby="..."``, referencing the modal title, to `.modal`, and `role="document"` to the `.modal-dialog` itself.
  147. Additionally, you may give a description of your modal dialog with `aria-describedby` on `.modal`.
  148. {% endcallout %}
  149. {% callout info %}
  150. #### Embedding YouTube videos
  151. Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more. [See this helpful Stack Overflow post](http://stackoverflow.com/questions/18622508/bootstrap-3-and-youtube-in-modal) for more information.
  152. {% endcallout %}
  153. ## Optional sizes
  154. Modals have two optional sizes, available via modifier classes to be placed on a `.modal-dialog`.
  155. <div class="bd-example">
  156. <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
  157. <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm">Small modal</button>
  158. </div>
  159. {% highlight html %}
  160. <!-- Large modal -->
  161. <button class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
  162. <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  163. <div class="modal-dialog modal-lg">
  164. <div class="modal-content">
  165. ...
  166. </div>
  167. </div>
  168. </div>
  169. <!-- Small modal -->
  170. <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm">Small modal</button>
  171. <div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  172. <div class="modal-dialog modal-sm">
  173. <div class="modal-content">
  174. ...
  175. </div>
  176. </div>
  177. </div>
  178. {% endhighlight %}
  179. <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  180. <div class="modal-dialog modal-lg">
  181. <div class="modal-content">
  182. <div class="modal-header">
  183. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  184. <span aria-hidden="true">&times;</span>
  185. <span class="sr-only">Close</span>
  186. </button>
  187. <h4 class="modal-title" id="myLargeModalLabel">Large modal</h4>
  188. </div>
  189. <div class="modal-body">
  190. ...
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. <div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  196. <div class="modal-dialog modal-sm">
  197. <div class="modal-content">
  198. <div class="modal-header">
  199. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  200. <span aria-hidden="true">&times;</span>
  201. <span class="sr-only">Close</span>
  202. </button>
  203. <h4 class="modal-title" id="mySmallModalLabel">Small modal</h4>
  204. </div>
  205. <div class="modal-body">
  206. ...
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. ## Remove animation
  212. For modals that simply appear rather than fade in to view, remove the `.fade` class from your modal markup.
  213. {% highlight html %}
  214. <div class="modal" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
  215. ...
  216. </div>
  217. {% endhighlight %}
  218. ## Using the grid system
  219. To take advantage of the Bootstrap grid system within a modal, just nest `.container-fluid within the `.modal-body` and then use the normal grid system classes within this container.
  220. {% example html %}
  221. <div id="gridSystemModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridModalLabel" aria-hidden="true">
  222. <div class="modal-dialog" role="document">
  223. <div class="modal-content">
  224. <div class="modal-header">
  225. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  226. <h4 class="modal-title" id="gridModalLabel">Modal title</h4>
  227. </div>
  228. <div class="modal-body">
  229. <div class="row">
  230. <div class="col-md-4">.col-md-4</div>
  231. <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div>
  232. </div>
  233. <div class="row">
  234. <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div>
  235. <div class="col-md-2 col-md-offset-4">.col-md-2 .col-md-offset-4</div>
  236. </div>
  237. <div class="row">
  238. <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div>
  239. </div>
  240. <div class="row">
  241. <div class="col-sm-9">
  242. Level 1: .col-sm-9
  243. <div class="row">
  244. <div class="col-xs-8 col-sm-6">
  245. Level 2: .col-xs-8 .col-sm-6
  246. </div>
  247. <div class="col-xs-4 col-sm-6">
  248. Level 2: .col-xs-4 .col-sm-6
  249. </div>
  250. </div>
  251. </div>
  252. </div>
  253. </div>
  254. <div class="modal-footer">
  255. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  256. <button type="button" class="btn btn-primary">Save changes</button>
  257. </div>
  258. </div>
  259. </div>
  260. </div>
  261. <div class="bd-example bd-example-padded-bottom">
  262. <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#gridSystemModal">
  263. Launch demo modal
  264. </button>
  265. </div>
  266. {% endexample %}
  267. ## Varying modal content based on trigger button
  268. Have a bunch of buttons that all trigger the same modal, just with slightly different contents? Use `event.relatedTarget` and [HTML `data-*` attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) (possibly [via jQuery](http://api.jquery.com/data/)) to vary the contents of the modal depending on which button was clicked. See the Modal Events docs for details on `relatedTarget`.
  269. {% example html %}
  270. <div class="bd-example">
  271. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
  272. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
  273. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
  274. <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  275. <div class="modal-dialog" role="document">
  276. <div class="modal-content">
  277. <div class="modal-header">
  278. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  279. <span aria-hidden="true">&times;</span>
  280. <span class="sr-only">Close</span>
  281. </button>
  282. <h4 class="modal-title" id="exampleModalLabel">New message</h4>
  283. </div>
  284. <div class="modal-body">
  285. <form>
  286. <div class="form-group">
  287. <label for="recipient-name" class="control-label">Recipient:</label>
  288. <input type="text" class="form-control" id="recipient-name">
  289. </div>
  290. <div class="form-group">
  291. <label for="message-text" class="control-label">Message:</label>
  292. <textarea class="form-control" id="message-text"></textarea>
  293. </div>
  294. </form>
  295. </div>
  296. <div class="modal-footer">
  297. <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  298. <button type="button" class="btn btn-primary">Send message</button>
  299. </div>
  300. </div>
  301. </div>
  302. </div>
  303. </div>
  304. {% endexample %}
  305. {% highlight js %}
  306. $('#exampleModal').on('show.bs.modal', function (event) {
  307. var button = $(event.relatedTarget) // Button that triggered the modal
  308. var recipient = button.data('whatever') // Extract info from data-* attributes
  309. // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  310. // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  311. var modal = $(this)
  312. modal.find('.modal-title').text('New message to ' + recipient)
  313. modal.find('.modal-body input').val(recipient)
  314. })
  315. {% endhighlight %}
  316. ## Modals with dynamic heights
  317. If the height of a modal changes while it is open, you should call `$('#myModal').data('bs.modal').handleUpdate()` to readjust the modal's position in case a scrollbar appears.
  318. ## Usage
  319. The modal plugin toggles your hidden content on demand, via data attributes or JavaScript. It also adds `.modal-open` to the `<body>` to override default scrolling behavior and generates a `.modal-backdrop` to provide a click area for dismissing shown modals when clicking outside the modal.
  320. ### Via data attributes
  321. Activate a modal without writing JavaScript. Set `data-toggle="modal"` on a controller element, like a button, along with a `data-target="#foo"` or `href="#foo"` to target a specific modal to toggle.
  322. {% highlight html %}
  323. <button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
  324. {% endhighlight %}
  325. ### Via JavaScript
  326. Call a modal with id `myModal` with a single line of JavaScript:
  327. {% highlight js %}$('#myModal').modal(options){% endhighlight %}
  328. ### Options
  329. Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-backdrop=""`.
  330. <div class="table-responsive">
  331. <table class="table table-bordered table-striped">
  332. <thead>
  333. <tr>
  334. <th style="width: 100px;">Name</th>
  335. <th style="width: 50px;">type</th>
  336. <th style="width: 50px;">default</th>
  337. <th>description</th>
  338. </tr>
  339. </thead>
  340. <tbody>
  341. <tr>
  342. <td>backdrop</td>
  343. <td>boolean or the string <code>'static'</code></td>
  344. <td>true</td>
  345. <td>Includes a modal-backdrop element. Alternatively, specify <code>static</code> for a backdrop which doesn't close the modal on click.</td>
  346. </tr>
  347. <tr>
  348. <td>keyboard</td>
  349. <td>boolean</td>
  350. <td>true</td>
  351. <td>Closes the modal when escape key is pressed</td>
  352. </tr>
  353. <tr>
  354. <td>show</td>
  355. <td>boolean</td>
  356. <td>true</td>
  357. <td>Shows the modal when initialized.</td>
  358. </tr>
  359. </tbody>
  360. </table>
  361. </div>
  362. ### Methods
  363. #### .modal(options)
  364. Activates your content as a modal. Accepts an optional options `object`.
  365. {% highlight js %}
  366. $('#myModal').modal({
  367. keyboard: false
  368. })
  369. {% endhighlight %}
  370. #### .modal('toggle')
  371. Manually toggles a modal. **Returns to the caller before the modal has actually been shown or hidden** (i.e. before the `shown.bs.modal` or `hidden.bs.modal` event occurs).
  372. {% highlight js %}$('#myModal').modal('toggle'){% endhighlight %}
  373. #### .modal('show')
  374. Manually opens a modal. **Returns to the caller before the modal has actually been shown** (i.e. before the `shown.bs.modal` event occurs).
  375. {% highlight js %}$('#myModal').modal('show'){% endhighlight %}
  376. #### .modal('hide')
  377. Manually hides a modal. **Returns to the caller before the modal has actually been hidden** (i.e. before the `hidden.bs.modal` event occurs).
  378. {% highlight js %}$('#myModal').modal('hide'){% endhighlight %}
  379. ### Events
  380. Bootstrap's modal class exposes a few events for hooking into modal functionality. All modal events are fired at the modal itself (i.e. at the `<div class="modal">`).
  381. <div class="table-responsive">
  382. <table class="table table-bordered table-striped">
  383. <thead>
  384. <tr>
  385. <th style="width: 150px;">Event Type</th>
  386. <th>Description</th>
  387. </tr>
  388. </thead>
  389. <tbody>
  390. <tr>
  391. <td>show.bs.modal</td>
  392. <td>This event fires immediately when the <code>show</code> instance method is called. If caused by a click, the clicked element is available as the <code>relatedTarget</code> property of the event.</td>
  393. </tr>
  394. <tr>
  395. <td>shown.bs.modal</td>
  396. <td>This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the <code>relatedTarget</code> property of the event.</td>
  397. </tr>
  398. <tr>
  399. <td>hide.bs.modal</td>
  400. <td>This event is fired immediately when the <code>hide</code> instance method has been called.</td>
  401. </tr>
  402. <tr>
  403. <td>hidden.bs.modal</td>
  404. <td>This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).</td>
  405. </tr>
  406. <tr>
  407. <td>loaded.bs.modal</td>
  408. <td>This event is fired when the modal has loaded content using the <code>remote</code> option.</td>
  409. </tr>
  410. </tbody>
  411. </table>
  412. </div>
  413. {% highlight js %}
  414. $('#myModal').on('hidden.bs.modal', function (e) {
  415. // do something...
  416. })
  417. {% endhighlight %}