The CSS hover effect for text backgrounds is acting unexpectedly when the mouse moves away

I’ve added a hover effect to an h1 tag (check out the code and demo below), but it’s acting strangely when I move the mouse away, almost flickering before returning to its original state.

Any suggestions on how to smoothly transition back to its original color, just like it fades in on hover?

Thank you for your help!

Here is the codepen link

h1 {
        transition: 0.5s;
      }

      h1:hover {
        background: linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB);
        background-size: 400% 400%;
        color:transparent;
        -webkit-background-clip: text;
        background-clip: text;
        -webkit-animation: Gradient 15s ease infinite;
        -moz-animation: Gradient 15s ease infinite;
        animation: Gradient 15s ease infinite;
      }

      @-webkit-keyframes Gradient {
          0%, 100% {
              background-position: 0 50%
          }
          50% {
              background-position: 100% 50%
          }
      }

      @-moz-keyframes Gradient {
          0%, 100% {
              background-position: 0 50%
          }
          50% {
              background-position: 100% 50%
          }
      }

      @keyframes Gradient {
          0%, 100% {
              background-position: 0 50%
          }
          50% {
              background-position: 100% 50%
          }
      }
<h1>The Title</h1>

Answer №1

The issue lies in the attempt to blend an animation with a transition, which does not function as expected. Introducing a transition does not guarantee a smoother animation. Essentially, transitions cannot be added to animations.

Considering that you have set the duration of the animation to 15s on hover, it seems unnecessary to include the infinite loop in this scenario since no one would likely hover for more than 15s. Therefore, transforming this into a transition will produce the desired smoothness.

I opted to incorporate black color into the gradient for our initial state. Through utilizing a transition, we can achieve half of the original animation effect and at the conclusion, you will experience a 7s duration, which is suitable for a hover effect:

