changing background color smoothly in a short time without using JQuery

Check out this link for some code

.back {
  margin: 20px;
  padding: 100px;
  background: yellow;
  font-size: 30px;
}
<div class="back">

            Fun place to stay. We got a golf cart to see the rest of the island, but the house is well-equipped and comfortable. Vanessa was kind to pick us up and make golf cart rental arrangements. We were here on our honeymoon, it was perfect for a memorable trip!
            Hosts easily available

        </div>

I'm looking to change the yellow background color over time using only CSS. There are jQuery solutions on StackOverflow, but I want a CSS-only solution that works across all browsers.

I tried using this resource, but the color reverts back. How can I make the new color permanent?

Answer №1

Make sure to incorporate the following updated code. Simply insert 'animation-fill-mode: forwards' to halt the animation once it has finished.

.back {
    margin: 20px;
    padding: 100px;
    font-size: 30px;
    background-color: yellow;
    -webkit-animation-name: example; 
    -webkit-animation-duration: 4s; 
    animation-name: example;
    animation-duration: 4s;
    -webkit-animation-fill-mode: forwards; /* Safari 4.0 - 8.0 */
     animation-fill-mode: forwards;
}
@-webkit-keyframes example {
    from {background-color: yellow;}
    to {background-color: white;}
}

@keyframes example {
    from {background-color: yellow;}
    to {background-color: white;}
}

Answer №2

To achieve a cool visual effect, try incorporating a keyframe animation:

@keyframes color-transition {
    to { background-color: lightblue; }
}

.element {
    animation: color-transition 1s forwards;
}

Answer №3

