Skip to content

Instantly share code, notes, and snippets.

@nalinvyas
Last active December 29, 2022 06:43
Show Gist options
  • Save nalinvyas/36386cf80fe52232a8c2af7fda4fb509 to your computer and use it in GitHub Desktop.
Save nalinvyas/36386cf80fe52232a8c2af7fda4fb509 to your computer and use it in GitHub Desktop.
Responsive Gallery with modal popup on click with HTML5, CSS3, and simple javascript
// Open the Modal
function openModal(n) {
document.getElementById("myModal").style.display = "block";
showSlides((slideIndex = n));
}
// Close the Modal
function closeModal() {
document.getElementById("myModal").style.display = "none";
}
var slideIndex;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides((slideIndex += n));
}
// Thumbnail image controls
function currentSlide(n) {
showSlides((slideIndex = n));
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
captionText.innerHTML = dots[slideIndex - 1].alt;
}
/*heading style*/
.center {
text-align: center;
color: #777;
font-family: sans;
font-weight: 400;
}
/* Modal styles */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.9);
/* Black w/ opacity */
}
/* Modal content */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content,
#caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {
-webkit-transform: scale(0);
}
to {
-webkit-transform: scale(1);
}
}
@keyframes zoom {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}
/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
.row {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: minmax(100px, auto);
grid-gap: 20px;
display: grid;
grid-template-columns: repeat(
2,
1fr
); /* 2 columns, each with a width of 1 fraction */
padding: 0 15%;
flex-wrap: wrap;
max-width: 1000px; /* Set the maximum width of the row */
margin: 0 auto; /* Center the row */
}
.column {
display: flex;
align-items: center;
height: 300px;
/* adjust this value as needed */
width: 500px;
margin: 10px;
box-shadow: 10px 5px 5px #bbb;
flex: 1; /* Allow the column to grow or shrink to fill the available space */
min-width: 200px; /* Set the minimum width of the column */
}
/* Make the images take up the full width of the container on small screens */
@media only screen and (max-width: 1000px) {
.column {
width: 100%;
height: auto;
}
.row {
grid-template-columns: repeat(1, 1fr);
}
}
/* Previous button */
.prev {
color: white;
position: fixed;
top: 50%;
left: 0;
transform: translateY(-50%);
}
/* Next button */
.next {
color: white;
position: fixed;
top: 50%;
right: 0;
transform: translateY(-50%);
}
/* Image number on modal */
.numbertext {
color: #f1f1f1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment