Employing a linear gradient disrupts the flow of the CSS animation

I am attempting to create a slideshow using CSS with a fade in-out effect by combining images and linear gradients. The initial code I used worked perfectly for transitioning between images, but as soon as I added the linear gradient to each photo, the smooth transition stopped. Now, instead of animating, the background changes abruptly without any visual effects. Below is an example of the code that I initially tried:

.hero {
    height: calc(100vh - 100px);
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    text-align: center;
    justify-content: center;
    background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/slideshow_images/1.jpg');
    animation: changeBackground 15s infinite;
    animation-duration: 15s;
    animation-timing-function: ease-in-out;
    -webkit-animation-timing-function: ease-in-out;
}

@keyframes changeBackground {
    0%,
    6%,
    12% {
        background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/slideshow_images/1.jpg'), url('/slideshow_images/2.jpg');
    }
    24%,
    30%,
    36% {
        background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/slideshow_images/2.jpg'), url('/slideshow_images/3.jpg');
    }
    48%,
    54%,
    60% {
        background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/slideshow_images/3.jpg'), url('/slideshow_images/4.jpg');
    }
    72%,
    78%,
    84% {
        background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/slideshow_images/4.jpg'), url('/slideshow_images/1.jpg');
    }
}

Does anyone know how I can maintain the smooth animation effects from the original code snippet while also incorporating linear gradients into the images? It's worth noting that I included two image URLs in each group to resolve a previous issue where there was a flashing screen during background-image transitions.

Answer №1

Animating Elements

When it comes to animations, it's important to ensure that the values being defined are consistent throughout the animation. Mixing different types of values can cause issues, like in this example where the margin changes abruptly:

/* This will cause a flickering effect */
@keyframes animationTest {
  0%{margin:10px}
  100%{margin:auto}
}

To avoid these problems, make sure that the transition between values is equivalent. For instance, changing from 10px to 20px would work smoothly:

@keyframes animationTest {
  0%{margin:10px}
  100%{margin:20px}
}

Integrating a Second Background

Similarly, combining gradients with background images can lead to flashing effects. To prevent this, separate the values and apply the second background property without interfering with the ongoing animation:

/* Adding a relative position to .hero element */
.hero {
  position: relative;
}

/* Defining the second background independently. You can also include an animation here */
.hero::after {
  content: "";
  position: absolute;
  left:0;
  right:0;
  top:0;
  background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4));
}

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

CSS - selecting elements that are greater than a specific criteria N

I have multiple <p> tags in the body of my HTML. I want to display only the first two paragraphs and hide all the paragraphs that come after. However, the code I'm using doesn't seem to work as expected. <html> <head> < ...

Elements that intersect in a site's adaptable layout

I need to create a design similar to this using HTML/CSS with the Skeleton framework: view design preview I've experimented with various methods to overlap the background and image, utilizing absolute positioning for the image. However, this approach ...

Why is the defaultDate property not functioning properly in Material-UI's <DatePicker/> component with ReactJS?

I recently implemented the <DatePicker/> component from Material-UI ( http://www.material-ui.com/#/components/date-picker ) in my project. I encountered an issue while trying to set the defaultDate property to the current date on my computer, as it r ...

What causes the inability to separate the span size from the parent div when enlarging the span width?

Check out this Relevant Fiddle: https://jsfiddle.net/promexj1/1/ I'm working on adjusting the widths of two child spans within a parent div to be slightly smaller than the parent itself (due to starting translation 10px to the right for one of the sp ...

What's preventing my Angular list from appearing?

I am currently developing an Angular project that integrates Web API and entity framework code first. One of the views in my project features a table at the bottom, which is supposed to load data from the database upon view loading. After setting breakpoin ...

Seeking help on modfiying div content with AJAX - any tips for showing navigation using hash or anchor?

Looking to create a dynamic page that updates content within a div asynchronously while also providing users with direct access to specific content within the div by using anchors (e.g. www.website.co.uk/#page1). I've successfully implemented the upd ...

Adjusting the height of table rows based on the content within the <td> element

I am facing a challenge with a table that includes rows with 2 columns - one for images and the other for text. The issue arises when resizing large images in one column, causing the row to take its height from the image cell rather than the text cell. Sin ...

restricting the number of items displayed in a select box - including scroll functionality

Is there a way to add scrollbars to an HTML select box with around 50 items? The current list is too long and I would prefer to have it scrollable. How can this be achieved? ...

Is there a way for me to mimic the directory listing feature of a web server?

As part of my responsibilities, I often create customized websites for professors at my university. Recently, a professor requested a site with just links to files. I confidently assured him it would be a simple task since file links display automatically ...

relative font sizing based on parent element

My container is flexible in size, adjusting based on how the user moves elements around the screen. Inside this container, there is both an image and text. The image takes up 80% of the height of the container while the text should take up the remaining ...

ASP repeater exceeding container boundaries

Having trouble with my asp repeater that reads from a database and generates repeatable divs. The issue arises when the h4 spills over the div, particularly when using <%Eval. Any suggestions on how to fix this? Manual input divs don’t seem affected o ...

Retain the input values even after the page is refreshed

I have developed a simple form validation page in ReactJS. The input fields are validated using regex patterns. One issue I encountered is that if I enter data in the input fields and refresh the page before completing the form, the input fields are clear ...

Is there no method to capture the message "Encountered an unexpected URL from the web process:"?

Currently, I am in the process of converting an Objective-C application to Swift using WKWebView instead of UIWebView. This app primarily showcases local content. Initially, I had no issues displaying the first webpage in WKWebView. However, when I try to ...

Deactivate input fields when the checkbox is selected

In my ticket purchase form, you are required to fill in all personal information. However, there is an option to purchase an empty ticket without any name on it. This can be done by simply checking a checkbox, which will then disable all input fields. ...

Reviewing HTML for style elements (such as bold, italics, etc.)

My current project involves using edgarWebR to extract information from 10K (SEC EDGAR) filings. I am working on developing an algorithm that can classify each HTML element as normal text, a subheading, or a heading based on the formatting of the document. ...

"Storing a collection of PDF files in an array in TypeScript Angular - A step-by-step

Here we have an HTML code snippet that includes an input file element with Angular: <input type="file" class="btn btn-info" id="archivoPDF" #PDFfile value="Seleccionar PDF(s)" accept="application/pdf" multiple /> And this is the TypeScript code sni ...

Having trouble with a ReferenceError stating that the function is not defined? Learn how to troubleshoot and fix issues relating to JavaScript interacting with HTML

I am currently in the process of developing a straightforward web application. The main objective is to collect user input (specifically an Ethereum wallet address) and utilize the Alchemy API to display a list of transactions associated with that address. ...

What is the best way to navigate through images only when hovering?

I have a website that showcases a collection of images in a creative mosaic layout. Upon page load, I assign an array of image links to each image div using data attributes. This means that every image in the mosaic has an associated array of image links. ...

Comparing iPad and Desktop Devices

Working on the responsive site sandbox.mercomcorp.com, I encountered an issue where the CSS meant for an iPad device is affecting the desktop CSS. Shouldn't the iPad have its own separate media query from the desktop? Here's a snippet of the CSS: ...

How to properly adjust image size in a flex-direction column layout - tips and solutions

My mission is to place an <img> with text blocks above and below it inside a container of specified height. The exact heights of the text blocks and image are unknown. After extensive searching, my colleague provided a useful solution by using the ...