Synchronization Problem between Background-Color Transition and Before/After Opacity Transition

I'm currently experiencing difficulties trying to fade in a navigation item using before and after pseudo-elements.

When I hover over the nav item, it should transition its background-color from white to blue. It's a simple effect, but it also needs to display two background images by adjusting the ::before pseudo-element from 0 to 1 and the ::after pseudo-element from 0 to 1 as well.

The issue arises when even though I have set the same transition duration, the fade behavior of the element is slightly different compared to the background color transition on the element itself.

You can experiment with the hover effect (jsfiddle) by rapidly moving your mouse in and out to observe the issue more clearly.

If anyone could assist me in solving this mystery, it would be greatly appreciated :)

Here is my jsfiddle: https://jsfiddle.net/5qnw8ke4/

Here is a screenshot illustrating the transition problem: screen capture

a {
    display: block;
    width: 61px;
    height: 67px;
    margin: 50px;
    background-color: #fff;
    text-align: center;
    font-family: "Roboto", sans-serif;
    text-decoration: none;
    line-height: 67px;
    color: #259cff;
    position: relative;
    transition: background-color 0.3s, color 0.3s;
}

a::before, a::after {
    opacity: 0;
    height: 100%;
    position: absolute;
    top: 0;
    content: "";
    -webkit-transition: opacity 0.3s,width 0.3s,left 0.3s,right 0.3s;
    transition: opacity 0.3s,width 0.3s,left 0.3s,right 0.3s;
}

a::before {
    width: 12px;
    left: -12px;
    background: url("https://svgshare.com/i/J61.svg") no-repeat 0;
    background-size: auto 100%;
}

a::after {
    width: 12px;
    right: -12px;
    background: url("https://svgshare.com/i/J4j.svg") no-repeat 100%;
    background-size: auto 100%;
}

a:hover {
    background-color: #259cff;
    color: #fff;
}

a:hover::before, a:hover::after {
    opacity: 1;
    width: 17px;
}

a:hover::before {
    left: -17px;
}

a:hover::after {
    right: -17px;
}
<a href="#">My link</a>

Answer №1

If you want to simplify the effect without using SVG and instead relying on a single pseudo element for the entire shape, you can achieve it like this:

a {
    display: block;
    width: 61px;
    height: 67px;
    margin: 50px;
    text-align: center;
    font-family: "Roboto", sans-serif;
    text-decoration: none;
    line-height: 67px;
    color: #259cff;
    position: relative;
    transition:0.2s;
}

