INTRODUCTION
FLEXIBLE IMAGES
Welcome to a tutorial on how to create responsive full-screen images. So you need to show an image in its full glory? Be it in front of a gallery, or as a Background image? Well, there are actually quite a few ways to do it, and this guide will walk you through the exact steps. Read on to find out!
I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
CONFESSION
AN HONEST DISCLOSURE
Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!
NAVIGATION
TABLE OF CONTENTS
Step 1 | Step 2 | Step 3 |
Extra | Closing |
STEP 1
THE BASICS
Before we officially go into the full-sized image, here are the basics… Sort of an introduction for those of you who do not know how a “normal” responsive image works.
THE SCRIPT
Responsive Fullscreen Image Example
THE EXPLANATION
Yep, all we need to create a responsive image is to actually give it The browser will then automatically scale the image to fit onto the screen. But as from the screenshot above, we will run into a “not properly fitted” problem on the portrait orientation. This is why I will recommend using the Following up with a “not so nice” fullscreen responsive image on the portrait orientation, here is a “fix” using background images. In this version, we have simply removed the HTML That’s it. With just a few more lines of CSS, we can now fully control how the background will look like: This final step is actually optional, and only for those of you who are looking to show the full-size image when the user clicks on a thumbnail… Be warned though, there are a few lines of Javascript involved. That’s all for the code, and here is the download link as promised. Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project. Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding! The post 3 Steps Simple Responsive Full Screen Images appeared first on Code Boxx.width: 100%
and height: auto
. The z-index
number.background-image
property for full-screen images, as it offers more controls.STEP 2
BACKGROUND IMAGESFROM HTML TO CSS
THE RESULT
tag, and used the
background-image
property on the container instead. While it does fill up the entire screen now, it is still not so nice with repeating images, and we need to do a little more work on it.CONTROLLING THE BACKGROUND IMAGE
THE EXPLANATION
background-repeat
is enabled by default, and it is good if you have a repeating pattern. If not, it is best to set this one to no-repeat
.background-size
determines how the image will be processed to fit the space. cover
will simply stretch the image to fit, but you can play around with the other settings to see which one works for you.background-position
determines where the image will be displayed on the container. I usually set this to center
or left top
. Again, play around and see which one works for you.STEP 3
SCARY JAVASCRIPTTHE HTML
THE CSS
/* [THUMBNAILS] */
#tgallery img {
width: 33%;
height: auto;
float: left;
}
#tgallery img:hover {
cursor: pointer;
}
/* [LIGHTBOX] */
#lightbox {
/* Optional - fixed on screen */
position: fixed;
top: 0;
left: 0;
z-index: 999;
/* Full sized */
width: 100%;
height: 100vh;
/* Background image */
background-repeat: no-repeat;
background-size: cover;
background-position: center;
/* Hidden by default */
visibility: hidden;
opacity: 0;
/* Animation magic */
transition: all 0.3s;
}
#lightbox.show {
visibility: visible;
opacity: 1;
}
/* [DOES NOT MATTER] */
html, body {
margin: 0;
padding: 0;
}
THE JAVASCRIPT
var lb = {
wrap : null, // Holds the reference to the HTML lightbox div
init : function () {
// lb.init() : initialze on window load
// Get HTML wrapper
lb.wrap = document.getElementById("lightbox");
// Click to close the lightbox div
lb.wrap.addEventListener("click", lb.hide);
// Attach onclick event to all image thumbnails
var thumbs = document.querySelectorAll("#tgallery img");
for (var t of thumbs) {
t.addEventListener("click", lb.show);
}
},
show : function () {
// lb.show() : show the selected image
// Attach selected image as background image on the lightbox, show it
lb.wrap.style.backgroundImage = "url(" + this.getAttribute("src") + ")";
lb.wrap.classList.add("show");
},
hide : function () {
// lb.hide() : hide the full screen lightbox
lb.wrap.classList.remove("show");
}
};
/* [INIT] */
window.addEventListener("load", lb.init);
THE EXPLANATION
var lb
contains all the key players of the full-screen image display.lb.init
will fire up and attach onclick
event listeners to all the thumbnails.lb.show
will read the src
of the selected image, set it as the background on the full-screen container, and display it.lb.hide
will hide the container itself.DEMO
EXTRA
DOWNLOAD & MORESOURCE CODE DOWNLOAD
CLOSING
WHAT’S NEXT?