Bird
Raised Fist0
Bootsrapmarkup~15 mins

Scrollable and centered modals in Bootsrap - Deep Dive

Choose your learning style10 modes available
Overview - Scrollable and centered modals
What is it?
Scrollable and centered modals are popup windows that appear on top of a webpage. They are designed to be vertically centered on the screen and allow users to scroll through content if it is too long to fit. This helps keep important information visible and accessible without leaving the current page.
Why it matters
Without scrollable and centered modals, users might miss important content or have to scroll the entire page awkwardly. Centering makes the modal feel balanced and focused, while scrolling ensures all content is reachable even on small screens. This improves user experience and keeps interactions smooth.
Where it fits
Before learning this, you should understand basic Bootstrap components and how modals work in general. After this, you can explore customizing modals further with animations, accessibility features, or integrating them with JavaScript for dynamic content.
Mental Model
Core Idea
A scrollable and centered modal is a popup box that stays in the middle of the screen and lets you scroll inside it if the content is too big.
Think of it like...
It's like a picture frame hanging perfectly centered on a wall, but if the picture inside is too tall, you can slide it up and down to see the whole image without moving the frame.
┌───────────────────────────────┐
│           Screen              │
│ ┌─────────────────────────┐ │
│ │       Centered Modal     │ │
│ │ ┌─────────────────────┐ │ │
│ │ │ Scrollable Content   │ │ │
│ │ │  (scroll up/down)    │ │ │
│ │ └─────────────────────┘ │ │
│ └─────────────────────────┘ │
└───────────────────────────────┘
Build-Up - 6 Steps
1
FoundationBasic Bootstrap Modal Setup
🤔
Concept: Learn how to create a simple modal using Bootstrap's built-in classes.
To create a modal, use a button with data attributes to trigger it, and a modal structure with classes like 'modal', 'modal-dialog', and 'modal-content'. This creates a popup that appears when the button is clicked.
Result
Clicking the button opens a centered popup with default size and no scrolling.
Understanding the basic modal structure is essential before adding features like scrolling or centering.
2
FoundationModal Content Overflow Problem
🤔
Concept: Recognize what happens when modal content is too long to fit on the screen.
If you add a lot of text or elements inside the modal body, the modal might grow beyond the screen height. Without scrolling, some content becomes hidden and inaccessible.
Result
Modal content overflows off the screen, making it hard to read or interact with all information.
Knowing this problem helps motivate why scrollable modals are needed.
3
IntermediateMaking Modals Scrollable
🤔Before reading on: do you think adding 'overflow: auto' to the modal body will make the modal scrollable or the entire modal scroll?
Concept: Use Bootstrap's 'modal-dialog-scrollable' class to enable scrolling inside the modal body only.
Add the class 'modal-dialog-scrollable' to the 'modal-dialog' div. This limits scrolling to the modal body area, keeping the header and footer fixed while the content scrolls.
Result
The modal stays a fixed size, and the content inside can be scrolled vertically if it is too long.
Knowing how to isolate scrolling inside the modal body improves usability and keeps important controls visible.
4
IntermediateCentering Modals Vertically
🤔Before reading on: do you think modals are centered vertically by default in Bootstrap or do you need to add a class?
Concept: Use the 'modal-dialog-centered' class to vertically center the modal on the screen.
Add the class 'modal-dialog-centered' to the 'modal-dialog' div. This uses Flexbox to center the modal vertically regardless of screen height.
Result
The modal appears in the vertical center of the viewport, making it more balanced and easier to focus on.
Centering modals improves visual focus and user experience, especially on tall screens.
5
AdvancedCombining Scrollable and Centered Modals
🤔Before reading on: do you think you can add both 'modal-dialog-scrollable' and 'modal-dialog-centered' classes together? What happens?
Concept: Combine both classes on the modal dialog to get a modal that is vertically centered and has scrollable content.
Add both 'modal-dialog-scrollable' and 'modal-dialog-centered' classes to the 'modal-dialog' div. Bootstrap handles the layout so the modal is centered and the body scrolls if needed.
Result
A modal that stays centered on screen and allows scrolling inside the content area when content is large.
Understanding how Bootstrap combines these classes helps build flexible modals for many use cases.
6
ExpertResponsive Behavior and Accessibility
🤔Before reading on: do you think scrollable and centered modals behave well on small screens and support keyboard navigation by default?
Concept: Explore how scrollable and centered modals adapt on small devices and how to ensure they remain accessible.
Bootstrap modals are responsive by default, but very tall content may still require scrolling. Use ARIA roles and keyboard focus management to keep modals accessible. Test tab order and screen reader announcements.
Result
Modals work well on phones and tablets, and users with disabilities can navigate them easily.
Accessibility and responsiveness are critical for real-world use and often overlooked in modal design.
Under the Hood
Bootstrap modals use a combination of HTML structure, CSS Flexbox, and JavaScript to show and hide the popup. The 'modal-dialog-centered' class applies Flexbox centering to the modal container. The 'modal-dialog-scrollable' class sets a max height and overflow auto on the modal body, enabling internal scrolling without affecting the whole page. JavaScript toggles visibility and manages focus to keep the modal interactive and accessible.
Why designed this way?
Modals needed to be easy to use and flexible across devices. Centering with Flexbox is simpler and more reliable than older methods like absolute positioning. Scrolling inside the modal body keeps headers and footers visible, improving usability. Bootstrap chose these patterns to balance simplicity, accessibility, and responsiveness.
Screen View
┌───────────────────────────────┐
│           Overlay             │
│ ┌─────────────────────────┐ │
│ │  Modal Container (Flex)  │ │
│ │ ┌─────────────────────┐ │ │
│ │ │ Modal Dialog        │ │ │
│ │ │ ┌─────────────────┐ │ │ │
│ │ │ │ Header (fixed)  │ │ │ │
│ │ │ │ Body (scroll)   │ │ │ │
│ │ │ │ Footer (fixed)  │ │ │ │
│ │ │ └─────────────────┘ │ │ │
│ │ └─────────────────────┘ │ │
│ └─────────────────────────┘ │
└───────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does adding 'modal-dialog-scrollable' make the entire modal scroll or just the content area? Commit to your answer.
Common Belief:Adding 'modal-dialog-scrollable' makes the whole modal scroll on the page.
Tap to reveal reality
Reality:It only makes the modal body content scrollable, keeping header and footer fixed.
Why it matters:If you expect the whole modal to scroll, you might miss that headers stay fixed, which is important for user controls and context.
Quick: Are modals vertically centered by default in Bootstrap? Commit to yes or no.
Common Belief:Bootstrap modals are always vertically centered without extra classes.
Tap to reveal reality
Reality:They are top-aligned by default; you must add 'modal-dialog-centered' to center vertically.
Why it matters:Assuming modals are centered can lead to poor design choices and unexpected layouts.
Quick: Can you combine 'modal-dialog-scrollable' and 'modal-dialog-centered' classes without issues? Commit to yes or no.
Common Belief:Combining these classes causes layout problems or is not supported.
Tap to reveal reality
Reality:Bootstrap supports combining them smoothly to get both scrolling and centering.
Why it matters:Knowing this allows building better modals without trial and error or custom hacks.
Quick: Do scrollable modals automatically handle accessibility like keyboard focus? Commit to yes or no.
Common Belief:Scrollable modals handle all accessibility concerns automatically.
Tap to reveal reality
Reality:While Bootstrap manages basic focus, developers must test and sometimes add ARIA attributes for full accessibility.
Why it matters:Ignoring accessibility can exclude users and cause legal or usability issues.
Expert Zone
1
Scrollable modals keep header and footer visible by fixing their position, which improves user context during long content reading.
2
Centering modals with Flexbox avoids issues with varying screen heights and dynamic content sizes compared to older absolute positioning.
3
Combining scrollable and centered classes requires understanding Bootstrap's CSS layering to avoid unexpected overflow or clipping.
When NOT to use
Avoid scrollable and centered modals when content is extremely large or interactive, as full-page layouts or dedicated pages provide better usability. For complex interactions, consider off-canvas panels or custom dialogs with advanced focus management.
Production Patterns
In real-world apps, scrollable and centered modals are used for forms, alerts, and detailed info popups. Developers often customize modal sizes, add animations, and ensure keyboard navigation and screen reader support for accessibility compliance.
Connections
Flexbox Layout
Builds-on
Understanding Flexbox is key to grasping how Bootstrap centers modals vertically and manages their layout.
Accessibility (a11y) Principles
Builds-on
Knowing accessibility helps ensure modals are usable by everyone, including keyboard users and screen reader users.
User Interface Design
Builds-on
Good modal design balances visibility, focus, and content access, which are core UI design principles.
Common Pitfalls
#1Modal content overflows and is cut off without scrolling.
Wrong approach:
Correct approach:
Root cause:Not adding 'modal-dialog-scrollable' class means the modal body does not scroll, causing overflow.
#2Modal appears at the top of the screen, not centered.
Wrong approach:
Correct approach:
Root cause:Missing 'modal-dialog-centered' class means no vertical centering.
#3Combining scrollable and centered classes causes layout break.
Wrong approach:
Correct approach:
Root cause:Adding custom CSS that conflicts with Bootstrap's styles breaks the layout.
Key Takeaways
Scrollable and centered modals improve user experience by keeping content accessible and visually balanced.
Bootstrap provides simple classes to enable scrolling inside the modal body and vertical centering using Flexbox.
Combining 'modal-dialog-scrollable' and 'modal-dialog-centered' classes creates flexible modals for many scenarios.
Accessibility and responsiveness must be considered to ensure modals work well for all users and devices.
Understanding the underlying CSS and Bootstrap structure helps avoid common pitfalls and customize modals effectively.