a::before {
   content:"";
   position:absolute;
   z-index:-1;
   top:0;
   bottom:0;
   left:0;
   right:0; 
   opacity:0;
   padding:0 15px;
   background:
    linear-gradient(#259cff,#259cff) content-box,
    linear-gradient(to top    right,transparent 47%,#259cff 50%) left /15px 100%,
    linear-gradient(to bottom right,transparent 47%,#d4ecff 50%) left /15px 100%,
    linear-gradient(to bottom left ,transparent 47%,#259cff 50%) right/15px 100%,
    linear-gradient(to top    left ,transparent 47%,#d4ecff 50%) right/15px 100%;
   background-repeat:no-repeat;
   transition:inherit;
}
a:hover::before {
  left:-17px;
  right:-17px;
  opacity:1;
}
a:hover {
  color:#fff;
}
<a href="#">My link</a>

Another concept involves using skew transformation:

a {
  display: block;
  width: 61px;
  height: 67px;
  margin: 50px;
  text-align: center;
  font-family: "Roboto", sans-serif;
  text-decoration: none;
  line-height: 67px;
  color: #259cff;
  position: relative;
  transition: 0.2s;
}

a::before,
a::after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  opacity: 0;
  background: #d4ecff;
  transform: skewX(-12deg);
  transition: inherit;
}

a::after {
  background: #259cff;
  transform: skewX(12deg);
}

a:hover::before,
a:hover::after {
  left: -17px;
  right: -17px;
  opacity: 1;
}

a:hover {
  color: #fff;
}
<a href="#">My link</a>

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

Recommendations for a UI library specializing in displaying analytical graphs

Looking to create analytical graphs of tweets similar to the site found at the following link Website-Link Curious about the top open source options available for this task? I've heard good things about Raphael.js. Any other recommendations? ...

Does the webpack style loader load only application-specific CSS, or is it designed to handle all CSS files?

I am in the process of improving the modularity of my front-end design by delving into webpack. This tool provides a style loader, which enables you to import a css file and inject it into the document like this: require("style/url!file!./file.css"); // = ...

Uninterrupted Horizontal Text Scrolling using Pure CSS

I am currently working on developing a news ticker that displays horizontal text in a continuous scrolling manner, without any breaks between loops. While I hope to achieve this using only CSS and HTML, I'm unsure if it's feasible. Here is my ini ...

Struggling to align Bootstrap toasts in the center horizontally

Currently utilizing Bootstrap 4.6.1 (due to compatibility issues with bootstrap-select and newer versions) and endeavoring to center some toasts horizontally within a page using the code below: <div class="d-flex flex-row justify-content-c ...

challenges with positioning div elements using relative CSS styling

This is a section of my code: CSS : .body .left, .right { position:relative; z-index:6; display:inline-block; } .body .left { top:0; left:0; width:100px; height:300px; margin-right:10px; borde ...

Unlock the power of jQuery to apply CSS transition effects and animations with precision

A web application utilized Bootstrap, featuring a toggle navigation for the sidebar drawer/navigation that was supposed to be animated by default. However, there appeared to be no animation happening. To address this issue, the following CSS code was imple ...

Is there a way to showcase a block of Python code using a combination of HTML, CSS, and Javascript to enhance its

On my website, I want to display code blocks similar to how StackOverflow does it. The code block should be properly colored, formatted, and spaced out for better readability. All the code blocks on my site will be in python. def func(A): result = 0 ...

The excessive offset of pixels is causing the popup dialog to appear misaligned

I'm having an issue with my modal popups where the dialog is rendering 200px to the left and about 100px above its intended position. Just updated jQuery. DevicesRightClickActionsMenuController.prototype.showActionsMenu = function(event) { right ...

The z-index is not functioning properly in terms of layering correctly

Seeking advice on z-index... I'm experimenting with the code and trying to get my id="hidebar" to appear above the "content" and "content-inner" divs. Apologies if this is elementary, but I'm having some trouble =D CSS structure file: body { ...

What is the process for altering the active color of a materialize icon?

Currently working on a web application for my school project and incorporating the Materialize framework. However, I'm having trouble modifying the default teal color of the active icon. https://i.sstatic.net/9eEfM.jpg ...

Is there a way to achieve a double background color effect in CSS while ensuring that the text is centered within the second background color element?

How can I achieve a layout similar to the one shown in this image: ul menu li tags Should I use two tags for each element? Here is an example: <ul class="menu"> <div class="outside"><li class="inside"><a href="#">Firefox</a ...

Strategies for Connecting CSS, JavaScript, and Image Files in Django

Being new to Django 1.9.5 and working on Windows, I am facing difficulty in linking my CSS, images, and JS files to the Django template. This is how my project structure looks like: Here is my settings page: BASE_DIR = os.path.dirname(os.path.dirn ...

"Prominent logo displayed at the center of the navigation bar with links fl

My goal is to achieve a navbar layout similar to this: https://i.sstatic.net/4LYcI.png However, my current navbar looks like this: https://i.sstatic.net/1PDHR.png I am working with Bootstrap 5 and I want the navbar links to be positioned on either side o ...

What could be causing this navigation item to vanish in Firefox?

Check out this link: http://jsfiddle.net/Nvntm/1/ In Firefox and some other browsers, the "Support" navigation item disappears while the other three remain. Why does this happen? Is there something incorrect in my code? ...

Flashing white when transitioning background images

My website features a div with a blurred image that I want to gradually unblur upon hovering, using transition-property and transition-duration. I have two versions of the image prepared - one blurred and one unblurred. However, when viewing online (desp ...

Approaching fashion from a new angle: incorporating perspective into

How can I style elements in the perspective to appear straight(3d) instead of flat due to perspective? I want to achieve a design where the alphabets and flag post image align perfectly. https://i.sstatic.net/HcHMC.jpg .container { perspective: 100p ...

Using Bootstrap to style the <option> element with RGBA colors

Struggling to figure this out and seeking assistance. I have a form using bootstrap where I can apply rgba color for my select tag but not for the options. Select CSS: select { background-color: rgba(0,0,0,0.25) !important; border: 1px solid rgba ...

The jQuery function for $(window).scroll is not functioning as expected

My challenge is getting the scroll to reveal my scrollTop value I've been working with this code: $(document).ready(function(){ console.log('Hello!'); $(window).scroll(function(){ console.log('Scrolling...'); var wScroll = ...

What could be causing my sticky positioning to not function properly when I scroll outside of the designated

My website is organized into sections, with each section corresponding to a hyperlink in the navigation menu. I've applied the CSS property position: sticky to my navbar, which works as expected within the section bounds. However, I want my red navbar ...

Unable to Change Color of Buttons in Jquery Mobile

Utilizing JQuery mobile for my application. <input data-mini="true" data-inline="true" data-theme="d" value="Login" type="submit" /> When clicking the button in iOS, the color does not change immediately. It only changes when the button is held for ...