Linear-gradient() not responding to 2-second transition

Hey there, I'm working on a div element that has a linear-gradient().

When hovering over the div, it should take 2 seconds to expand to a width of 200px and add blue and green to the linear-gradient().

However, currently, the blue and green are being added immediately while the expansion takes 2 seconds.

Does anyone have a solution for this?

Below is my code:

div {
  background-image: linear-gradient( to right, red, yellow);
  background-color: red;
  width: 100px;
  border: 2px solid black;
  border-radius: 5px;
  transition: 1s;
}
div:hover {
  width: 200px;
  background-image: linear-gradient( to right, red, yellow, blue, green);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
</head>
<body>
    <div>hello world</div>
</body>
</html>

I would also like the blue to appear first and then the green.

Answer №1

To keep the background image unchanged, adjust its final size and let the expanding div reveal more or less of it

div {
  background-image: linear-gradient( to right, red, yellow 100px, blue, green);
  background-color: red;
  background-size: 200px;
  width: 100px;
  border: 2px solid black;
  border-radius: 5px;
  transition: 1s;
}
div:hover {
  width: 200px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
</head>
<body>
    <div>hello world</div>
</body>
</html>

Answer №2

Transitioning with background size is a great way to achieve smooth transitions, especially when using linear gradients which do not natively support transition effects.

div {
  background-image: linear-gradient( to right, red, yellow, blue, green);
  background-color: red;
  width: 100px;
  border: 2px solid black;
  border-radius: 5px;
  transition: 1s;
  background-size: 200% 200%;
}
div:hover {
  width: 200px;
  background-size:100% 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
</head>
<body>
    <div>hello world</div>
</body>
</html>

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

firefox compatibility issues with jquery ajax

Trying to establish a connection with a URL and expecting a large object in response has proven to be challenging. The process appears to function smoothly until an error occurs during the return reply, triggering the .ajax.error() function. Unfortunately, ...

Dimensions of image details

I am working on an article that includes an image with a description. The challenge is that the description should not exceed the width of the image, but since the image width varies, fixing the width with a class .article-image is not a viable solution. ...

Is JSDOM able to handle media queries?

I understand that JSDOM may not have all the features of a full-fledged browser, but there is mixed information out there about its CSS support. While I've personally seen it parse CSS, responsive styles don't seem to function as expected. Can an ...

Design an interactive quarter-circle using CSS styling

My goal is to create this element using CSS, however, the content inside needs to be dynamic. I initially attempted to use border-radius, but realized it is unable to achieve the desired outcome. If anyone has a solution or can offer assistance, I would g ...

What is the best way to deactivate an <a> tag in React after it has been clicked?

Is there a way to deactivate the anchor tag below in React once it has been clicked? The onClick function is not functioning on the anchor tag. <td align="left"> <input type="file" accept=".csv,.xlsx,.xls" ...

Is it possible for someone to assist me in making my map stay in a fixed position?

How can I make sure that my map stays in a fixed position on the screen even when the user scrolls? Here is the code snippet I am using: #map { float: right; height: 545px; width: 40%; margin-top: 12px; margin-right: 30px; posit ...

Django unable to locate any available templates

While working on a project referenced in the book "Lightweight Django_ Using REST, Websockets & Backbone [Elman & Lavin 2014-11-13]", I encountered an issue where my templates were not being found. Can anyone provide assistance? Here are my settings: im ...

Display issue with ul and li elements in Safari browser causing menu to appear incorrectly

Although I am new to CSS, I managed to create a menu on my website that hovers over the background video successfully. It is a basic menu using a ul with li elements and it appears as intended in all browsers except Safari. In Safari, the menu items stack ...

Problem with CSS dotted borders appearing as dashed in Chrome when applied to adjacent columns in a table

After testing the code on both Chrome and Firefox browsers, I noticed that they display the border differently. In Chrome, there is a dash in between adjacent cells while in Firefox, there are no dashes rendering. Can anyone suggest a solution to fix thi ...

Clicking on two svgs that overlap, on the other hand

Creating a web page buttons panel using .svg files has been an interesting challenge. After adjusting margins and other CSS attributes, I managed to position the buttons as seen in the code. The issue at hand: I'm facing difficulty with making both b ...

When selecting a new tab in HTML, the current page position remains unchanged. However, I would like the page to automatically scroll to the

In the design of my website, I have incorporated buttons that are linked to various pages using navigation tabs. However, when these buttons are clicked, the view maintains its position on the new page (e.g., halfway scrolled through the page). Instead o ...

Images are suddenly encircled by a mysterious border

On my wordpress website, I encountered a strange issue with some photos. Specifically, an image of a cross in a circle is getting a weird border around it, but only on certain resolutions (such as width:1506). Despite inspecting the element, I couldn' ...

Sibling proximity: an excess of elements separating the regulations

Is there a way to make a hidden p element appear when hovering over the first visible element in a tree structure? I found some help on Stack Overflow suggesting the use of adjacent sibling selectors, and while it works well with a simple example, it fails ...

Unable to view Mp4 video on Internet Explorer 11

My background video is in mp4 format and functions properly in Chrome, Firefox, and other browsers, but it does not work in Internet Explorer 11. To visit the website, click on "access as a non-secure site". ...

How can I incorporate multiple quality sources into a flowplayer using Angular?

Is there a way to add multiple sources with different qualities like 1080p, 720p etc.? Thank you in advance. flowplayer('#my_player', { src: '../assets/videos/video_1080p.mp4', // title: 'This is just demo&apo ...

The border color remains unchanged

I have designed a div with a border image on the right side and I want to add a colored border on the left side. However, when I change the color, it doesn't appear. How can I resolve this issue? Check out my FIDDLE for reference. CSS: div{ heig ...

I'm looking for a library or plugin that can be used to develop a customizable sidebar window with docking, resizing, and pinning capabilities similar to the ones found

In Visual Studio, the solution explorer and toolbox windows can be hidden at the side unless pinned. They are also resizable. Is there a way to achieve this same behavior using CSS? Has anyone created a jQuery plugin or similar tool for this? ...

Managing broken images within an internet browser

Is there a way to hide the broken image icon, similar to what's shown at '', when no image is displayed? I've set up a comment section using PHP and CSS that allows users to leave comments and upload photos. However, if a user decides t ...

Discovering the Style of CSS Using JavaScript

I have implemented a JavaScript file to validate whether the text-box is filled out correctly or not. My goal is that once all the text-boxes are correctly filled out, the application should proceed to the next page or trigger some action (in my case, disp ...

Combobox fails to dynamically update

Currently, I am attempting to create a dropdown menu that dynamically updates based on changes made in another dropdown menu. Despite my efforts, I have not been able to find a suitable solution and feel quite lost at this point. In an attempt to troubles ...