Hover Animation Button

Hi, can anyone help me? I want to make background slide animation for hover button and i tried to add this css but it didnt work. I want to change it for my add to cart button (which i use secondary button style). The hover color that i want is black

button-secondary.button-secondary-background-slide::before {

content: ‘’;

position: absolute;

top: 0;

left: 0;

bottom: 0;

right: 0;

z-index: -1;

background-color: var(–accent-color);

transition: transform 300ms ease-in-out;

transform: scaleX(0);

transform-origin: left;

}

.button-secondary.button-secondary-background-slide:hover::before,

.button-secondary.button-secondary-background-slide:focus::before {

transform: scaleX(1);

}

.button-secondary.button-secondary-background-slide {

transition: color 300ms ease-in-out;

z-index: 1;

}

.button-secondary.button-secondary-background-slide:hover,

.button-secondary.button-secondary-background-slide:focus {

color: white;

Your Add to cart button uses the class .add-to-cart-button, and Horizon has no --accent-color variable. They don’t exist in Horizon.

After Shots (I cannot show the animation but it works)

Here black slides in from the left on hover, text turns white.

Add it once

  • In your theme editor, on a product page, click Add section at the bottom of the list.
  • Search Custom Liquid and click it.

  • Paste this into the Liquid code box, then Save.
<style>
  .add-to-cart-button { position: relative; overflow: hidden; isolation: isolate; }
  .add-to-cart-button::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    background-color: #000;
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 300ms ease-in-out;
  }
  .add-to-cart-button:hover::before,
  .add-to-cart-button:focus-visible::before { transform: scaleX(1); }
  .add-to-cart-button:hover,
  .add-to-cart-button:focus-visible { color: #fff; --button-color: #fff; }
</style>

Note

  • Fill colour is background-color: #000 - change to any hex.
  • Speed is 300ms - lower is faster.
  • This targets the Add to cart button only.

Hi @veluxe1

Try this code.

.button-secondary.button-secondary-background-slide {
  position: relative;
  overflow: hidden;
  z-index: 1;
  transition: color 300ms ease-in-out;
}

.button-secondary.button-secondary-background-slide::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
  z-index: -1;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 300ms ease-in-out;
}

.button-secondary.button-secondary-background-slide:hover::before,
.button-secondary.button-secondary-background-slide:focus::before {
  transform: scaleX(1);
}

.button-secondary.button-secondary-background-slide:hover,
.button-secondary.button-secondary-background-slide:focus {
  color: #fff;
}

HI @veluxe1

Yes. The CSS is close, but there are a few problems in the snippet:

  • ‘’ are curly quotes, so the content is invalid.

  • var(–accent-color) uses an en dash instead of –accent-color.

  • The ::before pseudo-element needs the button to have position: relative and overflow: hidden.

  • Your selector only works if the button actually has the button-secondary-background-slide class.

If you’re using Shopify’s secondary button for Add to Cart, try this:

/* Secondary Add to Cart button */

.button-secondary {

  position: relative;

  overflow: hidden;

  z-index: 1;

  transition: color 300ms ease-in-out;

}



/* Sliding black background */

.button-secondary::before {

  content: "";

  position: absolute;

  inset: 0;

  background: #000;

  transform: scaleX(0);

  transform-origin: left;

  transition: transform 300ms ease-in-out;

  z-index: -1;

}



/* Slide in on hover */

.button-secondary:hover::before,

.button-secondary:focus-visible::before {

  transform: scaleX(1);

}



/* White text while hovering */

.button-secondary:hover,

.button-secondary:focus-visible {

  color: #fff;

}

If you only want this on Add to Cart

I wouldn’t apply it to every .button-secondary if your theme uses that class elsewhere.

Instead, inspect the Add to Cart button in your browser and find its specific class/ID. For example, if it looks like:

<button class="product-form__submit button button-secondary">

you could target:

.

.product-form__submit.button-secondary {

  position: relative;

  overflow: hidden;

  z-index: 1;

  transition: color 300ms ease-in-out;

}

.product-form__submit.button-secondary::before {

  content: "";

  position: absolute;

  inset: 0;

  background: #000;

  transform: scaleX(0);

  transform-origin: left;

  transition: transform 300ms ease-in-out;

  z-index: -1;

}

.product-form__submit.button-secondary:hover::before {

  transform: scaleX(1);

}



.product-form__submit.button-secondary:hover {

  color: #fff;

}

Where to put it

In Shopify:

Online Store → Themes → … → Edit code → Assets → base.css

Paste it at the bottom of the CSS file and save.

If your theme has a Custom CSS field, you can put it there instead.

One other thing: if the button has an icon (cart icon, arrow, etc.), the z-index: -1 approach can sometimes put the animation behind the button’s own stacking context. If that happens, use z-index: 0 for ::before and give the button’s text/icon wrapper position: relative; z-index: 1.

If you tell me which Shopify theme you’re using (Dawn, Horizon, Nordic, etc.), I can give you the exact selector for its Add to Cart button rather than applying the animation to every secondary button.

Hey @veluxe1

hope you’re doing well!

Your CSS is close, but the selector/class structure and quote characters are likely causing the issue. Also use #000 instead of the undefined --accent-color.
Try:

.button-secondary {
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.button-secondary::before {
  content: "";
  position: absolute;
  inset: 0;
  background: #000;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 300ms ease-in-out;
  z-index: -1;
}

.button-secondary:hover::before {
  transform: scaleX(1);
}

.button-secondary:hover {
  color: #fff !important;
}

If your add to cart button uses different class, inspect the button and replace -secondary with its actual class.

Thankyou so much! Also thanks to @devcoders @Custom-Cursor @ai-theme-code-editor for answering my questions. Really help me

Hi @veluxe1

Thank you for your response. It’s good to know that it’s worked for you. Kindly feel free to get back to me if you need any further assistance. If helpful, please like all posts. :white_check_mark:

Best regards,
Devcoder :laptop: