Need help centering your modal vertically with a margin on top and bottom?
One way to achieve this is by using Flexbox for the modal's parent element. This allows for easy vertical and horizontal centering.
div.backdrop {
background-color: rgba(0, 0, 0, 0.4);
height: 100%;
left: 0;
overflow: auto;
position: fixed;
top: 0;
width: 100%;
z-index: 2020;
display: flex;
justify-content: center;
align-items: center;
}
To add a margin at the top and bottom, set a max-height
for the modal:
div.modal {
background-color: white;
border: 1px solid red;
margin: 10% auto;
max-width: 800px;
width: 80%;
max-height: calc(100% - 40px);
}
An issue you might face is text overflowing at the bottom, prompting your next query:
Concerned about scrolling content within the modal instead of the modal itself?
If your content exceeds the modal height, you can enable scrolling within the modal itself by adding an overflow property:
div.modal {
background-color: white;
border: 1px solid red;
margin: 10% auto;
max-width: 800px;
width: 80%;
max-height: calc(100% - 40px);
overflow-x: auto;
}
Here is the revised CSS snippet for achieving the desired effect:
div.backdrop {
background-color: rgba(0, 0, 0, 0.4);
height: 100%;
left: 0;
overflow: auto;
position: fixed;
top: 0;
width: 100%;
z-index: 2020;
display: flex;
justify-content: center;
align-items: center;
}
div.modal {
background-color: white;
border: 1px solid red;
margin: 10% auto;
max-width: 800px;
width: 80%;
max-height: calc(100% - 40px);
overflow: auto;
}
<h1>Page main content</h1>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<img src="https://via.placeholder.com/800x120.png" />
<div class="backdrop">
<div class="modal">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
</div>
</div>
Edit:
Reducing unnecessary CSS attributes. Streamlined modal example:
.backdrop {
background-color: rgba(0, 0, 0, 0.4);
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
background-color: white;
border: 1px solid red;
margin: 10% auto;
max-width: 800px;
width: 80%;
max-height: calc(100% - 40px);
overflow: auto;
}
<h1>Page main content</h1;
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<img src="https://via.placeholder.com/800x120.png" />
<div class="backdrop">
<div class="modal">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
</div>
</div>
A clean solution involves removing unnecessary overflow from the body
, along with open/close functionality:
function openmodal() {
document.querySelector("body").classList.toggle("overflow-hidden");
document.querySelector(".backdrop").style.display = "flex";
}
function closemodal() {
if (event.target == document.querySelector(".backdrop")) {
document.querySelector("body").classList.toggle("overflow-hidden");
document.querySelector(".backdrop").style.display = "none";
}
}
body {
overflow: auto;
}
.backdrop {
background-color: rgba(0, 0, 0, 0.4);
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: none;
justify-content: center;
align-items: center;
}
.modal {
background-color: white;
border: 1px solid red;
margin: 10% auto;
max-width: 800px;
width: 80%;
max-height: calc(100% - 40px);
overflow: auto;
}
.overflow-hidden {
overflow: hidden;
}
<h1>Page main content</h1;
<button onclick="openmodal()">
Clik me
</button>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>;
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>;
<img src="https://via.placeholder.com/800x120.png" />
<div class="backdrop" onclick="closemodal()">
<div class="modal">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</p>
</div>
</div>