Create an Image Modal using CSS and Javascript

Step 1 : Creating our Body

First lets create our displayed image and our modal.
  1. <img id="sampleImg" src="gardens.jpg" alt="An Example of a Beautiful Garden" width="300" height="200">
  2.  
  3. <!-- Sample Modal -->
  4. <div id="sampleModal" class="modal">
  5. <span class="close" id="closeModal">&times;</span>
  6. <img class="modal-content" id="modalImage">
  7. <div id="desc"></div>
  8. </div>

Step 2 : Add CSS

  1. #sampleImg {
  2. cursor: pointer;
  3. transition: 0.3s;
  4. }
  5.  
  6. #sampleImg:hover {opacity: 0.7;}
  7.  
  8. .modal {
  9. display: none;
  10. position: fixed;
  11. z-index: 1;
  12. padding-top: 100px;
  13. left: 0;
  14. top: 0;
  15. width: 100%;
  16. height: 100%;
  17. overflow: auto;
  18. background-color: rgb(0,0,0);
  19. background-color: rgba(0,0,0,0.9);
  20. }
  21.  
  22. .modal-content {
  23. margin: auto;
  24. display: block;
  25. width: 80%;
  26. max-width: 700px;
  27. }
  28.  
  29. #desc {
  30. margin: auto;
  31. display: block;
  32. width: 80%;
  33. max-width: 700px;
  34. text-align: center;
  35. color: #ccc;
  36. padding: 10px 0;
  37. height: 150px;
  38. }
  39.  
  40. .modal-content, #desc {
  41. -webkit-animation-name: zoom;
  42. -webkit-animation-duration: 0.6s;
  43. animation-name: zoom;
  44. animation-duration: 0.6s;
  45. }
  46.  
  47. @-webkit-keyframes zoom {
  48. from {-webkit-transform:scale(0)}
  49. to {-webkit-transform:scale(1)}
  50. }
  51.  
  52. @keyframes zoom {
  53. from {transform:scale(0)}
  54. to {transform:scale(1)}
  55. }
  56.  
  57. .close {
  58. position: absolute;
  59. top: 15px;
  60. right: 35px;
  61. color: #f1f1f1;
  62. font-size: 40px;
  63. font-weight: bold;
  64. transition: 0.3s;
  65. }
  66.  
  67. .close:hover,
  68. .close:focus {
  69. color: #bbb;
  70. text-decoration: none;
  71. cursor: pointer;
  72. }
  73.  
  74. @media only screen and (max-width: 700px){
  75. .modal-content {
  76. width: 100%;
  77. }
  78. }

Adding the Javascript

Lastly, we add our javascript
  1. var modal = document.getElementById('sampleModal');
  2. var img = document.getElementById('sampleImg');
  3. var modalImg = document.getElementById("modalImage");
  4. var captionText = document.getElementById("desc");
  5. var close = document.getElementById("closeModal");
  6.  
  7. img.onclick = function(){
  8. modal.style.display = "block";
  9. modalImg.src = this.src;
  10. captionText.innerHTML = this.alt;
  11. }
  12.  
  13. close.onclick = function() {
  14. modal.style.display = "none";
  15. }

Full HTML

  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>Create an Image Modal using Javascript</title>
  4. <style type="text/css">
  5. #sampleImg {
  6. cursor: pointer;
  7. transition: 0.3s;
  8. }
  9.  
  10. #sampleImg:hover {opacity: 0.7;}
  11.  
  12. .modal {
  13. display: none;
  14. position: fixed;
  15. z-index: 1;
  16. padding-top: 100px;
  17. left: 0;
  18. top: 0;
  19. width: 100%;
  20. height: 100%;
  21. overflow: auto;
  22. background-color: rgb(0,0,0);
  23. background-color: rgba(0,0,0,0.9);
  24. }
  25.  
  26. .modal-content {
  27. margin: auto;
  28. display: block;
  29. width: 80%;
  30. max-width: 700px;
  31. }
  32.  
  33. #desc {
  34. margin: auto;
  35. display: block;
  36. width: 80%;
  37. max-width: 700px;
  38. text-align: center;
  39. color: #ccc;
  40. padding: 10px 0;
  41. height: 150px;
  42. }
  43.  
  44. .modal-content, #desc {
  45. -webkit-animation-name: zoom;
  46. -webkit-animation-duration: 0.6s;
  47. animation-name: zoom;
  48. animation-duration: 0.6s;
  49. }
  50.  
  51. @-webkit-keyframes zoom {
  52. from {-webkit-transform:scale(0)}
  53. to {-webkit-transform:scale(1)}
  54. }
  55.  
  56. @keyframes zoom {
  57. from {transform:scale(0)}
  58. to {transform:scale(1)}
  59. }
  60.  
  61. .close {
  62. position: absolute;
  63. top: 15px;
  64. right: 35px;
  65. color: #f1f1f1;
  66. font-size: 40px;
  67. font-weight: bold;
  68. transition: 0.3s;
  69. }
  70.  
  71. .close:hover,
  72. .close:focus {
  73. color: #bbb;
  74. text-decoration: none;
  75. cursor: pointer;
  76. }
  77.  
  78. @media only screen and (max-width: 700px){
  79. .modal-content {
  80. width: 100%;
  81. }
  82. }
  83. </style>
  84. </head>
  85.  
  86. <img id="sampleImg" src="gardens.jpg" alt="An Example of a Beautiful Garden" width="300" height="200">
  87.  
  88. <!-- Sample Modal -->
  89. <div id="sampleModal" class="modal">
  90. <span class="close" id="closeModal">&times;</span>
  91. <img class="modal-content" id="modalImage">
  92. <div id="desc"></div>
  93. </div>
  94.  
  95. var modal = document.getElementById('sampleModal');
  96. var img = document.getElementById('sampleImg');
  97. var modalImg = document.getElementById("modalImage");
  98. var captionText = document.getElementById("desc");
  99. var close = document.getElementById("closeModal");
  100.  
  101. img.onclick = function(){
  102. modal.style.display = "block";
  103. modalImg.src = this.src;
  104. captionText.innerHTML = this.alt;
  105. }
  106.  
  107. close.onclick = function() {
  108. modal.style.display = "none";
  109. }
  110. </body>
  111. </html>
That ends this tutorial. Happy Coding :)

Add new comment