css - How to make Modal in the center of screen due to absolute position?

Css - How to make Modal in the center of screen due to absolute position?

To center a modal in the middle of the screen using absolute positioning in CSS, you can set the modal's top, left, right, and bottom properties to 50%, and then use negative margins to shift it back by half of its own width and height. Here's how you can do it:

HTML:

<div class="modal"> <div class="modal-content"> <span class="close">&times;</span> <p>This is the modal content.</p> </div> </div> 

CSS:

.modal { display: none; /* Hide modal by default */ position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.4); } .modal-content { position: absolute; background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; border-radius: 10px; top: 50%; left: 50%; transform: translate(-50%, -50%); } .close { position: absolute; right: 10px; top: 5px; color: #aaa; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } 

JavaScript:

// Get the modal var modal = document.querySelector('.modal'); // Get the close button var closeBtn = document.querySelector('.close'); // When the user clicks on the close button or outside the modal, close it closeBtn.addEventListener('click', function() { modal.style.display = 'none'; }); window.addEventListener('click', function(event) { if (event.target == modal) { modal.style.display = 'none'; } }); 

In this example:

  • The modal is initially hidden (display: none;).
  • The .modal class sets the modal to cover the entire viewport with a semi-transparent background color.
  • The .modal-content class centers the modal using absolute positioning and translates it by half of its own width and height.
  • The .close class styles the close button and sets up event listeners to close the modal when clicked.
  • JavaScript is used to handle the modal's visibility by toggling its display property.

Examples

  1. "CSS center modal with absolute position"

    Description: This query helps users find out how to center a modal with absolute positioning in CSS. It typically involves using a combination of position, top, left, and transform properties.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Centers the modal on the screen z-index: 1000; // Bring the modal above other content } 
  2. "CSS modal position absolute center screen"

    Description: This query guides users on how to position a modal in the center of the screen with absolute positioning. It focuses on techniques for ensuring the modal stays centered regardless of viewport size.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Centers the modal both vertically and horizontally } 
  3. "CSS center modal on screen"

    Description: This query assists users in centering a modal on the screen, providing code examples and explanations for achieving this effect with CSS.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Center the modal z-index: 1000; // Ensure modal is above other content } 
  4. "Center modal with absolute position in CSS"

    Description: This query is for users seeking to center a modal using absolute positioning in CSS. It often involves combining multiple CSS properties to achieve the desired result.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Centers the modal } 
  5. "CSS modal center absolute position"

    Description: This query helps users find ways to center a modal with absolute positioning in CSS. It leads to examples showing how to center the modal both vertically and horizontally.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Center the modal z-index: 1000; // Keep modal above other content } 
  6. "Center modal in CSS with absolute position"

    Description: This query provides guidance on centering a modal in CSS using absolute positioning. It typically includes examples demonstrating how to align the modal properly on the screen.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Centers the modal background-color: white; // Optional background color } 
  7. "CSS absolute modal centered on screen"

    Description: This query assists users in centering a modal on the screen using absolute positioning. It focuses on techniques for consistent centering regardless of viewport size.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Centers the modal } 
  8. "Center modal with absolute and translate"

    Description: This query helps users find ways to center a modal using absolute positioning and the translate property in CSS. It typically leads to examples demonstrating this combination for consistent centering.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Center modal with absolute position z-index: 1000; // Ensure modal is on top } 
  9. "CSS center modal horizontally and vertically"

    Description: This query guides users in centering a modal both horizontally and vertically using CSS. It often involves absolute positioning with appropriate transformation.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Center modal in both axes } 
  10. "Position modal at the center of the screen with CSS"

    Description: This query provides guidance on how to place a modal at the center of the screen using CSS. It typically leads to examples demonstrating various approaches to achieve this with absolute positioning.

    /* CSS */ .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); // Center modal in the middle of the screen z-index: 1000; // Ensure it's above other content } 

More Tags

ruby-on-rails-3 maxlength es6-modules popup binary-tree android-viewmodel authorization react-android avassetexportsession fileoutputstream

More Programming Questions

More Everyday Utility Calculators

More Financial Calculators

More Dog Calculators

More Stoichiometry Calculators