Using CSS animations to create a smooth transition effect, the background will fade in when hovered over, remain visible, and revert back to

Imagine you have an HTML element that is red with transparency. When you hover over the element, it should turn solid red. After hovering and then stopping, the element should animate back to its original state after a set time period (X seconds).

Everything seems to be working fine so far based on the code snippet provided.

The issue arises when you stop hovering over the element and then go back to hovering over it. The initial animation starts again before the animation to the initial state has completed, resulting in an abrupt transition from full color to transparent and then fading back to full color.

Is there a way to resolve this without using JavaScript?

If you set the background of the div to the full color, it maintains the color when not hovering but also sets it back to the initial color. How can this be addressed? Removing the background only removes the color when not hovering!

Using opacity with a transition is not feasible as there is content inside the div.

div {
      width: 100px;
      height: 100px;
      background: rgba(50, 60, 95, 1);
      animation-name: fadeItOut;
      animation-duration: 1s;
      animation-delay: 2s;
      animation-fill-mode: forwards;
    }

    div:hover {
      animation-name: fadeItIn;
      animation-duration: 1s;
      animation-delay: 0s;
      animation-fill-mode: forwards;
    }

    @keyframes fadeItIn {
      0% {
        background: rgba(50, 60, 95, 0.3);
      }
      100% {
        background: rgba(50, 60, 95, 1);
      }
    }

    @keyframes fadeItOut {
      0% {
        background: rgba(50, 60, 95, 1);
      }
      100% {
        background: rgba(50, 60, 95, 0.3);
      }
    }
<div></div>

Answer №1

It seems like the use of animation might have made things more complex, whereas using a transition could have been a better choice. Animations involve a start and end point (the 0% and 100% keyframes), so when you hover in or out, the element first takes on the state defined at the 0% keyframe (since animation direction is normal) before transitioning to the 100% keyframe. If you quickly hover in or out (before the previous animation is finished), you will always see these abrupt changes due to the properties set at the 0% keyframe.

The following code snippet should provide the solution you are looking for:

div {
  width: 100px;
  height: 100px;
  background: rgba(50, 60, 95, 0.3);
  transition-property: background;
  transition-duration: 1s;
  transition-delay: 2s;
}
div:hover {
  background: rgba(50, 60, 95, 1);
  transition-delay: 0s;
}
<div></div>

Answer №2

Your solution is effective.

The issue you mentioned about the "horrible" effect is simply due to hovering over the element before the fadeOut animation completes.

You can improve this by reducing the animation delay of the div:

div {
  width: 100px;
  height: 100px;
  background: rgba(50, 60, 95, 1);
  animation-name: fadeItOut;
  animation-duration: 1s;
  animation-delay: 1s;
  animation-fill-mode: forwards;
}

I created a fiddle for you to visualize it: https://jsfiddle.net/oqyep7tt/

Overall, Harry's approach seems superior in comparison.

Answer №3

Check out this cool CSS animation!

.box {
  width: 100px;
  height: 100px;
  background: rgba(50, 60, 95, 1);
  transition: background 300ms;
  animation-name: fadeItOut;
  animation-duration: 1s;
  animation-delay: 0s;
  animation-fill-mode: forwards;
}

.box:hover {
  animation-name: fadeItIn;
  animation-duration: 1s;
  animation-delay: 0s;
  animation-fill-mode: forwards;
}

@keyframes fadeItIn {
  0% {
    background: rgba(50, 60, 95, 1);
  }
  100% {
    background: rgba(50, 60, 95, 0.3);
  }
}

@keyframes fadeItOut {
  0% {
    background: rgba(50, 60, 95, 0.3);
  }
  100% {
    background: rgba(50, 60, 95, 1);
  }
}

View the live demo here.

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

The Bootstrap navbar appears to extend beyond the content on the page

I am new to using Bootstrap. I have encountered an issue with the navbar on my website. The navbar appears to be longer than the screen size and the right button is being overlapped by the right side of the window. Unfortunately, I am unable to scroll the ...

What is the best way to implement two separate fonts in a document using the Monaco editor?

I have implemented react-monaco-editor in my project. In the overall CSS, I have defined a font family to be used as Helvetica, Tahoma, Arial, monospace. However, I also need to customize the font for a specific editor instance on the page to be "Fi ...

How can you give a div a background without any text inside?

I am struggling to create an HTML element with a background image using CSS, but the background does not show up unless there is text inside the element. CSS - #box { background:url(.....) } HTML - <div id="box">something</div> // this w ...

