Like our free content? Then you'll love our exclusive Club!
Divi Academy Membership gives you exclusive access to over 110 resources, including child themes, layouts, cheatsheets, tutorials, live training and member perks. And new content is added EVERY Monday!
Back in 2016 I wrote this post on How to Add Links to the Divi Gallery Module and Show Captions on Hover and it is one of my top 5 most popular posts. But its quite outdated now with all the extra Divi options available and not that easy to edit for beginnners, plus I had a load of questions about how to still allow the images to open in a lightbox.
So, as there is always more than one way to skin a cat, rather than update that post I decided to create a new one with the aim of keeping as much in the Divi module options as possible and making the additional code simple and flexible.
I am going to show you how to display the title and caption of a gallery image when the user hovers over the image, still allowing the image to open in the lightbox. As a bonus, I will also show you how to change the thumbnail image dimensions so you can have them square like my demo, or any other aspect ratio you choose, without having to resize your actual images (they will open at their original dimensions in the lightbox).
So let’s get cooking!
Ingredients
The latest version of the Divi Theme from Elegant Themes.
You only really need a child theme for this if you decide to change the gallery image thumbnail dimensions as we will be adding some code to functions.php, but I always recommend using a child theme regardless. It’s just safer
Cooking time
This should only take around 15 – 20 minutes.
Preparation
For the prep we need to set up our gallery. So add a new section and row to your page and then add the Gallery module.
In the Content tab, choose the number of images you want to display and make sure Show Title & Caption is set to Yes. It’s up to you whether you want to show pagination or not.
Next, click on Update Gallery and add the images you want to use to your gallery in the media library. You need to make sure each image has a title and caption, and you can also select to have the images display in a random order if you wish. When you’re done, click Update Gallery.
Back in your module, click on the Design tab and make sure the Layout is set to Grid. The Thumbnail Orientation is up to you, but if you are going to be using square images like my demo, the orientation is irrelevant.
Then use the slider to set the opacity for both the Zoom Icon Colour and Hover Overlay Colour to zero. We want to use the overlay functionality for our gallery so our images still open in a lightbox, but by setting the opacity to zero, we won’t actually see it and can therefore style our text hover effect any way we choose with CSS.
You can then scroll down and choose your font, colour, font size etc. settings for the title and caption text.
Finally, click on the Advanced tab and give the module a CSS Class of ds-hover-gallery then Save & Exit.
Ok, that’s our prep done. Time for the method.
Method
Now we need to add in our code. We will start with our JavaScript.
Add the following code in Divi > Theme Options > Integration > Add code to the < head > of your blog.
What we are doing here is wrapping the title and caption elements in a div element called ds-gallery-text to act as a container, so we can position and style them as one rather than two separate elements. This makes controlling their position a whole lot easier for you. We have also wrapped this in an Ajax function so it works if you are using pagination on your gallery module (which now uses Ajax to load more images without reloading the page).
<script> (function ($) { $(document).ready(function () { $(document).bind('ready ajaxComplete', function () { $(".ds-hover-gallery .et_pb_gallery_item").each(function () { $(this).find(".et_pb_gallery_title, .et_pb_gallery_caption").wrapAll('<div class="ds-gallery-text"></div>'); }); }); }); })(jQuery); </script>
Next, and this is optional, add the following to functions.php in your child theme.
Here we are redefining the size of the images Divi uses for the gallery thumbnails. Using the same value for both the height and the width will obviously result in a square thumbnail, but you can change the 400 values to whatever you want, just be sure not to make them too large or it will increase the page load time. Have a play with the values, start low and if your thumbnails are looking blurry, just take the value up a bit until you are happy.
function ds_gallery_thumbnail_height($height) { return '400'; } add_filter( 'et_pb_gallery_image_height', 'ds_gallery_thumbnail_height' ); function ds_gallery_thumbnail_width($width) { return '400'; } add_filter( 'et_pb_gallery_image_width', 'ds_gallery_thumbnail_width' );
Now for the CSS.
Firstly, we relatively position the gallery items so that our hover effects stay bound to their corresponding images.
.ds-hover-gallery .et_pb_gallery_item { position: relative; }
Then we remove the margin from the title, so that when we position it there isn’t any extra space we don’t want. We also give the Divi default overlay a z-index of 1, this is so it sits on top of our title and caption, which is what allows the image to still open in a lightbox, but as we set the opacity of the overlay to zero in the module settings, we won’t see it.
.ds-hover-gallery .et_pb_gallery_title { margin: 0 !important; } .ds-hover-gallery .et_overlay { z-index: 1; }
And finally the styling for our new overlay containing our title and caption.
First we absolutely position the new ds-gallery-text div we created with our JS snippet and give it top, left, right and bottom values of zero, this will make our container completely cover our image.
Then we add a little padding, I have chosen 20px but you can change this value.
Next we set the container to display: flex;, this allows us to vertically position the text with justify-content. The value of center will have your text sit in the middle of the image, but you can change this to flex-start if you want it at the top and flex-end if you want it at the bottom.
We add a background colour using RGBA so we can have some opacity and the image will show through, here I have set the colour to white with 80% opacity.
We then set the opacity for the actual container to zero, this is so it doesn’t show up until we hover.
The last property gives us the transition between hiding and displaying our text, this will fade the overlay in and out over half a second. You can increase or decrease this value to make that transition slower or faster.
The final declaration simply sets the opacity of our new text overlay to 1, which means it will display when the gallery item is hovered over.
.ds-hover-gallery .ds-gallery-text { position: absolute; top: 0; left: 0; right: 0; bottom: 0; padding: 20px; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; background: rgba(255, 255, 255, .8); opacity: 0; -webkit-transition: all .5s ease-in-out; transition: all .5s ease-in-out; } .ds-hover-gallery .et_pb_gallery_item:hover .ds-gallery-text { opacity: 1; }
If you want to have the text display by default and hide on hover, simply switch the opacity values in this last section of CSS so the first one is set to 1 and the last one is set to 0.
So that is all the CSS you need, obviously you can add more if you want additonal styling but this should serve as a good foundation to get you started.
Copy the all the CSS from the toggle below complete with comments and paste into your child theme stylesheet or Divi > Theme Options > Custom CSS.
Complete CSS
/*-----------------------------------------------------------------------*/ /*-----Show the Gallery Image Title and Caption on Hover by Divi Soup----*/ /*-----------------------------------------------------------------------*/ /*Position the gallery items*/ .ds-hover-gallery .et_pb_gallery_item { position: relative; } /*Remove the default top margin from the title*/ .ds-hover-gallery .et_pb_gallery_title { margin: 0 !important; } /*Set the overlay z-index*/ .ds-hover-gallery .et_overlay { z-index: 1; } /*Position and style the content text container*/ .ds-hover-gallery .ds-gallery-text { position: absolute; top: 0; left: 0; right: 0; bottom: 0; padding: 20px; display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; background: rgba(255, 255, 255, .8); opacity: 0; -webkit-transition: all .5s ease-in-out; transition: all .5s ease-in-out; } /*Show the content text container on hover*/ .ds-hover-gallery .et_pb_gallery_item:hover .ds-gallery-text { opacity: 1; } /*-----------------------------------------------------------------------*/ /*---End Show the Gallery Image Title and Caption on Hover by Divi Soup--*/ /*-----------------------------------------------------------------------*/
And that’s it. It’s pretty simple to create and who knows, maybe one day soon it will be available as an option in the actual module settings
If you found this helpful please leave a comment and subscribe to my newsletter for all my latest Divi related content.
Michelle X
Hi Michelle,
thanks a ton for this! I’ve tried this and the previous version (which I love, with the white box) on WP5 and divi 3.18.1 and it doesn’t work. Where should I add the full css block? At page level, or within the gallery advanced. If so, in which field? There is main item, gallery hover…
Thanks for advise!
The demo of this is using 3.18 and you can see it is working fine. The CSS can be added in your child theme stylesheet or Divi > Theme Options > Custom CSS
Hey Michelle,
the tutorial works very well. I only had to remove “.ds-hover-gallery” everywhere, probably Divi changed something?
(couldn’t find it in the page source code, so I just removed it
A little tweak: I’m trying not to center the caption, but to put it into the left down corner of the thumbnail.
How to do it?
Thanks for any suggestion!
Bye
Sven
Hey Michelle,
how can I center the Text?
Thank you in advanced.
The text in recipe is already centered. If something different is happening on your site, make sure you have all of the correct code included. If you do, please share a link and we can take a look.
Thanks for the instructions. I’ve never done a lick of CSS before, which is probably why there was one thing I couldn’t get to work, even after following everything above… though I was able to have my hover text show up as described above, I’ve lost the ability to make the gallery itself a click-able gallery (I hover over with mouse, text shows up magically, but the individual gallery doesn’t launch). Any suggestions?
The gallery will only launch on click, not hover. Nothing in this CSS would prevent that from happening. Make sure you followed all of the steps. If you would like to share a link we can look.
Thanks a lot ! It’s been days that i’m trying to fix R22 and haven’t see that you have made this new one ! It’s work perfectly now ! Thanks again for all your tutorials !
Thank you very much for the code and tutorial, Michelle.
I’ve copied and followed the steps. But when I am on hover the image is white and when I change the background-color on hover only the Title and the Caption changed the opacity but not the image. You can check it here: http://miloky.com/галерия-2/. Any advice will be helpful. Thank you.
This looks OK to me, but if you want to change the color or opacity on the image, just adjust this value in the CSS:
.ds-hover-gallery .ds-gallery-text {
background: rgba(255,255,255,0.78);
}
The first three sets of digits are the color (255, 255, 255) is white. The last three digits are the opacity – decreasing that number will make the image more visible, increasing it will make it more white.
Thank you, Shay.
Thanks for sharing some great tutorials. If I’m using the gallery module with the slider setting instead of the grid. How can I show the caption as well?
Daryl
.
You can’t I am afraid it isn’t an option. You could potentially add it in with Javascript but thats a whole other thing
Hey Daryl, Not sure about captions but you can add titles with JS, I just been through this on one of my sites. Add the following in a code module at the bottom of the page containing the gallery: (function addImageTitles() { //grab galery slides let slideSet = document.getElementsByClassName(‘et_pb_gallery_image’); //loop the slides for (slide of slideSet) { // get the a child elements let link = slide.firstElementChild; //get the title attribute let linkTitle = link.getAttribute(‘title’); // create a span element with the title let titleSpan = document.createElement(‘span’); titleSpan.textContent = linkTitle; titleSpan.classList.add(‘slide-title’); //append the span as the last child of the… Read more »
Hello Michelle,
I followed the recipe step by step, but it doesn’t work. I also deleted other CSS that could overwrite it, but still nothing.
Link to the page where I used it ist http://www.creatronic.ch/event-it-technik/
Thanks in advance
Recipe works (http://galleryhover.divisoup.com/), but I don’t see a gallery on the site. Can you tell me where it is exactly so I can take a look?
Love this. Thanks. Even a relative beginner like me can set it up!
However, what I’d really like is to be able to make it a clickable link to a page. I see a comment below that refers to Recipe 22, but you say that doesn’t work properly anymore. Excuse my ignorance, is there a section from it I should be using?
Can I get instructions for a beginner Thanks!
This is for a gallery, which links won’t work with the lightbox effect that goes along with that module. However, you can use individual images and we have some recipes for that: https://divisoup.com/four-more-linkable-image-hover-effects/ and https://divisoup.com/four-linkable-image-hover-effects/
Also beginner, but found the way to avoid using the lightbox popup and use a link to a page instead. Just reading the code comments, at some point refers to the z-index, which allows to keep working the lightbox. So my solution was to deactivate ( adding comment symbols before and after: /* the content */), or just delete the following from the css code:
.ds-hover-gallery .et_overlay {
z-index: 1;
}
Kind regards,
Dihue
Thanks for a great tutorial Michelle. It worked perfectly, instructions were terrific. Now I’m trying to add a little tweak but I’m not sure if it’s possible. I would like the image to appear black and white initially. Then on hover the image changes to colour with an overlay as with your original. Is this possible? Could you give me hint how to achieve this ( I’m guessing it’s a Webkit or something)
You mean this? https://elegantmarketplace.com/get-a-color-on-hover-look-with-the-divi-theme-for-wordpress/
I did it step by step and it removed the hover icon etc but did not change my gutters, or put my titles/captions on my images :/
You may have some existing CSS overriding it. If you can share a link to the page you used this on we can look.
Hi Im new to Divi and would love to apply this to my portfolio but as a beginner Im finding it hard to follow – is there a video I can follow please?
Unfortunately, no. You’ll first need to have a child theme installed with Divi. You can create a free one here: https://elegantmarketplace.com/child-theme-maker/
Once you do that, just follow the recipe step by step. Don’t look at it as a whole. Just get through one step, and move on. It should take about 20 minutes.
Hi! Can you please also describe how to add links to the gallery (Like you did in the R22 post?)
You can follow that recipe and just not include the code for hiding the title so it shows.
Hello. Great tutorial!
However… what happen to the “linkable gallery solution? I was hoping this blog also addressed a solution for the issues in that tutorial.
Thanks.
That is Recipe 22 and is located here: https://divisoup.com/how-to-add-links-to-the-divi-gallery-module-and-show-captions-on-hover/
Thanks again, Michelle, this is a really fine looking gallery now! What if I wanted to show the caption text below the image like some explanation and only show the title on hover like we have it right now?
You would need to remove the caption class from the JS that is wrapping it in a div, so change this: (function ($) { $(document).ready(function () { $(document).bind(‘ready ajaxComplete’, function () { $(“.ds-hover-gallery .et_pb_gallery_item”).each(function () { $(this).find(“.et_pb_gallery_title, .et_pb_gallery_caption”).wrapAll(‘ ‘); }); }); }); })(jQuery); TO this: (function ($) { $(document).ready(function () { $(document).bind(‘ready ajaxComplete’, function () { $(“.ds-hover-gallery .et_pb_gallery_item”).each(function () { $(this).find(“.et_pb_gallery_title”).wrapAll(‘ ‘); }); }); }); })(jQuery); If fact you don’t even need the JS if you want the caption below, so if you are ok with editing CSS, just remove all the JS and and apply and styling on the… Read more »
Thank you for the code, Michelle. I had tried out the same thing, and it works. Now on hover the overlay covers both the image and the title-text underneath it. And I don’t know how to confine that overlay to just the image. Maybe you could me out there once more, please and thank you!
BTW: learning-by-doing things this way is really cool, because one remembers stuff that you were sweating over much longer
Hello and thanks for the fine tutorial-update. One small question: suppose I wanted a media query so that tablets would show 2 rows in portrait-mode instead of three?
Is this also possible with the gallery in Slider mode?
No Evonne, the slider does not show the title and caption
Thank you for this tutorial it really helped me a lot.
That great to hear Gerald, thanks
Please make a tutorial for doing this on the blog grid module!
Travis if you subscribe to my newsletter you get 25 free Divi resources and quite a few of them are blog grid layouts with this hover effect. But I will add it to the recipe list anyway
Awesome Michelle! That’s a neat solution. Thank you so much for sharing your knowledge, its very inspiring.
Very kind of you Bjarne and glad it is helpful
Thank you so much, this is most elegant and I love that I can do it all myself!
Most welcome Hurri