.back {
  margin:20px;
  padding: 100px;
  font-size: 30px;
  animation-name: changeColorSmooth;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

@-webkit-keyframes changeColorSmooth {
  0% {
    background-color: yellow;
  }
  50% {
    background-color: white;
  }
  100%{
    background-color: yellow;
  }
}



@keyframes changeColorSmooth {
  0% {
    background-color: yellow;
  }
  50% {
    background-color: white;
  }
  100%{
    background-color: yellow;
  }
}

This CSS code will smoothly transition the background color of an element from yellow to white and back to yellow, creating a visually appealing effect that repeats infinitely.

Answer №4

Here is the solution you've been seeking.

.element {
    border: 1px solid blue;
}

.element:hover {
    border: 2px solid red;
    -webkit-transition: border-color 500ms ease-in-out;
    transition: border-color 500ms ease-in-out;
}

Answer №5

give this code a shot

.back {
  margin: 20px;
  padding: 100px;
  // background: yellow;
  font-size: 30px;
}

div {
  background-color: red;
  -webkit-animation-name: example;
  /* Safari 4.0 - 8.0 */
  -webkit-animation-duration: 4s;
  /* Safari 4.0 - 8.0 */
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}


/* Safari 4.0 - 8.0 */

@-webkit-keyframes example {
  from {
    background-color: red;
  }
  to {
    background-color: yellow;
  }
}


/* Standard syntax */

@keyframes example {
  from {
    background-color: red;
  }
  to {
    background-color: yellow;
  }
}
<div class="back">

  Fun place to stay. We got a golf cart to see the rest of the island, but the house is well-equipped and comfortable. Vanessa was kind to pick us up and make golf cart rental arrangements. We were here on our honeymoon, it was perfect for a memorable trip!
  Hosts easily available

</div>

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

Series of responses cascading from one another

Users are experiencing an issue where the need for adjusting margins based on the depth of responses in posts. Currently, this involves setting a margin-left value of 40px for one level deep reactions and 80px for two levels deep, and so on. To address th ...

Arrangements of lines in website design for various screen resolutions

I am having an issue with the layout I'm working on - specifically, there is a visible outline of the div boxes at certain resolutions or when zooming in. This is what it should look like: However, this is how it appears at some resolutions or when ...

applying a margin to the cover div

I am currently working on a #cover element with the following CSS styling: #cover { background-color: #FFFFFF; height: 100%; opacity: 0.4; position: fixed; width: 100%; z-index: 9000; } The goal is to have it cover the entire visi ...

Prevent the movement of the first column in a table when rearranging rows up or down by utilizing

Feeling a bit stuck here, can't seem to figure out what I've missed. I've managed to move up or down a row in a table, but I need the value of the cell in the first column to remain unchanged. Here's my code: This is my HTML file: &l ...

Utilize HTML5 local storage to highlight a specific row in a table

Currently, we are in the process of developing a code that involves marking table rows using HTML5 local storage and jQuery. Below is the initial code snippet... $(document).ready(function () { $('table tbody tr').click(function() { ...

Steer clear of utilizing CSS pseudo-elements like :before and :after

I am seeking a solution to hide a <label> when viewing on a small mobile device. Below is the HTML code: <label class="page_createpassword_label" for="page_createpassword"> <span class="page_label_main">Span Text</span> <span c ...

Adjust the positioning of the content within each card inside the modal box

I require assistance in adjusting the CSS for different cards within the modal box. I would like all cards to have consistent column width for various content within them, creating a symmetrical appearance. Currently, the cards appear staggered. Also, plea ...

In mobile view, the grid text should be positioned on top of the input field

Created a basic grid layout with input fields. Everything looks fine on the PC, but on mobile devices, long text causes the input field to be positioned below the text. However, when the text is short like Lor, it appears next to the input field... I want ...

Creating a dropdown menu with an initial value fetched from a text file

I'm currently working on a school project that involves adjusting basic wireless parameters in a Linux router using command-line operations through an HTML interface. My experience has mostly been with PHP, and so far, I've been progressing well. ...

Using HTML video to fill entire div container

I'm currently facing an issue with using an HTML video as a fullscreen background replacement for an image. The problem is that the video overflows and covers the entire page instead of staying within the constraints of the header section. Here is th ...

What is the best way to accommodate 4 columns within a 3-column HTML table layout?

I am facing a challenge with my table layout. Normally, it has 3 columns, but I am trying to figure out how to fit 4 cells into one row while maintaining the same width. Any suggestions on how to achieve this? ------------- | 1 | 2 | 3 | ------------- | ...

Identifying browser compatibility for date input type with Angular 2 and above

Is there a reliable method to check if the browser supports <input type="date"> in Angular2+? I came across a suggestion on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date where they recommend creating the input element and chec ...

What is the best way to implement pushstate degradation – using backbone.history or history.js?

I am interested in implementing pushstate functionality, which involves making an ajax request, changing the URL, and adjusting the browser history stack. However, since pushstate is not universally supported by all browsers, I need to find a workaround f ...

Tips on customizing the appearance of JavaScript output?

I recently created a plugin for my website with JavaScript, and one of the lines of code I used was output.innerHTML = "Test"; Is it possible to apply CSS styles to this element, or is there an alternative method? ...

What is the technique to enable this div to be clickable?

I am trying to make each "card" of a WordPress plugin clickable on my website. I have inserted a Pure JS element with the following code: document.getElementsByClassName('fc_card-container').onclick = function() {alert('It works!');} ...

Expanding a wrapper to fit the entire width of its content

I am facing an issue where the background color of a wrapper does not adjust to the overflowing content within it. How can I make sure that the background stretches behind all the content, regardless of its size? <div style="background-color: black;m ...

Showing headings in the table vertically

I have a header1 and header2 with corresponding data1 and data2 that I want to display differently. h h e e a a d d e e r r 1 2 data1 data2 To enhance the presentation, I wish to add borders around the head ...

Issue with syntax when transferring an HTML element from the #div to the body

Attempting to duplicate an element and remove the original because of a z-index issue in IE. Planning to use conditional comments: <!--[if lte IE 7]> <script type="text/javascript"> $(document).ready(function() { var callCenter ...

Angular dropdown filtering techniques

I need a select HTML element that can be replicated multiple times on one page. Each select element should display options from a main list, filtering out any items already selected in other select elements unless they were just duplicated. However, when ...

I utilized the "data-target" attribute to ensure the active link retains its style. Is there a way to preserve the style when navigating away from an active link?

Can someone assist me with this re-upload? I need help tweaking my code to maintain style when navigating between pages using the "data-target" attribute. Currently, the style is being lost when moving from the original link (e.g., "link/sub01.html") to a ...