I've been struggling to properly insert images into a <div>
element on my website. I've experimented with various techniques, such as setting max-width
and max-height
to 100%
, changing the display property of the parent element to flex
, and trying different values for object-fit
. Am I overlooking something important, or is it just not feasible to achieve this?
openModal();
function openModal() {
modal.style.display = "flex";
var t = setTimeout(transitionOpenModal, 30);
}
function transitionOpenModal() {
modal.style.opacity = "1";
modalContainer.style.transform = "scale(100%)";
modalContainer.style.webkitTransform = "scale(1)";
}
function closeModal() {
modalContainer.style.transform = "scale(0%)";
modalContainer.style.webkitTransform = "scale(0)";
modal.style.opacity = "0";
var t = setTimeout(closeModalAfterTransition, 250);
}
function closeModalAfterTransition() {
modal.style.display = "none";
}
/* MODAL DIALOG */
.modal {
display: none;
opacity: 0;
position: fixed;
z-index: 99;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
transition: opacity 0.25s;
justify-content: center;
align-items: center;
}
.modal-header {
position: sticky;
padding-bottom: 16px;
}
.modal-container {
background-color: white;
border-radius: 4px;
margin-left: auto;
margin-right: auto;
padding: 16px;
max-width: 80vw;
max-height: 80vh;
filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.25));
transform: scale(0%);
-webkit-transform: scale(0);
transition: transform 0.25s;
}
.modal-content {
max-width: 100%;
max-height: 100%;
}
img.modal-content-inner {
max-width: 100%;
max-height: auto;
object-fit: contain;
}
.modal-header>h2 {
display: inline;
}
.modal-close {
float: right;
font-size: 24px;
font-weight: bold;
}
.modal-close:hover,
.modal-close:focus {
text-decoration: none;
cursor: pointer;
}
<div id="modal" class="modal">
<div class="modal-container" id="modal-container">
<div class="modal-header">
<h2 id="modal-title">modal</h2>
<span class="modal-close" id="modal-close">×</span>
</div>
<div class="modal-content" id="modal-content">
<img src="https://via.placeholder.com/1920x1080.png">
<!-- this is a test case, please try with multiple image sizes -->
</div>
</div>
</div>