How we can create a lightbox using HTML and CSS? Solution: See this HTML CSS Image Lightbox With Toggle Feature, Simple Lightbox with pure CSS.
Earlier I have shared a Pure CSS Lightbox Gallery, but this is just a simple lightbox with toggle click feature. Basically, Lightbox is a JavaScript library that displays images and videos by filling the screen, and dimming out the rest of the web page. But using only HTML and CSS we can also create that kind of effect for expanding image from a thumbnail.
Today you will learn to create Simple Lightbox with toggle click feature. This simple lightbox contains just small image thumbnail, after clicking it will expand to the large or original size, also on second time click it will again small size. That is a kind of toggle, but this very simple method which is based on the link target.
So, Today I am sharing HTML CSS Image Lightbox With Toggle Feature. As you know, Here I have used only HTML and CSS you can call it a pure CSS lightbox example. I think this post will be useful for you, if you are a beginner then you will definitely like this trick for creating the toggle. You also can use it on your website or project.
If you are thinking now how this image box actually is, then see the preview given below.
Preview Of Simple Lightbox
See this video preview to getting an idea of how this design looks like.
Now you can see this visually, you also can see it live by clicking the button given above. If you like this, then get the source code of its.
You May Also Like:
- Ecommerce Product Card Design
- CSS Stylish Checkbox With Tick
- 3D Image Gallery With CSS
- Responsive Flexbox Design
HTML CSS Image Lightbox With Toggle Feature Source Code
Before sharing source code, let’s talk about it. First, I have placed an image two times inside the hyperlink tag using HTML. The first image is for the thumbnail and the second is the full-size image. Also in the HTML file, I have placed some texts and heading. Here I have used a google font for creating beautiful texts (get).
Now using CSS I have placed all the elements like text, image on the right place, as you can see in the preview. In the small image I have placed a class named ‘thumbnail‘ and in the large one, I put nothing. And in the hyperlink tag on the large image, I have placed an ID named ‘img‘. Firstly I gave display: none; to the lightbox.
And I have used the target method to create this program. In the small image I have target the large image using <a href="#img"> tag, and on the large image I have placed the target as blank <a href="#_"> . In CSS file, just put command as on target display will block otherwise none. Left all other things you will understand after getting the codes, I can’t explain all.
For creating this lightbox, you have to create only 2 files for that. One for HTML and one for CSS files, Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>HTML CSS Lightbox | Webdevtrick.com</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" /> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Pure CSS Lightbox</h1> <p>Click the image given below to activate the lightbox</p> <!-- small size --> <a href="#img"> <img src="https://webdevtrick.com/wp-content/uploads/visual-studio-code.jpg" class="thumbnail"> </a> <!-- full size --> <a href="#_" class="lightbox" id="img"> <img src="https://webdevtrick.com/wp-content/uploads/visual-studio-code.jpg"> </a> <p class="website">webdevtrick.com</p> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
html,body { font-family: 'Raleway', sans-serif; padding: 0 2em; font-size: 18px; background: #4d4d4d; color: #fff; text-align:center; } h1 { font-size: 3em; font-weight: 200; margin: 0.5em 0 0.2em 0; } p { margin: 1.5em 0; color: #f3f3f3; } .thumbnail { max-width: 40%; } .website { font-style: bold; font-size: 0.8em; color: #ff3d3d; } .lightbox { display: none; position: fixed; z-index: 999; width: 100%; height: 100%; text-align: center; top: 0; left: 0; background: rgba(0,0,0,0.8); } .lightbox img { max-width: 90%; max-height: 80%; margin-top: 2%; } .lightbox:target { outline: none; display: block; } @media (max-width:765px) { .thumbnail { max-width: 95%; } .lightbox img { margin-top: 50%; } } |
That’s It. Now you have successfully created HTML CSS Image Lightbox With Toggle Feature, Simple Lightbox Feature. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.