What's the reason behind CSS transitions starting with a black background?

When I apply a transition to the heart, it inexplicably turns black at the start of the animation before displaying the intended gradient effect.

Below is the code snippet:

.heart {
     font-size: 2.5rem;
     padding-right: 2rem;
     }

.heart:hover::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f004";
  background-image: linear-gradient(#9356DC, #FF79DA);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  transition: 5s ease-in-out;
  cursor: pointer;
  }
<link
            rel="stylesheet"
            href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
        />
<span class="fa-regular fa-heart heart"></span>

I'm trying to figure out why there is a black background at the beginning of the transition. I've experimented with different solutions, like adding a background-color property, but so far, nothing has resolved the issue.

Thank you for any help you can provide.

Update: I have adjusted the transition duration from .9s to 5s to highlight the problem more clearly. Adding color to the element does not eliminate the issue; the heart should maintain a black outline when the cursor is not hovering over it.

Answer №1

It seems the solution you are seeking is akin to an Instagram like button. The trick here involves incorporating a pseudo-element ::after and tweaking its styling to match the before element on hover. By adjusting the opacity of the before element to 0 and the after element to 1 upon hover, with a smooth transition effect, the two seamlessly transition between states. Remember to assign a relative position to the .heart element so that the after element can be positioned directly on top of the before element.

.heart {
  position: relative;
  font-size: 2.5rem;
  font-weight: 400;
}
.heart::before {
  transition: 0.3s ease-in-out;
  cursor: pointer;
}
.heart::after {
  position:absolute;
  left: 0;
  transition: 0.3s ease-in-out;
  content: "\f004";
  background-image: linear-gradient(#9356DC, #FF79DA)!important;
  font-weight: 600;
  color: transparent;
  background-clip: text;
  -webkit-background-clip: text;
  opacity: 0;
  cursor: pointer;
  
}

.heart:hover::before {
  opacity: 0;
}
.heart:hover::after {
  opacity: 1;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />

<span class="fa-regular fa-heart heart"></span>

Answer №2

.heart {
  font-size: 2.5rem;
  padding-right: 2rem;
  /* Implement these lines of code */
  background: linear-gradient(#9356DC, #FF79DA);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.heart:hover::before {
  font-family: "Font Awesome 6 Free";
  font-weight: 900;
  content: "\f004";
  background-image: linear-gradient(#9356DC, #FF79DA);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  transition: 0.9s ease-in-out;
  cursor: pointer;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />

<span class="fa-regular fa-heart heart"></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble getting Tinymce to appear on the screen

I am currently attempting to install TinyMCE for use with my text editor in order to provide the user with a text box similar to the one on Stack Overflow. However, I am encountering an issue where it is not displaying as expected. In the header of my ind ...

Creating an accordion using HTML and jQuery allows for dynamic and interactive content organization

I am trying to incorporate an "ACCORDION" feature into my application using HTML and jQuery. Within this implementation, I have nested accordions. Below is a snippet of my code: <div class="row-fluid"> <div id="filtersContainer" class="span1 ...

How can you use jQuery to display an image when hovering over text?

Looking for a tutorial or script that displays an image when hovering over HTML text with the mouse? ...

Title: How to Build a Dynamic Logo Carousel with React and CSS without External Dependencies

Currently, I am in the process of integrating a logo carousel into my React web application using CSS. My goal is to create a slider that loops infinitely, with the last logo seamlessly transitioning to the first logo and continuing this cycle indefinitely ...

use ajax to dynamically append a dropdown menu

Currently working on creating a form that includes a dropdown menu populated with elements from a database. The challenge I'm facing is ensuring that once an element is selected from the dropdown, another one appears dynamically. My goal is to use AJA ...

What might be causing the navigation to malfunction in Firefox?

Could you please help me identify the issue with the navigation on this website? It seems to work correctly on webkit, but not on firefox! Intended vs actual ...

How can I use the store command to retrieve the href value of a link using a css selector in Selenium?

Is there a way to store a URL from a link using CSS instead of xpath? store //tr[td[contains(.,'6 Day')]][1]/td[8]/a@href my_var open $my_var If so, how can I achieve this goal with css? I managed to use the following locator: store css= ...

When clicking on the text areas, the Twitter-Bootstrap drop-down login automatically closes

Greetings! I am currently working on my first website design using JQuery. My project includes a dropdown login box created with Twitter-Bootstrap. Unfortunately, I'm facing some challenges with the JQuery script that controls the behavior of the logi ...

The flex grow animation fails to activate when the Bootstrap 4 style sheet is added

Trying to implement transitions with flex grow has been a challenge. It works smoothly without any issues, but as soon as I add the bootstrap 4 style sheet, the transitions stop working. I haven't even started using the bootstrap 4 classes yet, it&apo ...

Storing information in the DOM: Choosing between Element value and data attribute

When it comes to storing values in a DOM element, we have options like using the data attribute. For example, $("#abc").data("item", 1) can be used to store values and then retrieve them with $("#abc").data("item"). However, I recently discovered that th ...

Adjusting diagram size based on screen width using Bootstrap and jQuery

I recently created a diagram that adjusts its size according to the screen width. However, when I implemented this code on my page, I encountered an issue where the final circle or glyph would drop to the next line instead of staying on the same line as in ...

"Can anyone provide guidance on how to use Twig to read from a JSON file

I currently have a basic PHP file set up as follows: <?php // Loading our autoloader require_once __DIR__.'/vendor/autoload.php'; // Setting the location of our Twig templates $loader = new Twig_Loader_Filesystem(__DIR__.'/views& ...

Move the menu position in Bootstrap 4 navbar

<div class="fixed-top"> <div class="collapse" id="navbarToggleExternalContent"> <div class="bg-dark p-4"> <h5 class="text-white h4">Hidden content</h5> <span class="text-muted">Toggleable via the ...

What is the best way to grab the initial section of a website's text while using a WKWebView?

When working with a WKWebView, retrieving the webpage's title is simple using webView.title. However, obtaining the first paragraph of the webpage's content poses a challenge with no straightforward solution. Any tips or suggestions on how to ac ...

Issues with the navigation and logo design in bootstrap mode

1: How can I adjust the size of the logo and name in my Bootstrap navigation? 2: My navigation menu is not showing up correctly. Can anyone help me troubleshoot? https://i.sstatic.net/0pXya.jpg Navbar: <nav class="navbar navbar-expand-lg py-4 f ...

The resizing of the window does not occur when a scrollbar is automatically added in IE11

I encountered an issue with my Angular project where the view resizes properly in Chrome and Firefox, but not in IE 11. When the scrollbar appears, some components get covered by the scrollbar. Here is the CSS for the menu: #menu-principal .navbar-lower ...

Providing a user with a special discount tailored to their email address?

<script type="text/javascript"> function updatePrice() { var price = document.getElementById("product").value; var size_price = document.getElementById("size").value; var a=parseInt(price);//parsed type price var b=parseInt(size_price);//pa ...

Trigger a click event to alter the child div elements

I have a <div> that expands when clicked and shrinks back when clicked again. Inside this div are images that should only appear once the div expands. My issue is ensuring that all images except the first one are hidden until the div expands. https: ...

if jade is being inconsistent

I am currently using expressjs in my app and have the following setup: app.get('/profile',index.profile); app.get('/',index.home); Within layout.jade, I have the following code: ... if typeof(username)!=='undefined' ...

Prevent your HTML tables from overflowing

I am attempting to create an HTML table with three columns, where I want the first and third columns to have a width of "auto" based on their content. The middle column should then take up the remaining space. While this setup works well when the content ...