css overflowing content can disrupt the layout of the screen

I am currently working on creating a modal that will be displayed in the center of the screen with the same width as the main page. However, I am facing an issue where the modal is not perfectly centered. I suspect that this is due to the overflow style adding a scroll on the right side. How can I resolve this issue?

<body>
    <main>
      <h1>Welcome to the Main Page</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam rerum molestiae tempora nesciunt illo, id placeat unde similique eius mollitia. Accusantium a cum libero impedit quidem alias molestiae, odio, saepe aspernatur provident temporibus, vel dolorum suscipit.
      </p>
      <button>Click to show modal</button>
    </main>
    <div class="modal-container">
      <div class="modal-wrapper">
        <h2>The Modal Page</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque tenetur voluptates quidem, nemo, eius fugit sit reprehenderit, temporibus libero, eum recusandae? Fugit nam blanditiis soluta a.
        </p>
      </div>
    </div>

    <script>
      document.querySelector("button").addEventListener("click", ()=> document.querySelector(".modal-container").classList.add("show"));
    </script>
    <script src="script.js"></script>
  </body>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
main {
  width: min(calc(100% - 3.125rem), 45.625rem);
  height: 200vh;
  position: relative;
  left: 50%;
  transform: translate(-50%, -5%);
  background-color: aqua;
}
.modal-container {
  position: fixed;
  inset: 0;
  padding: 20vh 0;
  background: rgba(0, 0, 0, 0.6);
  opacity: 0;
  overflow: scroll;
  pointer-events: none;
  transition: 200ms;
}
.modal-container.show {
  opacity: 1;
}
.modal-wrapper {
  width: min(calc(100% - 3.125rem), 45.625rem);
  height: 150vh;
  margin: auto;
  padding: 1.75rem 1.25rem;
  pointer-events: none;
  transition: 200ms;
  border-radius: 0.5rem;
  background-color: aquamarine;
}

Answer №1

In this updated instance, we have a scrollable and centered example. The JavaScript 'close' script is missing. Some CSS lines were disabled to fix the problem, and can be re-enabled if required for your main content. Further CSS adjustments may be needed for non-scrollable text in the popup.

    document.querySelector("button").addEventListener("click", ()=> document.querySelector(".modal-container").classList.add("show"));
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
main {
/*--  xwidth: min(calc(100% - 0.1rem), 45.625rem); --*/
  height: 80vh;
  position: relative;
/*--  xleft: 25%; --*/
/*--  xtransform: translate(-0%, -0%); --*/
  background-color: aqua;
}
.modal-container {
  position: fixed;
/*--  xinset: 0; --*/
/*--  xpadding: 20vh 0; --*/
  background: rgba(0, 0, 0, 0.6);
  opacity: 0;
/*--  xoverflow: scroll; --*/
  pointer-events: none;
  transition: 200ms;
  /*--add all below--*/
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
}
.modal-container.show {
  opacity: 1;
}
.modal-wrapper {
/*--  width: min(calc(100% - 3.125rem), 45.625rem); --*/
/*--  height: 150vh; --*/
  margin: auto;
  padding: 1.75rem 1.25rem;
  pointer-events: auto;
  transition: 200ms;
  border-radius: 0.5rem;
  background-color: aquamarine;
  /*--add all below--*/
  height: 50%;
  width: 50%;
  overflow: auto; 
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0; 
}
<main>
      <h1>THIS IS MAIN PAGE</h1>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae
        mollitia, repellendus, ratione modi saepe iste ea id qui quia autem nisi
        distinctio maxime! Repellat, consequatur. Possimus delectus perferendis,
        illo vero perspiciatis tenetur debitis provident itaque rem laborum sint
        esse temporibus officia reprehenderit nesciunt laudantium, aspernatur
        consectetur quia sapiente exercitationem odit. Molestiae necessitatibus,
        dolorem illum expedita minus obcaecati dicta distinctio minima?
      </p>
      <p>&nbsp;</p>
      <button>show modal centered</button> 
      <p>&nbsp;</p>
      <button id="myBtn">W3 Open Modal</button>
    </main>
    
    <div class="modal-container">
      <div class="modal-wrapper">
        <h2>THIS IS MODAL PAGE</h2>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Excepturi
          nobis nulla iure consequuntur provident? Laborum, suscipit. Aut
          similique dolor unde tempora laborum voluptates at cupiditate,
          deserunt voluptate illum cum explicabo magnam quibusdam quam facere
          consectetur aspernatur adipisci. Magnam, inventore harum.
        </p>
      </div>
    </div>

For a detailed guide and examples of a perfectly centered modal, check this out:
codepen

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing Online Presence with Video Gallery Website Development

I'm in the process of creating a website and need help finalizing my video page. I envision a layout similar to this example: https://i.stack.imgur.com/hctui.gif The main feature should be a large video placeholder at the top, followed by several thu ...

Animation doesn't apply to child components, but is successful when applied to the parent component

