skip to Main Content

I’m working with a shopify theme, and I’m trying to display an Add To Cart button on top of my product image when a user hovers over it. That said, the ‘hover’ aspect is already active on the product image using the following CSS. In short, when I currently hover over a product image, it simply lowers the opacity to 0.7. That said, I can’t seem to figure out how to write HTML for .grid-product__image-link if I want a button or an image to appear on hover instead? Help is appreciated!

Current HTML

<div class="grid__item grid-product {{ grid_item_width }}{% if sold_out %} is-sold-out{% endif %}">
  <div class="grid-product__wrapper">
    <div class="grid-product__image-wrapper">
      <a class="grid-product__image-link{% unless featured_image.src == blank %} grid-product__image-link--loading{% endunless %}" href="{{ product.url | within: collection }}" data-image-link>
        {% if featured_image.src == blank %}
   
        <img class="grid-product__image" src="{{ featured_image.src | img_url: '1024x' }}" alt="{{ featured_image.alt | escape }}">
   
        
          {% else %}
          {% include 'image-style' with image: featured_image, small_style: true, width: width, height: height, wrapper_id: img_wrapper_id, img_id_class: img_id_class %}
          <div id="{{ img_wrapper_id }}" class="product--wrapper">
            <div style="padding-top:{{ 1 | divided_by: featured_image.aspect_ratio | times: 100 }}%;">
              <img class="product--image lazyload {{ img_id_class }}"
                   data-src="{{ img_url }}"
                   data-widths="[180, 370, 590, 740, 900, 1080, 1296, 1512, 1728, 2048]"
                   data-aspectratio="{{ featured_image.aspect_ratio }}"
                   data-sizes="auto"
                   alt="{{ featured_image.alt | escape }}"
                   data-image>
            </div>
          </div>
          <noscript>
  
                
           <img class="grid-product__image" src="{{ featured_image.src | img_url: '1024x' }}" alt="{{ featured_image.alt | escape }}">
  
             
        </noscript>
        {% endif %}
      </a>
      {% if sold_out %}
        <div class="grid-product__sold-out">
          <p>{{ 'products.product.sold_out_html' | t }}</p>
        </div>
      {% elsif on_sale %}
        <div class="grid-product__on-sale">
          {% capture saved_amount %}{{ product.compare_at_price | minus: product.price | money_without_trailing_zeros }}{% endcapture %}
          <p>{{ 'products.general.save_html' | t: saved_amount: saved_amount }}</p>
        </div>
      {% endif %}
    </div>

CSS

.grid-product__wrapper {
  text-align: center;
  margin-bottom: $gutter;

  .grid-collage & {
    margin-bottom: 0;
  }
}

.grid-product__image-wrapper {
  position: relative;
  width: 100%;
  display: table;
  table-layout: fixed;
}

.grid-product__image-link {
  position: relative;
  display: block;
  width: 100%;
  background-color: $colorProductBackground;
  @include transition(opacity 0.4s ease-out);

  .grid-collage & {
    padding: 0 20px;
  }

  &:hover, 
  &:focus {
    opacity: 0.7;
    @include transition(opacity 0.15s ease-in);
  }

}

2

Answers


  1. I would use javascript to make another html <img> or <button> element popup with a higher z-index property when your mouse gets on top of that image, but I’m not sure if that works for you.

    Login or Signup to reply.
  2. If I correctly understand you, maybe this will help you 🙂

    Html:

       <a class="grid-product__image-link>
           <div class = "product-overlay"></div>
       </a>
    

    Scss:

    .grid-product__image-link {
      position: relative;
      display: block;
      width: 100%;
      background-color: $colorProductBackground;
      @include transition(opacity 0.4s ease-out);
    
      .grid-collage & {
        padding: 0 20px;
      }
    
    
      .product-overlay{
        display:none;
      }
    
      &:hover, 
      &:focus {
        opacity: 0.7;
        @include transition(opacity 0.15s ease-in);
        
        .product-overlay{
            display:block;
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search