Bootstrap modal variants size, position


Modal Variants in Bootstrap (Size, Position)

Bootstrap modals can be customized in terms of size and positioning to suit different design requirements. These variants help ensure that modals fit well into various layouts and use cases.


1. Modal Sizes

Bootstrap offers several predefined modal sizes that you can use to adjust the width of the modal dialog.

a. Small Modal

The small modal is narrower and can be used for less content or more compact displays.

<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. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
  • .modal-sm: Adds a smaller width to the modal dialog.

b. Large Modal

The large modal provides more space for displaying content and is used for larger content or forms.

<div class="modal fade" id="largeModal" tabindex="-1" aria-labelledby="largeModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="largeModalLabel">Large Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This is a large modal. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
  • .modal-lg: Adds a larger width to the modal dialog.

c. Extra Large Modal

The extra-large modal provides even more space for content and is useful for very large forms or detailed information.

<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 is an extra-large modal. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
  • .modal-xl: Adds an extra-large width to the modal dialog.

2. Modal Positioning

Modals in Bootstrap are centered vertically and horizontally by default. However, you can customize their positioning using additional utility classes or custom CSS.

a. Vertically Centered Modals

The default modal positioning centers it vertically within the viewport. If you need to adjust the vertical position, you can use Bootstrap’s built-in classes or custom CSS.

Example:

<div class="modal fade" id="verticallyCenteredModal" tabindex="-1" aria-labelledby="verticallyCenteredModalLabel" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="verticallyCenteredModalLabel">Vertically 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 vertically centered. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
  • .modal-dialog-centered: Vertically centers the modal in the viewport.

b. Full-Screen Modals

For a modal that takes up the full width or height of the viewport, you can use custom classes or inline styles.

Example:

<div class="modal fade" id="fullScreenModal" tabindex="-1" aria-labelledby="fullScreenModalLabel" aria-hidden="true"> <div class="modal-dialog modal-dialog-fullscreen"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="fullScreenModalLabel">Full-Screen Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This modal takes up the full screen. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> <style> .modal-dialog-fullscreen { max-width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; } .modal-content { height: 100%; } </style>
  • .modal-dialog-fullscreen: Custom class to adjust the modal to full screen.

c. Custom Positioning

You can also use custom CSS to position the modal differently if needed.

Example:

<div class="modal fade" id="customPositionModal" tabindex="-1" aria-labelledby="customPositionModalLabel" aria-hidden="true"> <div class="modal-dialog" style="margin-top: 100px;"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="customPositionModalLabel">Custom Position Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This modal has custom positioning. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div>
  • Inline Styles: Directly apply styles to adjust positioning.

Summary of Modal Variants (Size, Position)

  • Modal Sizes: Use .modal-sm, .modal-lg, and .modal-xl to adjust the width of the modal.
  • Modal Positioning: By default, modals are centered. Use .modal-dialog-centered for vertical centering, and custom classes or styles for full-screen or other specific positions.
  • Custom Positioning: Apply custom CSS for additional adjustments as needed.