Bird
Raised Fist0
Bootsrapmarkup~5 mins

Modal sizes and positioning in Bootsrap

Choose your learning style10 modes available
Introduction

Modals help show important messages or content on top of the page. Changing their size and position makes them fit better for different needs.

You want a small popup for a quick message or confirmation.
You need a large modal to show a form or detailed information.
You want the modal to appear centered or aligned differently on the screen.
You want to make sure the modal looks good on phones and big screens.
You want to control where the modal appears for better user experience.
Syntax
Bootsrap
<div class="modal-dialog modal-sm">
  <div class="modal-content">
    <!-- Modal content here -->
  </div>
</div>

Use modal-sm, modal-lg, or modal-xl classes on modal-dialog to change size.

By default, modals are centered horizontally. You can add modal-dialog-centered to center vertically.

Examples
This creates a small modal, good for short messages.
Bootsrap
<div class="modal-dialog modal-sm">
  <div class="modal-content">
    <!-- Small modal content -->
  </div>
</div>
This modal is large and vertically centered on the screen.
Bootsrap
<div class="modal-dialog modal-lg modal-dialog-centered">
  <div class="modal-content">
    <!-- Large centered modal content -->
  </div>
</div>
This modal is extra large, useful for showing lots of content.
Bootsrap
<div class="modal-dialog modal-xl">
  <div class="modal-content">
    <!-- Extra large modal content -->
  </div>
</div>
Sample Program

This example shows three buttons that open modals of different sizes and positions. The small modal is compact, the large modal is centered vertically, and the extra large modal is very wide. All modals have accessible labels and close buttons.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Bootstrap Modal Sizes and Positioning</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
  <div class="container py-4">
    <h1>Modal Sizes and Positioning Demo</h1>
    <button type="button" class="btn btn-primary me-2" data-bs-toggle="modal" data-bs-target="#smallModal">
      Small Modal
    </button>
    <button type="button" class="btn btn-success me-2" data-bs-toggle="modal" data-bs-target="#largeCenteredModal">
      Large Centered Modal
    </button>
    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#extraLargeModal">
      Extra Large Modal
    </button>
  </div>

  <!-- Small Modal -->
  <div class="modal fade" id="smallModal" tabindex="-1" aria-labelledby="smallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="smallModalLabel">Small Modal</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          This is a small modal for quick messages.
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

  <!-- Large Centered Modal -->
  <div class="modal fade" id="largeCenteredModal" tabindex="-1" aria-labelledby="largeCenteredModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="largeCenteredModalLabel">Large Centered Modal</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          This modal is large and vertically centered on the screen.
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

  <!-- Extra Large Modal -->
  <div class="modal fade" id="extraLargeModal" tabindex="-1" aria-labelledby="extraLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="extraLargeModalLabel">Extra Large Modal</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          This modal is extra large, perfect for showing a lot of content.
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
OutputSuccess
Important Notes

Always add aria-labelledby and aria-hidden for accessibility.

Use modal-dialog-centered to center modals vertically.

Test modals on different screen sizes to ensure they look good everywhere.

Summary

Use modal-sm, modal-lg, and modal-xl classes to control modal size.

Add modal-dialog-centered to center modals vertically on the screen.

Always include accessible labels and test responsiveness for best user experience.

Practice

(1/5)
1. Which Bootstrap class would you add to a modal dialog to make it appear smaller than the default size?
easy
A. modal-centered
B. modal-lg
C. modal-xl
D. modal-sm

Solution

  1. Step 1: Understand modal size classes

    Bootstrap provides specific classes to control modal sizes: modal-sm for small, modal-lg for large, and modal-xl for extra large modals.
  2. Step 2: Identify the class for smaller modal

    The class modal-sm is used to make the modal smaller than the default size.
  3. Final Answer:

    <code>modal-sm</code> -> Option D
  4. Quick Check:

    Small modal = modal-sm [OK]
Hint: Small modal uses modal-sm class [OK]
Common Mistakes:
  • Using modal-lg or modal-xl for small size
  • Confusing modal-centered with size classes
  • Not adding any size class and expecting smaller modal
