Creating an animated link with a dynamic underline featuring a trio of vibrant colors

I'm having trouble figuring out how to animate my links on hover with 3 different colors. I attempted using the linear-gradient property but it doesn't seem to be working. Any suggestions on how to proceed?

Below is an example of what I'm attempting to achieve with a single color.

var link = document.querySelector(".dropdown-item");

['mouseover', 'touchstart'].forEach(function (e) {
link.addEventListener(e, function () {
link.classList.add("is-active");
});
});

['mouseleave', 'touchleave'].forEach(function (e) {
link.addEventListener(e, function () {
link.classList.remove("is-active");
})
});
li {
  list-style: none;
}

.dropdown-item::after {
content: '';
margin: auto;
padding: 0 10px;
height: 2px;
width: 0;
position: relative;
bottom: -10px;
border: 2px solid transparent;
display: block;
transition: 0.3s;
}

.is-active.dropdown-item::after {
width: 100%;
background: #123456;
transition: width 0.3;
}
<ul>
  <li><a class="dropdown-item" href="#">EXAMPLE</a></li>
</ul>

Here's an example of the desired transition effect:

Result

If anyone has any ideas on how to achieve this, I would greatly appreciate your input.

Thank you

Answer №1

You may want to try using gradient backgrounds and multiple backgrounds:

li {
  list-style: none;
}

.dropdown-item {
  padding: 20px 10px;
  border: 2px solid transparent;
  display: block;
  transition: 0.3s;
  background-image: 
    linear-gradient(red, red), 
    linear-gradient(blue, blue), 
    linear-gradient(green, green);
  background-size: 0 10px;
  background-position: bottom left;
  background-repeat: no-repeat;
}

.dropdown-item:hover {
  background-size: 
    33%  10px, 
    66%  10px, 
    100% 10px;
}
<ul>
  <li><a class="dropdown-item" href="#">EXAMPLE</a></li>
</ul>

Another method you could use is:

li {
  list-style: none;
}

.dropdown-item {
  padding: 20px 10px;
  border: 2px solid transparent;
  display: block;
  transition: 0.3s;
  background-image: 
    linear-gradient(to right, red 33%,green 33%,green 66%,blue 0);
  background-size: 0 10px;
  background-position: bottom center;
  background-repeat: no-repeat;
}

.dropdown-item:hover {
  background-size: 100% 10px; 
}
<ul>
  <li><a class="dropdown-item" href="#">EXAMPLE</a></li>
</ul>

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

In React, the constant always has a default value of "Object : Default"

My current project involves using SlateJS and I am encountering an issue when trying to load data into a const using: imgData = data.get("file"); Everything works as expected if React is not imported, but once it is included, the object contains unexpecte ...

Tips on sending template variables to JavaScript

Greetings, I am currently facing an issue with passing a template variable to javascript in order to create a chart. Unfortunately, Django treats all js files as static files which means that dynamic template variables are not accessible within javascript. ...

Retrieving multiple images from a directory and storing the image filenames in a JSON array

Currently, I am attempting to locate and retrieve the images stored within a specific folder using the following code. This code successfully retrieves the image names along with the total count of images. Subsequently, my goal is to save this information ...

Exposing the secrets of the Ajax module