Tips on preventing the constant shifting of my webpage div elements when resizing the browser

Whenever I resize my webpage browser, the 3 text input boxes move and overlap, causing the page layout to become messy. View of The Browser in full screen (normal) View of The Browser when it's smaller (not normal) As a beginner programmer, I have ...

Having issues with the display of Bootstrap SelectPicker dropdown in a dataTable?

I am currently populating data into a jQuery datatable dynamically with the help of AJAX. Each row in the table contains a dropdown selectpicker from Bootstrap, displayed as follows: https://i.sstatic.net/g0czE.png However, I am experiencing issues with ...

How can I ensure my HTML/CSS webpage resizes automatically to fit all screen sizes?

I created a login screen for my website class term project which is due soon. It looks great, but I noticed that when I resize the browser, the entire page falls apart. Everything starts overlapping each other, which is quite comical. I've tried usin ...

The combination of Asp.net and Bootstrap offers a powerful

I am facing an issue with using Bootstrap in my ASP.NET project. The page displays correctly on a desktop, but when I resize the page to mobile phone size, the navigation panel does not stay in dropdown position. Here is my code: <%@ Page Language="C ...

Increasing the size of the center image by using CSS elements placed along the circumference

Is there a way to enlarge the center image by 1.25 times compared to the outer images? You can view the code on this codepen. For a simplified explanation of the code, check out this link. I have attempted to adjust the circle size but it ends up affectin ...

How to Align Text and Image Inside a JavaScript-Generated Div

I am attempting to use JavaScript to generate a div with an image on the left and text that can dynamically switch on the right side. What I envision is something like this: [IMAGE] "text" Currently, my attempt has resulted in the text showing ...

Guide to adjusting the image height as a percentage of the width through CSS

I have a situation where I need to display post thumbnails using a WP query/loop. Regardless of the width of the thumbnail, I want the aspect ratio to always be 4/3, meaning the height is 75% of the width. While setting the aspect ratio in pixels would be ...

Tips on concealing the parent div while only displaying the child div

Within my HTML, I have a parent div and a child div. I am trying to hide the parent div while still displaying the child div (which is a canvas element). I've attempted using show, hidden, and visibility options but none of them seem to be working pro ...

The full extent of the background color is not reaching 100% of the height

My content wrapper has a height set to 100%, but the issue is that the background color doesn't fully extend across all content. Here are some images and my code for reference. Any assistance would be greatly appreciated! You can see the white space a ...

Troubleshooting code: JavaScript not functioning properly with CSS

I am attempting to create a vertical header using JavaScript, CSS, and HTML. However, I am facing an issue with the header height not dynamically adjusting. I believe there might be an error in how I am calling JSS. Code : <style> table, tr, td, t ...

Can CSS `content` support the combination of different font styles?

I have set up a CSS element to insert some content. Here's the code: .foo:after { content: "Bold, italics"; } What I want is for the word "Bold" to appear in bold font-weight and the word "italics" to appear in an italic font-style. I know that ad ...

Require triggering action upon hovering and reverting back to the original state after 7 seconds

I am working on a website that incorporates various animation effects. My goal is to activate a gif (animation) when hovered over and have it return to a static state once the cursor is moved away. When hovering, I want Image1 to disappear and reveal ...

Three image resources combined to create a unique border design in CSS

I am currently working on designing a webpage and am in need of creating a background border. I have access to three image resources for this task: one for the left side, another for the top, and the third for the right side. My struggle lies in finding s ...

The content within a div block is having trouble aligning vertically

I need help with centering the content inside my fixed-top navigation bar vertically. I am using bootstrap to design my page, and the navigation bar consists of an image as the nav header and a container with links. The container surrounding these element ...

The document.write function in JavaScript is malfunctioning

I've been attempting to utilize the javascript function document.write to incorporate an external css file in my template. However, I am aiming to achieve this using Twig, like so: document.write('<link href="{{ asset('bundles/activos/cs ...

Is there a way to add Internet Explorer specific stylesheets to the Wordpress functions directory using enqueue?

I'm facing challenges setting up my Wordpress theme to be compatible with early versions of Internet Explorer. Most online tutorials have advised me to enqueue specific I.E stylesheets in the header, but this method has not been successful for me. Add ...

Troubleshooting: Issues with Mod_rewrite affecting CSS, JS, and Image functionality

Today marks my first time posting a query in this forum. I'm currently facing an issue with mod_rewrite. Below is the structure of my filesystem: site_new/stylesheets/layout.css site_new/javascript/somescript.js site_new/hallendetails.php ...