Practice

(1/5)
1. What does the Bootstrap class modal-dialog-scrollable do in a modal?
easy
A. Disables modal animations
B. Makes the modal content scrollable inside the popup
C. Changes the modal background color
D. Centers the modal vertically on the screen

Solution

  1. Step 1: Understand the class purpose

    The class modal-dialog-scrollable is designed to allow the modal content area to scroll if the content is too long.
  2. Step 2: Compare with other classes

    Unlike modal-dialog-centered which centers the modal, this class specifically controls scrolling inside the modal.
  3. Final Answer:

    Makes the modal content scrollable inside the popup -> Option B
  4. Quick Check:

    modal-dialog-scrollable = scrollable content [OK]
Hint: Scrollable modals use modal-dialog-scrollable class [OK]
Common Mistakes:
  • Confusing scrollable with centered behavior
  • Thinking it changes modal size
  • Assuming it affects modal background
2. Which is the correct way to add both scrollable and vertically centered behavior to a Bootstrap modal dialog?
easy
A. <div class='modal-dialog modal-dialog-scrollable modal-dialog-centered'>
B. <div class='modal-dialog scrollable-centered'>
C. <div class='modal-dialog scrollable centered'>
D. <div class='modal-dialog scrollable centered-modal'>

Solution

  1. Step 1: Identify correct Bootstrap classes

    Bootstrap uses modal-dialog-scrollable and modal-dialog-centered as separate classes to add scroll and vertical centering.
  2. Step 2: Check syntax for multiple classes

    Multiple classes are added separated by spaces inside the class attribute, so both classes must be included exactly as documented.
  3. Final Answer:

    <div class='modal-dialog modal-dialog-scrollable modal-dialog-centered'> -> Option A
  4. Quick Check:

    Use both classes separated by space [OK]