2. Which of the following is the correct way to center a Bootstrap modal vertically on the screen?
easy
A. Add modal-dialog-centered to the modal-dialog div
B. Add centered-modal to the modal div
C. Add modal-center to the modal-content div
D. Add modal-vertical to the modal-dialog div

Solution

  1. Step 1: Identify the correct class for vertical centering

    Bootstrap uses the class modal-dialog-centered on the modal-dialog element to vertically center the modal.
  2. Step 2: Verify the element to apply the class

    The class must be added to the modal-dialog div, not modal or modal-content.
  3. Final Answer:

    Add modal-dialog-centered to the modal-dialog div -> Option A
  4. Quick Check:

    Vertical center = modal-dialog-centered on modal-dialog [OK]
Hint: Use modal-dialog-centered on modal-dialog div [OK]
Common Mistakes:
  • Adding centering class to wrong modal element
  • Using non-existent classes like modal-center
  • Confusing modal-dialog-centered with modal size classes
3. What will be the visual size of the modal dialog in this code snippet?
<div class="modal-dialog modal-xl">
  <div class="modal-content">
    <!-- Modal body -->
  </div>
</div>
medium
A. Small modal dialog
B. Default modal dialog size
C. Extra large modal dialog
D. Centered modal dialog

Solution

  1. Step 1: Identify the size class used

    The class modal-xl is applied to the modal-dialog div, which sets the modal to extra large size.
  2. Step 2: Understand the effect of modal-xl

    This class makes the modal wider than default and large sizes, so the modal will appear extra large on the screen.
  3. Final Answer:

    Extra large modal dialog -> Option C
  4. Quick Check:

    modal-xl = extra large modal [OK]
Hint: modal-xl class means extra large modal [OK]
Common Mistakes:
  • Confusing modal-xl with modal-lg or modal-sm
  • Thinking modal-xl centers the modal
  • Ignoring the class and assuming default size
4. You want to make a modal vertically centered and large sized. Which of these code snippets correctly applies both features?
A) <div class="modal-dialog modal-lg modal-dialog-centered">
B) <div class="modal-dialog modal-centered modal-lg">
C) <div class="modal-dialog modal-lg-centered">
D) <div class="modal-dialog modal-lg center-dialog">
medium
A. A
B. B
C. C
D. D

Solution

  1. Step 1: Check correct Bootstrap classes for size and centering

    The correct classes are modal-lg for large size and modal-dialog-centered for vertical centering.
  2. Step 2: Verify the class names and order

    A uses both classes correctly on the modal-dialog div. Other options use incorrect or non-existent classes.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    modal-lg + modal-dialog-centered = A [OK]
Hint: Combine modal-lg and modal-dialog-centered on modal-dialog [OK]
Common Mistakes:
  • Using wrong class names like modal-centered or modal-lg-centered
  • Placing classes on wrong elements
  • Missing one of the required classes
5. You want to create a responsive modal that is small on mobile, large on tablets, and extra large on desktops, and also vertically centered. Which Bootstrap classes should you apply to the modal-dialog to achieve this?
hard
A. modal-dialog modal-sm modal-lg modal-xl modal-dialog-centered
B. modal-dialog modal-dialog-centered plus custom CSS media queries for sizes
C. modal-dialog modal-dialog-centered modal-sm modal-lg modal-xl
D. modal-dialog modal-dialog-centered modal-sm modal-lg modal-xl with responsive utility classes for each breakpoint

Solution

  1. Step 1: Understand Bootstrap modal size classes

    Bootstrap modal size classes like modal-sm, modal-lg, and modal-xl are not responsive by themselves and cannot be combined to change size at different screen widths.
  2. Step 2: Identify how to make modal responsive

    To have different modal sizes on different devices, you must use modal-dialog-centered for vertical centering and write custom CSS media queries to adjust modal width responsively.
  3. Final Answer:

    modal-dialog modal-dialog-centered plus custom CSS media queries for sizes -> Option B
  4. Quick Check:

    Responsive modal sizes require custom CSS + modal-dialog-centered [OK]
Hint: Use modal-dialog-centered plus CSS media queries for responsiveness [OK]
Common Mistakes:
  • Trying to combine multiple size classes on modal-dialog
  • Assuming Bootstrap size classes are responsive by default
  • Not adding vertical centering class
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