Viewport units are based on the dimensions of the viewport (width and height of the visible browser page, excluding toolbars, search bars).
vh – viewport height
vw – viewport width
1vh – 1% of viewport’s height.
So in your modal you can do something like,
.modal-container {
...
height: 75vh;
....
}
It will make sure that the height of the modal is always 75% of the screen’s height.
Also you wanna handle the overflowing scenario. So that the content inside the modal will always be visible to the user.
Try this,
.modal-container {
...
height: 75vh;
overflow-y: auto
....
}
You can also use “vw” (viewport width) to the modal container to control the width in the same way.
Solution 2 :
You can use the flex property to align content in the center of the model so it will automatically adjust the browser height. Please check the below example
So Currently, I am creating a modal that is centered in the middle of the page by position absolute. The problem is when I adjust my broswer height and only the height, it will push the modal up and touch the top of the screen and even go pass it. I just want to know how to stop that from helping.
This is what is doing when I shrink the browswer’s height and only the height.
Comments
Comment posted by reacttesting
Just curious, why height: 75vh but not 100vh?
Comment posted by DJ Hemath
It’s just a random value. Using 100vh would touch the screen edges. With some reduced values, we can have a nice gap between edges of the screen and the modal. 🙂
Comment posted by media.giphy.com/media/FcI4m8YkBUZCHWudHD/giphy.gif
Can you create a sandbox for this. So that we can test it live and we can take a look at it.
Comment posted by reacttesting
I tried this method before and doesn’t give the solution I need. The method you provided does set the modal content center. However, when I adjust my browser height, it will shrink the modal-content. I just want the modal-content to be centered but does shrink instead it will just show a scroll bar for user.
Comment posted by Ashok K
@reacttesting I think you are flex property placement is wrong you can add a parent of the content div also add the height: 100%
Comment posted by media.giphy.com/media/FcI4m8YkBUZCHWudHD/giphy.gif