Hint: Use both classes separated by spaces inside class attribute [OK]
Common Mistakes:
  • Using incorrect class names like 'scrollable' or 'centered'
  • Combining classes into one word
  • Missing one of the required classes
3. Given this modal HTML snippet, what will be the visual effect?
<div class='modal-dialog modal-dialog-scrollable modal-dialog-centered'>
  <div class='modal-content'>
    <div class='modal-body'>
      <p>Very long content here...</p>
    </div>
  </div>
</div>
medium
A. Modal is centered but content does not scroll
B. Modal content scrolls but modal is aligned to top
C. Modal content scrolls inside and modal is vertically centered on screen
D. Modal neither scrolls nor centers vertically

Solution

  1. Step 1: Analyze classes used

    The modal uses both modal-dialog-scrollable and modal-dialog-centered classes.
  2. Step 2: Understand combined effect

    These classes together make the modal vertically centered and allow the modal body content to scroll if it is long.
  3. Final Answer:

    Modal content scrolls inside and modal is vertically centered on screen -> Option C
  4. Quick Check:

    Both scrollable and centered classes active = scroll + center [OK]
Hint: Both classes together scroll content and center modal [OK]
Common Mistakes:
  • Assuming scroll disables centering
  • Thinking content scrolls outside modal
  • Confusing modal-body scrolling with page scrolling
4. You added modal-dialog-scrollable but the modal content is not scrolling. What is the likely mistake?
medium
A. You forgot to add modal-dialog-centered
B. You must add scrollable class instead
C. You need to add overflow-auto to modal-content
D. You placed modal-dialog-scrollable on the wrong element

Solution

  1. Step 1: Check where the class is applied

    The modal-dialog-scrollable class must be on the modal-dialog element, not on modal-content or others.
  2. Step 2: Understand Bootstrap structure

    If placed incorrectly, the scroll behavior won't activate because Bootstrap styles target the dialog container.
  3. Final Answer:

    You placed modal-dialog-scrollable on the wrong element -> Option D
  4. Quick Check:

    modal-dialog-scrollable must be on modal-dialog [OK]
Hint: Apply scrollable class only on modal-dialog div [OK]
Common Mistakes:
  • Adding scrollable to modal-content instead
  • Confusing scrollable with centered class
  • Forgetting to add modal-dialog class
5. You want a modal that is vertically centered, scrollable, and also has a fixed header and footer that stay visible while the body scrolls. Which Bootstrap structure and classes should you use?
hard
A. Use modal-dialog modal-dialog-scrollable modal-dialog-centered with modal-header, modal-body, and modal-footer inside modal-content
B. Use modal-dialog modal-dialog-centered only and add custom CSS for scrolling
C. Use modal-dialog-scrollable on modal-content and add fixed position to header/footer
D. Use modal-dialog modal-dialog-scrollable but omit modal-header and modal-footer

Solution

  1. Step 1: Identify required classes for scroll and center

    Use both modal-dialog-scrollable and modal-dialog-centered on the modal-dialog element.
  2. Step 2: Structure modal content properly

    Inside modal-content, include modal-header and modal-footer which remain fixed, while modal-body scrolls.
  3. Step 3: Confirm Bootstrap default behavior

    Bootstrap automatically keeps header and footer visible when using scrollable modals with this structure.
  4. Final Answer:

    Use modal-dialog modal-dialog-scrollable modal-dialog-centered with modal-header, modal-body, and modal-footer inside modal-content -> Option A
  5. Quick Check:

    Combine scrollable + centered + header/footer structure [OK]
Hint: Use both classes on modal-dialog and include header/footer inside modal-content [OK]
Common Mistakes:
  • Adding scrollable to modal-content instead of modal-dialog
  • Omitting modal-header or modal-footer
  • Trying to fix header/footer with custom CSS unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard