Modal Popup – Twitter Bootstrap

Just a quick post on how to create a modal popup using twitter bootstrap:

Step #1:

Add the following to a page element such as a link or button in order to open the modal:



<a type="button" class="btn" style="width:100%;" href="#test_modal" data-toggle="modal">Add Image</a>

Step #2:
Create a content div i.e.

  1. <div class="modal fade" id="test_modal">
  2.   <div class="modal-header">
  3.     <a class="close" data-dismiss="modal">&times;</a>
  4.     <h3>Modal Header</h3>
  5.   </div>
  6.   <div class="modal-body">
  7.     <p>Test Modal</p>
  8.   </div>
  9.   <div class="modal-footer">
  10.     <a href="#" class="btn" data-dismiss="modal">Close</a>
  11.     <a href="#" class="btn btn-primary">Save Changes</a>
  12.   </div>
  13. </div>

And that’s all there is to it, just make sure you’ve included the bootstrap-modal.js file.

Modal Popup - Twitter Bootstrap

Modal Popup - Twitter Bootstrap

Open Twitter Bootstrap Modal Popup on Page Load:
In order to open the modal dialog on page load simply add the following JavaScript:

  1. <script type="text/javascript">
  2.     $(document).ready(function(){
  3.         $('#test_modal').modal('show');
  4.     });
  5. </script>

You can also close the modal popup manually by using the following JavaScript:

  1. <script type="text/javascript">
  2. $('#test_modal').modal('hide')
  3. </script>

Adjust Modal Width:
In order to adjust the modal width you have two options, Javascript or CSS. You can use the following JavaScript mentioned on GitHub:

  1. <script type="text/javascript">
  2.    $('#test_modal').modal({
  3.         backdrop: true,
  4.         keyboard: true,
  5.         show: false \\remove this if you don't want it to show straight away
  6.     }).css({
  7.         width: 'auto',
  8.         'margin-left': function () {
  9.             return -($(this).width() / 2);
  10.         }
  11.     });
  12. </script>

Or this CSS from StackOverflow:

  1. body .modal-admin {
  2.     //new custom width
  3.     width: 750px;
  4.     //must be half of the width, minus scrollbar on the left (30px)
  5.     margin: -345px 0 0 -375px;
  6. }

Show modal when the user scrolls past certain div
I’ve received another request on how to show the modal when a user scrolls to a certain div. The following should work, but please let me know if you have any issues.

  1. /* Wait for document to load */
  2. $(document).ready(function(){
  4. $(window).scroll(function(){
  6. //Retrieve scroll distance from top
  7. var top = $(window).scrollTop();
  9. //Retrieve the divs distance from top of page
  10. var div_distance = $('my_div').offset().top;
  12. //Use this if you want a bit of a variable distance, for instance if they scroll 150 pixels past the div
  13. var buffer_distance = 150;
  15. //Check if scrolled past div
  16. if(top >= div_distance && top <= (div_distance + buffer_distance)){
  18. //Show modal - you may want to add checks etc to see if it's already show
  19. $('#test_modal').modal('show');
  21. } else {
  23. //Hide the modal, have scrolled past - you may want to add some checks to see if it's already hidden etc
  24. $('#test_modal').modal('hide');                         
  25. }        
  26. }); 
  27. });

15 thoughts on “Modal Popup – Twitter Bootstrap

  1. Tom Harrison

    This is helpful in the case where the model is more like a popup (the modal HTML is there, just hidden) — I am trying to make my user edit pages open in a modal. So there are existing Rails routes (/users/edit/1) controllers, and views, then from one page I want to click a link to open the new page, or alternatively call the controller method as AJAX, have it insert the modal HTML and then display it. Any ideas?

    1. Errors Post author

      Hey Tom, this isn’t too difficult – the easiest way is to simply bind an onclick event to the button you want to use to open the modal and then use this to fetch the partial via ajax. You can then render it using escape_javascript etc.

      I tend to place the modal on the page before hand and then fetch just the form itself via ajax. Just place a loader gif in the modal and then replace it with the partial once it has been fetched. This adds a little extra overhead, however it provides a better ux (in my opinion)

    1. Chris Owens Post author

      Hey Cursos,

      Sorry about the late reply, have been busy! I’ve updated the post to include a quick guide on how to do it, please let me know if you have any trouble.

        1. Chris Owens Post author

          Hey Ddnhf,

          Seem to be a few issues with styles there:

          Using Chrome:
          – Hitting the login button the overlay z-index is too high. Not sure if you’ve manually set the z-index somewhere but it is greater than the modal itself. Adjust the “.modal-backdrop” class to have a lower z-index to fix this bit.
          – The sign up button seems to work fine

          And just any FYI, no text is appearing in the modals in IE9. If I’m not looking at the right issues please let me know.


  2. zuhry

    i create one modal with remote content, in remote content page have pagination control, when i click the pagination number, my browser directly redirect to the oringinal url content. so could you help me for fix it?

    1. Chris Owens Post author

      Hey Zuhry,

      Happy to help out where I can, but you’ll need to give me a fair bit more info. Do you have a link or jsfiddle to it running?



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.