I had previously inquired about a similar issue, but I've since made some style changes and now I'm unsure of how to position the close button in the top-right corner of the image, along with the previous and next buttons on either side of the image with specific spacing.
This is just above the footer of my page:
<div class="modal" id="modal">
<div id="modal-content">
<span class="gallery-button" id="close">✖</span>
<span class="gallery-button" id="previous">◄</span>
<span class="gallery-button" id="next">►</span>
<img id="modal-image">
</div>
</div>
How I display the modal:
function showModal(directory, response, i) {
modal.style.display = "block";
modalImg.src = document.getElementById(path(directory, response[i])).src;
/* previousButton.style.display = "block"; */
/* nextButton.style.display = "block"; */
}
And here is my CSS styling:
/* Background when the image is displayed */
#modal {
display: none; /* Hidden by default */
/* Positioning */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
/* Sizing */
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(119, 119, 119); /* Fallback color */
background-color: rgba(119, 119, 119, 0.7); /* Black with opacity */
}
/* Image wrapper */
#modal-content {
/* Sizing */
width: 100%;
height: 100%;
}
/* Image */
#modal-image {
/* Sizing */
max-width: 80%;
height: calc(100vh * 0.6);
/* Style */
border: 10px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
/* Horizontal center image */
margin: auto;
/* Zoom (image enlarges) on open */
animation-name: zoom;
animation-duration: 0.6s;
/* Vertical center image */
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* Gallery Buttons */
.gallery-button {
/* Style */
color: #ffffff;
transition: 0.3s;
background-color: #000000;
border: 2px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
/* Make the button round */
width: 20px;
height: 20px;
line-height: 20px;
border-radius: 50%;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
}
/* Previous Button */
#previous {
font-size: 12px;
/* Position */
position: absolute;
/* Horizontally */
left: 30px;
/* Vertically */
top: 50%;
transform: translateY(-50%);
}
/* Next Button */
#next {
font-size: 12px;
/* Position */
position: absolute;
/* Horizontally */
right: 30px;
/* Vertically */
top: 50%;
transform: translateY(-50%);
}
/* Close Button */
#close {
font-size: 15px;
/* Position */
position: absolute;
top: 25px;
right: 30px;
}
/* Change cursor when hovering over button */
.gallery-button:hover,
.gallery-button:focus {
text-decoration: none;
cursor: pointer;
}
/* Display image at full width on smaller screens */
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
Currently, the close button is positioned in the top right corner of the entire page, and the previous and next buttons are already vertically centered (which is good), but not yet aligned near the image. How can I attach these buttons to the image so that they move with it? Adjusting the image size was challenging, and I am uncertain of an alternative approach to accommodate both the sized image and buttons within a div
.