Here is the code snippet I am working with: my.data = function () { var getAuth = function (userName, password) { var model = JSON.stringify({ "UserName": userName, "Password": password }); var result; $.ajax({ url: m ...

angular-ui-tree, dynamically generate the tree using data retrieved from the DATABASE

I have implemented the angular-ui-tree library to present the folder hierarchy. The nodes are saved in a MongoDB database, with each node object structured as follows: { "node_name" : "Folder 1", "node_path" : "AAABBB", "node_id" : 103, "node ...

The system encountered a TypeError: Unable to access the 'nativeElement' property as it is undefined

Hello, I am a beginner in Angular 2 and currently working on an image cropping plugin. My goal is to display the image on a canvas element. Below is my HTML code: <div class="row"> <div class="col-sm-6"> <canvas id="layout" width="40 ...

Guide to creating a unit test for canActivate guard in Angular routing

Seeking guidance on writing a unit test for angular routing with the canActivate guard. Encountering an error when using the guard on routes, but no error is thrown without it. Would appreciate a suitable example along with an explanation. app-routing.mod ...

Is it possible to activate numerous hover effects on links by hovering over a div?

How can I trigger multiple hover effects simultaneously when hovering over my main div? I'm struggling to figure out how to make the line animate on all three links at the same time. Is it possible to achieve this using only CSS or do I need to use Ja ...

What are the best ways to format an outgoing email using inline CSS?

On my upcoming website, I have set up a single form for guests to subscribe. The issue I am facing is with styling the email that is sent upon submission. I attempted using inline CSS, but it doesn't seem to be working. Instead of transforming the cod ...

Glowing effects on svg shapes

I'm looking to add a pulsing light animation around an SVG half circle shape that I have created. After experimenting with CSS and Webkit, the closest I've come is achieving a pulsing light around the parent element, rather than the actual shape ...

What is the process for saving an image from a Three.js canvas?

Can you provide tips on saving an image from a Three.js canvas? I'm having trouble making Canvas2Image work with Threejs. The issue seems to arise because the canvas object needs a div to be attached to before it is defined. Check out this resource ...

The current registry configuration does not provide support for audit requests when running npm audit

I am facing an issue with one of my dependencies that is in the form of "protobufjs": "git+https://github.com/danieldanielecki/protobufjs-angularfire.git#master". I installed it using npm install --save https://github.com/danieldanielecki/protobufjs-angula ...

The issue of CSS transitions flickering in Internet Explorer

Can someone help me troubleshoot an issue with this code in Internet Explorer? CSS: .nav-fillpath a { width: 100px; height: 100px; position: absolute; display: block; bottom: 0%; left:0; right:0; color: #3e3e3e; margin ...

Unable to load CSS in Node.js when the parameter in Express.js is too long

I have been working on an application using Node.js, express.js, and ejs. I am utilizing the following code to incorporate ejs and load css: app.engine('.html', require('ejs').__express); app.set('views', __dirname + '/vi ...

I am interested in updating the color of the active item on the navbar

I am looking to modify the color of the active page, Here is the HTML code snippet: <html> <head> <title>John Doe - Portfolio</title> <link rel="stylesheet" href="custom-style.css"> <link rel=" ...

Data structure for Highcharts:

Recently, I've been experimenting with Highcharts (http://www.highcharts.com) in a test application built on rails 3.1.1 and HAML. As someone who is still new to JavaScript, I'm striving towards achieving a seamless integration of Highcharts. Wi ...

A guide on conditionally rendering components in React

When I try to add a nested if statement in JSX, condition ? true example : false example works perfectly. However, when I change it to if(condition) { ... }, it displays an error in the console: https://i.stack.imgur.com/TIBRo.jpg Example with one-line c ...

The hover effect in CSS animations is turned up too high

Check out my Reddit news feed design: https://codepen.io/Teeke/pen/YxXXaE When hovering over articles, the headlines get displayed. But if the article text is too long, part of the headline may go off-screen. Instead of changing the line-height, I' ...

Turn off and then turn on user input without exiting the textarea

I've been working on a small project that requires me to enable and disable text input in a textarea using key commands, similar to Vi/Vim's insertion and command modes. However, I'm struggling to find an elegant solution. Disabling the tex ...

Azure-Graph is reporting an error: 'Invalid or missing Access Token.'

In my Node.js project, I effortlessly integrate azure APIs. Logging in: const MsRest = require('ms-rest-azure'); MsRest.loginWithServicePrincipalSecret(keys.appId, keys.pass, keys.tenantId); Creating a resource group: const { ResourceManageme ...