HomejavascriptResponsive Image Gallery Responsive Image Gallery Blogger Leb January 26, 2021 0 HTML Click to enlarge Click to enlarge Click to enlarge Click to enlarge Click to enlarge Click to enlarge Click to enlarge Click to enlarge Click to enlarge CSS * { padding: 0; margin: 0; box-sizing: border-box; font-family: sans-serif; } h1 { font-size: 1.5em; line-height: 8em; } #modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; height: 100%; width: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.7); } .modal-container { margin: 10% auto; width: 67%; height: 67%; display: flex; } .modal-container img { max-height: 100%; max-width: 100%; margin: 0 auto; animation-name: modalopen; animation-duration: 1s; } .container { max-width: 950px; margin: auto; border: white solid 3px; background-color: white; } .imgs img { width: 100%; height: 200px; box-shadow: 5px 5px 2px rgba(40,62,80, 0.20); } .imgs { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 15px; cursor: pointer; margin-top: 20px; } @media only screen and (max-width: 700px) { .imgs { grid-template-columns: repeat(2, 1fr); } } @media only screen and (max-width: 500px) { .imgs { grid-template-columns: repeat(1, 1fr); } } .photo-overlay { opacity: 0; color: #fff; position: absolute; top: 0; right: 0; bottom: 0; left: 0; padding-left: 20px; padding-right: 20px; display: flex; flex-direction: column; justify-content: center; align-items: center; background: rgba(0,0,0, .45); transition: opacity 0.1s linear; } .photo-overlay:hover { opacity: 1; } .img-container { position: relative; } @keyframes modalopen { from{opacity: 0} to {opacity: 1} } JavaScript const current = document.querySelector('#current'); let enlargeMessages = document.querySelectorAll('.photo-overlay h1'); const modal = document.getElementById('modal'); const overlay = document.querySelectorAll('.photo-overlay'); const modalContainer = document.querySelector('.modal-container'); for (let i =0; i < enlargeMessages.length; i++){ enlargeMessages[i].addEventListener('click', imgClick); } window.addEventListener('click', clickOutside); function imgClick(e){ modal.style.display = 'block'; current.src = e.target.parentNode.previousElementSibling.src; } function clickOutside(e){ if(e.target == modal || e.target == modalContainer){ modal.style.display = 'none';} } Tags CSS HTML javascript Facebook Twitter Whatsapp Share to other apps Responsive Image Gallery CSS Newer Older