Is there a way to use CSS alone to smoothly transition the background color sliding down when adding or removing a class?

Hey there, I'm looking to add some pizzazz to my website by animating the background of a div. My goal is to have the new background slide down from above for a cool effect.

If you want an idea of what I can currently achieve and the context of my question, check out this example.

To make this happen, I plan on adding and removing classes as triggers:

.token {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
  border-radius: 50%;
  margin-bottom: 20px;
  transition: background 1000ms linear;
}

.red {
  background: red;
}

.blue {
  background: blue;
}

I've already created a simple animation, but I'm uncertain if CSS alone can give me the desired slide down effect.

My vision is to generate multiple classes, each altering the background of the applied div through a sliding down motion.

The animation should activate once a specific class is added, with that class dictating the color transition.

Answer №1

To achieve the desired effect, you can utilize linear-gradient to set the color while adjusting the background-size property.

Consider this approach:

let currentClass="";
function changeColor(className) {
  $('#token').removeClass(currentClass);
  currentClass=className;
  setTimeout(function() {
      $('#token').addClass(currentClass);
  }, 500);

}
.token {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
  border-radius: 50%;
  margin-bottom: 20px;
  background-size: 100% 0;
  background-position: 0 0;
  background-repeat: no-repeat;
  background-image: linear-gradient(white, white);
  transition: 0.5s background-size linear;
}

.red {
  background-image: linear-gradient(red, red);
  background-size: 100% 100%;
}

.blue {
  background-image: linear-gradient(blue, blue);
  background-size: 100% 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="token" id="token"></div>
<button onclick="changeColor('red')">Red</button>
<button onclick="changeColor('blue')">Blue</button>

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

Implementing and styling jQuery hamburger navigation menu

I'm struggling with implementing functionality to my hamburger menu. I've spent a day working on the design, but now I need it to actually work as intended. I have tried combining different code snippets I found, but as I am new to jQuery, I&apos ...

Layering elements using the z-index property in Internet Explorer 7,

Issue with text placement in a dialog div only occurring in IE 7. To see the problem, visit this page and click on any of the text boxes on the left side. Attempts to fix by adjusting z-index in the skin.css file for div.dialogFromAndTo did not resolve ...

Utilizing a combination of jQuery and JavaScript, construct an innovative image slider to meet

I'm working on creating a slider that allows users to navigate through reviews by clicking arrows, moving from review1 to review2 and so on. I've set up my HTML with the reviews and my CSS with hidden values for now. Any assistance would be great ...

How to effectively implement light and dark themes using SCSS colors?

Currently, I am utilizing Vue.js and SCSS along with PrimeVue and Element plus, where I am importing their variables. My objective is to dynamically change the theme color. In my dark-variables.scss file, I have defined various color variables for differe ...

Why does this CSS ticker only display the first four posts?

Hello, I'm interested in adding a News Ticker to my Rails application. I came across this CSS ticker: I'm facing an issue where it only displays the first four posts. I believe the problem might not be with my data retrieval using the .each loop ...

What is the best way to design three boxes in varying sizes?

I am currently facing an issue while trying to create 3 boxes: one big box and two small ones next to it. Each box contains images, but the problem is that the two small boxes are not responsive on any devices except desktop. I have been troubleshooting th ...

Simultaneous display of icon and text on Bootstrap toggle button

Currently, I am utilizing Bootstrap 3 along with Jquery. Within my coding structure, there is a button that holds an icon within a span tag (specifically using the glyphicon provided by Bootstrap). <button id="swapArchived" class="btn btn-default btn-s ...

Showing content in a single line causes it to drop down to the next

Check out my code snippet here: http://jsfiddle.net/spadez/aeR7y/23/ I'm curious about how I can make my header list align on the same line as the header, without dropping down. Here's the CSS code I have for the list: #header li { display: ...

Adjust `theme-color` as the class and theme of the page are switched

My website has a JavaScript toggle that changes the color/theme of the page by adding/removing classes on the html element. The default theme is white, the second theme is black (.dark-mode), and the third theme is beige (.retro). The JavaScript code for ...

Changing the opacity of the background when a Bootstrap modal is displayedHere's how you can

While working with a bootstrap modal, I noticed that the background content opacity does not change by default when the modal is open. I attempted to change this using JavaScript: function showModal() { document.getElementById("pageContent").style.opaci ...

CSS inline block is failing to create a new line

Hey there, I'm trying to create this webpage but I'm encountering an issue. I'm using the inline-block property, but it's not creating a new line; instead, everything is just staying in a straight line. I need it to move to a new line o ...

Revamping Tab Styles with CSS

Currently, I am working on a mat tab project. The code snippet I am using is: <mat-tab-group> <mat-tab [label]="tab" *ngFor="let tab of lotsOfTabs">Content</mat-tab> </mat-tab-group> If you want to see the liv ...

Applying CSS blur filter to container without affecting its content

I'm trying to create a hover effect where an image within a parent div transitions into a blurred state. However, I've run into an issue where if the image is set to 100% width/height of the parent div, there is a visible bezel of the parent&apos ...

Tips for altering the distance between dots on a line

.ccw--steps { margin: 10px auto; position: relative; max-width: 500px; } .ccw--step-dot { display: inline-block; width: 15px; border-radius: 50%; height: 15px; background: blue; border: 5px solid #e8e8e8; position: relative; top: -8p ...

Why is the line height dimension missing from my model box when padding and margin are both set to 0?

I'm currently working on a project involving CSS/HTML and using Bootstrap 4 for my layout. I have a menu where I want the boxes to be the same size as the font-size, so I've set the margin and padding to 0. .menu-ppal { display: inline; li ...

The bottom margin of the last element does not affect the size of its container

I'm curious as to why the margin-bottom of .content-b isn't affecting the height of the .container. .container { background: grey; } .content-a, .content-b { height: 100px; border: 1px solid red; margin-bottom: 100px; } <div class= ...

Guide to aligning navbar links at the center using Bootstrap 4

I am struggling to center the links in my navbar. I have tried using mx-auto and text-center but nothing seems to work. Here is my code: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <nav ...

Changing font styles using the jMenu jQuery plugin: A step-by-step guide

I'm having trouble changing the font-family using CSS with jQuery's "jMenu" plugin. Here is the CSS I currently have: .jMenu{ position : absolute; top : 80px; left : 0px; width : 100%; display:table; margin:0; padding ...

increasing the top margin on an HTML document

Looking to create a specific amount of blank space at the top of your HTML page? The space needs to be exactly X pixels tall. How can this be achieved effectively, while ensuring compatibility with Safari? ...

Combining jQuery and CSS for seamless background image and z-index chaining

I am facing an issue where I need to add a background image within a div containing a generated image with the class .result. Despite my attempts, I have not been successful in displaying the background image correctly. The code snippet I used is as follow ...