How to resize Twitter Bootstrap modal dynamically based on the content
Dramatically resize a Bootstrap modal dynamically based on its content by applying CSS rules to .modal-body
. The golden tool, jQuery is used to adjust max-height
and overflow-y
once the modal is up in the shining browser:
With this snippet, you are telling the modal body that it can only consume the available screen space and not a pixel more, ensuring the king and queen (header and footer) remain visible without adding a scroll for the whole kingdom (modal).
A CSS Govt. order for resizing
Let's send a letter use CSS to tell our modal's Kingdom (.modal-dialog) about the rules:
An event handler using jQuery to react when we hide the modal:
JavaScript, the CSS-helper
Sometimes, CSS feels lonely and tired, and that's where JavaScript comes to the rescue, helping to dynamically calculate dimensions.
Fancy outfits for different devices
Oh, the variety!
Your modal should put on the correct outfit (size) based on the device. Media queries got you covered!
Interactive content: a beauty contest!
Forms, sliders or other interactive elements can be a little problematic - rather dramatic. They may change their size suddenly like a pop singer changes outfits. Be ready for them!
Smooth resizing: A lullaby for pixels
Just like a baby enjoying a lullaby, your modal also enjoys smooth resizing transitions. Take care of your modal baby!
UX - User's eXpectation from you!
Always keep an eye on user’s journey. Loading states, progress indicators can be critical for pigeon mail (modals loading external content like AJAX requests).
Was this article helpful?