h1 {
  transition: 7s;
  background: 
    linear-gradient(-45deg, #EE7752, #E73C7E, #23A6D5, #23D5AB,#000);
  background-size: 400% 400%;
  color:transparent;
  -webkit-background-clip: text;
  background-clip: text;
  background-position: 0 0;
}

h1:hover {
  background-position: 100% 50%;
  
}
<h1>The Title</h1>

Answer №2

Before implementing transitions, it is crucial to establish the initial state of the element properties that will be altered.

h1 {
  background: black;
  -webkit-background-clip: text;
  background-clip: text;
}

I came across an intriguing example with a similar effect to yours. https://codepen.io/anthony-liddle/pen/uFoxA

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

javascript how to retrieve the custom attribute value of an HTML style

I am dealing with an HTML element that has some inline styles as well as a custom CSS property. I am able to access every style attribute except for my custom style property. Here is the code I am working with: <img id="myimg" class="ImgClass" style=" ...

How can the caption be aligned to the right of the image within a Bootstrap thumbnail in a precise manner, while also ensuring the button is positioned at the bottom of the caption?

Greetings! I am in the process of developing a website using bootstrap v3.3.2. My first query revolves around utilizing the grid system to correctly position the caption on the right side of the thumbnail, as illustrated below: <div class="container"& ...

Guide on aligning a popup next to the button that activated it using CSS and JavaScript

I am currently generating a dynamic quantity of divs, each containing a button that triggers a popup when clicked. I am attempting to position the popup below the specific button that activated it, but it remains static and does not move accordingly. <d ...

Perform a check on the state of the slideToggle using an if-else function and provide a report on the slideToggle state

http://jsfiddle.net/vmKVS/ I need some help understanding the functionality of slideToggle using a small example. After toggling for the first time, the options element changes color to green. However, I expected the menu element to turn red when I togg ...

Bootstrap's ability to display panels of equal height across multiple rows is a game

Just started using Bootstrap 4 after being familiar with Foundation for years. In Foundation, equalizer was a tool to ensure divs had the same height, which would be applied to all divs within a container. I understand that Bootstrap uses Flex, but I&apos ...

Scrollbars behaving differently across browsers

Struggling to find the right words to explain this issue. Take a look at this fiddle for code examples. The problem arose when using a jQuery plugin[1] to customize select elements. I need an absolutely positioned div with a minimum width. Browsers disp ...

Mobile responsiveness issue with menu not functioning as expected

I'm completely new to the world of responsive design with CSS. I recently created a responsive menu that works perfectly when I resize the browser window on my computer. However, when I try to access the site on my phone, the menu is just zoomed out a ...

What is the best way to format text within the _e() function in WordPress?

As I work on developing a basic plugin, I encountered the following line of code: echo "<p style=\"color:red;\">Please enter a valid email address!</p>"; I aim to make this plugin easy to localize and internationa ...

CSS for Positioning Elements Side by Side Using Absolute Positioning

I'm having trouble figuring out how to position balloons like the ones shown in this image: <div class="red_frame"> <img src="https://i.sstatic.net/54SMF.png" class="r_over"/> <img src="https://i.sstatic.net/54SMF.png" ...

CSS: Card elevates when keyboard is activated on a mobile device

[view image 1[view image 2] Image 1: Normal View, Image 2: When the keyboard is enabled, the card jumps up and when I close it's normal. Is this behavior normal? Or is there a fault in my CSS? I utilized styled-components for writing the CSS. CSS ...

Scrollbar styling functions flawlessly on Chrome and Safari, but encounters issues on Mozilla Firefox

Currently, the scrollbar is functioning properly in Chrome and Safari, but in Mozilla, it is not behaving as expected. Below, you can find my code. Although using jquery or JavaScript would be a quick solution, I am exploring CSS options first. ::-webki ...

Is using transform scale in CSS to scale SVG sprites a valid method of scaling?

I have been using an SVG sprite as a CSS background image in this manner: .icon-arrow-down-dims { background: url("../sprite/css-svg-sprite.svg") no-repeat; background-position: 16.666666666666668% 0; width: 32px; height: 32px; display: inline-b ...

Running the command "nexrjs create next-app" successfully generates all the necessary directories for the

After trying to install NextJS using both methods npm install next react react-dom and npx create-next-app appname, I noticed that my project directories are different from what they are supposed to look like: Instead of having pages, api(_app.js, index.j ...

The dimensions of the HTML table do not adjust properly when new items are being appended using JavaScript

I utilized this HTML Code to generate a table: <div class="noten_tabelle"> <table id="grades_table" style="width:100%"> <tr> <th>Subject</th> <th>Oral</th&g ...

Is there a way to style alternate iframes using CSS?

I need to select every second iframe in the image provided. Is it possible to achieve this using nth-child? I have tried with the following code (unsuccessfully): #main iframe:nth-child(2n+2) ...

Tips for eliminating excessive spacing between words to achieve justified alignment on a mobile device

please add image description hereMy issue is with using text-align: justify; as it sometimes creates large gaps between words. I attempted to resolve this for mobile devices by using text-justify: inter-word;, but unfortunately, it did not work. @media sc ...

The impact of adjusting the article's margin-top on the margin-top of

I want these two divs to have different positioning. The sidebar should remain fixed at the top. Why is the sidebar aligned with the article div instead of sticking to the top? body{ margin: 0; padding: 0; } div#sidebar{ background-color: yel ...

Do overlay elements have to be positioned at the bottom of the HTML body every time?

As I delve into the world of JavaScript frameworks, one common trend I've noticed is that elements like dialogs, tooltips, and alerts usually appear at the end of the body tag. With my own implementation of these UI elements, I am determined to make ...

Apply a specific style to the initial element generated by *ngFor

My current method for displaying a certain number of * characters is utilizing the code snippet below: <div *ngFor="let line of lines; let i=index">*</div> I am interested in applying a margin only to the first element. The margin value sh ...

Mismatched heights of grid elements

Currently, I am utilizing Tailwind CSS V3 and struggling to set the consistent height of grid items. The only way I can achieve uniform height is by assigning them a height of h-96. However, when I attempt to use breakpoints such as lg:h-80 and md:h-96, th ...