I am working on animating the width of a child component's div. Here is my simple code: export const OuterComponent = () => { const sidebarCtx = useContext(SidebarContext); const style = "w-[100px] h-[60px] transit ...

Enhancing the pagination look and feel in Laravel 5.1

Is it possible to customize the paginator layout in Laravel 5.1? I am looking to include first and last links along with the default previous and next links. I would like to change the previous and next links to show icons instead of text, while still ma ...

Tips for showcasing an image prior to uploading within a personalized design input file

I am facing an issue with displaying multiple images inside custom style input file labels before uploading them to the database. Currently, the script I am using only displays one image at a time and in random positions. My goal is to have each image ap ...

Make sure that the webpage does not display any content until the stylesheet has been fully loaded by

I am seeking to utilize ng-href for loading different Themes. One issue I am encountering is that the unstyled content appears before the stylesheet is applied. I have created a Plunker where I made changes to Line 8 in the last 3 Versions for comparison ...

Utilize varying sizes within a single column using Bootstrap

I am struggling to create a heading that spans the entire width of my listview within Bootstrap. Despite being in the same column, the heading does not extend as far as the listview. I desire for the title to stretch all the way across the list, leading m ...

Show the bottom left quarter of the image in an HTML display

Currently, I am still new to HTML coding and experimenting with displaying specific sections of an image. The current code I have is this: <img id="theImg" style="width:100%;" src="https://'myimage.jpg'" /> I'm struggling to figure o ...

Position an image on the top left corner of a div's border

I have a situation where I'm trying to overlay a small square transparent image on the top left border of a div that has a 1px border. The image is 48px by 48px in size and I want it to give the illusion that it's going underneath the top and lef ...

Enhance container and typography for layouts converted to 1920px width to improve overall design

Greetings to all! I need some assistance with converting a design file that is 1920px wide into an HTML layout. Can someone provide tips on optimizing containers and typography for responsiveness? Specifically, I would like the font-size and line-height ...

Submitting a form via email without the need for PHP

Currently, I am focusing on a project that is small and emphasizes presentation more than security. The main objective is to create a form where users can input data, then click submit for all the information gathered to be sent to a specified 'mailt ...

Is it possible for me to overlap a text over hidden text, and if the hidden text becomes visible through JavaScript, my text will shift to the right of the now visible hidden text?

I'm currently working on a website for a company that requires users to complete all the information before proceeding. To achieve this, I created a form with the following code: <form action="Owners Infoback.php" onsubmit="return validateFo ...

"Initial loading issue with bootstrap carousel causes slides not to slide smoothly

I am currently working on implementing a slider carousel using Bootstrap. Although the 'active' image loads successfully, the controls and slide functionality are not working as expected. Initially, I believed this exercise would be straightforwa ...

Unexpected symbols appearing in image tag after AJAX response

I'm currently grappling with an issue related to an AJAX request I am working on (I am quite new to the world of AJAX). I have set up an API and my goal is to fetch a PNG image using an Authorization header that requires a token supplied by me (which ...

Automatically adjust padding in nested lists with ReactJS and MaterialUI v1

How can I automatically add padding to nested lists that may vary in depth due to recursion? Currently, my output looks like this: https://i.stack.imgur.com/6anY9.png: However, I would like it to look like this instead: https://i.stack.imgur.com/dgSPB. ...

Ways to troubleshoot the MudTabPanel during scrolling

I am trying to create a fixed text at the top of a scrollable MudTab. As I scroll down, I want the text to remain visible. Here is an example of what I have done: <MudTabs Position="Position.Top" PanelClass="pt-2" Style="hei ...

Dynamic horizontal div elements laid out in a repeating pattern using CSS Grid

I'm fairly new to CSS/Html/JS and I'd like to create a row of Boxes (loaded from a json file) displayed horizontally. Something similar to this: https://i.sstatic.net/ZgxMR.png Here's the code snippet I've been using: <style> ...

How do I make the YouTube Grid Gallery player show up in a pop-up window?

I have been experimenting with the following code: <head> <script type="text/javascript" src="http://swfobject.googlecode.com/svn/trunk/swfobject/swfobject.js"></script> <script type="text/javascript"> function loadVideo(playerUrl, ...

I prefer boxes to be aligned next to each other without any gaps, and I won't be

In my quest to showcase 3 boxes seamlessly lined up in a row, I am faced with the challenge of eliminating spaces between them. My goal is to accomplish this feat without resorting to the font-size property. html,body { height:100%; width ...

What is the best way to tally the frequency of words in a collection of URLs using JavaScript?

I have a JSON object in WordPress that contains a list of URLs. I would like to determine the frequency of occurrence of the second part of each URL. Currently, the code snippet below is able to extract the remaining part of the URL after the prefix https ...

Bootstrap 5 - Create a full-screen modal perfectly aligned with its parent element

BootStrap 5 Incorporating the following layout: <div class="row flex-nowrap"> <div class="left"> <!-- leftbar --> </div> <div class="main overflow-auto position-relative"> <!-- ...