
Collection – Add A Hover Effect To Product Images On Your Collection Pages
You can add a hover effect to your product images on the home page and on collection pages. When a customer moves the cursor over a product image, the image will change to show either an alternate product image, custom text, or a combination of the two:
Add CSS to your stylesheet
To add a hover effect, you will need to add some CSS code to your theme’s stylesheet:
- From your Shopify admin, go to Online Store > Themes.
- Find the theme you want to edit, and then click Actions > Edit code.
- In the Assets directory, click
theme.css.liquid
. If your theme doesn’t have atheme.css.liquid
file, then clickstyles.css.liquid
, or another file with a.css.liquid
file extension. - At the very bottom of the file, paste this code
/* ===============================================
// Reveal module
// =============================================== */
.reveal .hidden { display: block !important; visibility: visible !important;}
.product:hover .reveal img { opacity: 1; }
.reveal { position: relative; }
.reveal .hidden {
position: absolute;
z-index: -1;
top: 0;
width: 100%;
height: 100%;
opacity: 0;
-webkit-transition: opacity 0.3s ease-in-out;
-moz-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
.reveal:hover .hidden {
z-index: 100000;
opacity: 1;
}
.reveal .caption {
position: absolute;
top: 0;
display: table;
width: 100%;
height: 100%;
background-color: white; /* fallback for IE8 */
background-color: rgba(255, 255, 255, 0.7);
font: 13px/1.6 sans-serif;
text-transform: uppercase;
color: #333;
letter-spacing: 1px;
text-align: center;
text-rendering: optimizeLegibility;
}
.reveal .hidden .caption .centered {
display: table-cell;
vertical-align: middle;
}
@media (min-width: 480px) and (max-width: 979px) {
.reveal .caption {
font-size: 11px;
}
}
5.Click Save.
Edit the code for your product images
To edit the code for your product images:
- In the Snippets directory, click
product-grid-item.liquid
.
If your theme doesn’t have aproduct-grid-item.liquid
file, then look for one of the following:- product-card.liquid
- product-card-grid.liquid
- product-loop.liquid
- Find the HTML img tag for your product images by searching for
<img
. The code for the tag varies theme to theme, but always starts with<img
, and ends with either>
or/>
. It might look similar to this:
<img src=”{{ featured.featured_image.src | img_url: ‘450×450’ }}” alt=”{{ featured.featured_image.alt | escape }}”> - On a new line above the
img
tag, paste the following code:
<div class=”reveal”>
On a new line below theimg
tag, paste the following code:
</div>
The result should look like this:
<div class=”reveal”> <img src=”{{ featured.featured_image.src | img_url: ‘450×450’ }}” alt=”{{ featured.featured_image.alt | escape }}”> </div> - On a new line below the
img
tag and above the closing</div>
tag, add the code that changes what is shown on hover. The code that you add will vary depending on whether you want to show an alternate product image, custom text, or a combination of the two. Follow the steps for the customization of your choice.
Show an alternate product image on hover
- On a new line below the
img
tag and above the closing</div>
tag, paste the following code:
<img class=”hidden” src=”{{ product.images.last | img_url: ‘450×450’ }}” alt=”{{ product.images.last.alt | escape }}” />
Your code should look like this:
<div class=”reveal”> <img src=”{{ featured.featured_image.src | img_url: ‘450×450’ }}” alt=”{{ featured.featured_image.alt | escape }}”><!– this is your original image tag –> <img class=”hidden” src=”{{ product.images.last | img_url: ‘450×450’ }}” alt=”{{ product.images.last.alt | escape }}” /> </div> - Click Save.
Show custom text on hover
- On a new line below the
img
tag and above the closing</div>
tag, paste the following code:
<div class=”hidden”> <div class=”caption”> <div class=”centered”> YOUR TEXT </div><!– end of .centered –> </div><!– end of .caption –> </div><!– end of .hidden –>
ReplaceYOUR TEXT
with the text of your choice. You can use HTML and Liquid tags, for example:
<div class=”hidden”> <div class=”caption”> <div class=”centered”> <p>{{ product.title }}</p> <p>{{ product.price | money }}</p> </div><!– end of .centered –> </div><!– end of .caption –> </div><!– end of .hidden –>
The example code above shows the title and the price of a product when you hover over the image.
Your code should look like this:
<div class=”reveal”> <img src=”{{ featured.featured_image.src | img_url: ‘450×450’ }}” alt=”{{ featured.featured_image.alt | escape }}”><!– this is your original image tag –> <div class=”hidden”> <div class=”caption”> <div class=”centered”> YOUR TEXT </div><!– end of .centered –> </div><!– end of .caption –> </div><!– end of .hidden –> </div><!– end of .reveal –>. - Click Save.
Show an alternate product image with custom text on hover
- On a new line below the
img
tag and above the closing</div>
tag, paste the following code:
<div class=”hidden”> <img src=”{{ product.images.last | img_url: ‘450×450’ }}” alt=”{{ product.images.last.alt | escape }}” /> <div class=”caption”> <div class=”centered”> YOUR TEXT </div><!– end of .centered –> </div><!– end of .caption –> </div><!– end of .hidden –>
Your code should look like this:
<div class=”reveal”> <img src=”{{ featured.featured_image.src | img_url: ‘450×450’ }}” alt=”{{ featured.featured_image.alt | escape }}”><!– this is your original image tag –> <div class=”hidden”> <img src=”{{ product.images.last | img_url: ‘450×450’ }}” alt=”{{ product.images.last.alt | escape }}” /> <div class=”caption”> <div class=”centered”> YOUR TEXT </div><!– end of .centered –> </div><!– end of .caption –> </div><!– end of .hidden –> </div><!– end of .reveal –> - Click Save.