CSS gradient in a vertical orientation

I am attempting to create a vertically animated linear-gradient, but for some reason it only works when I use pixel values instead of percentages for the background-position in keyframes. Using a percentage would be more flexible since the dimensions of the element may change.

When the gradient is oriented at 90 degrees, it functions properly, but it displays horizontally instead of vertically.

Below is the code snippet:

div {
  width: 50px;
  height: 150px;
  background: linear-gradient(0deg, yellow, orange, red, violet, purple, blue, yellow);
  animation: color 5s linear infinite;
  margin: 50px auto;
}

@keyframes color {
    0% {
        background-position: 0%;
    }

    100% {
        background-position: 200%; /* does not work */
        /*background-position: 0 -150px; -> works fine but not ideal */
    }
}
<div></div>

I have tried using negative values and other variations of background-position with no success.

Here is a link to the pen with the code: https://codepen.io/petruhaand1/pen/jOKMrWB

Answer №1

After referencing this response to a similar query, I was able to make some progress:

div {
  width: 50px;
  height: 150px;
  margin: 50px auto;
  overflow: hidden;
  position: relative;
  z-index: 0;
}

div:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  right: 0;
  background: linear-gradient( 0deg, yellow, orange, red, violet, purple, blue, yellow) top/100% 50%;
  bottom: 0;
  animation: color 5s linear infinite;
}

@keyframes color {
  0% {
    background-position: top;
  }
  100% {
    background-position: bottom;
  }
}
<div></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

Issue with Dropdown alignment in Bootstrap Navigation Menu

I'm currently utilizing Bootstrap to design a Navbar. However, I'm encountering an issue where clicking on the dropdown menu causes the items in the dropdown to extend outside of the window instead of adjusting the window size accordingly. To try ...

Arrange a table by simply clicking the header of each column using jQuery, all without having to replicate the full table

Looking to implement a table sorting function that only duplicates rows, not the entire table. When the user clicks on a column header, the data in that column should be compared and the rows rearranged accordingly. However, I'm facing some issues wit ...

I am looking for a way to retrieve the ids of all div elements that have the same x coordinate using document.elementFromPoint in JavaScript. Can someone help me with

Currently, I am facing an issue where I have two divs positioned at the same x coordinate. I am attempting to retrieve the IDs of both divs using document.elementFromPoint(). However, I am only able to receive the ID of one div. var elem = document.elem ...

Changing the color of a Java HTML applet with a simple click操作

Hey everyone, I could use some help. I'm currently studying IT and we have just started programming with Java. Our task was to create an HTML site with a Java Applet that would switch the background and foreground color of a textbox on click. Here is ...

Tips for downsizing a large image to fit into a smaller area

I am working on a page layout that features a small circular navigation element. However, I am facing an issue with fitting a large picture within the boundaries of this small circle without it overflowing and causing alignment problems. Does anyone have ...

The HTML markup is failing to display correctly

I'm struggling with the HTML and jQuery code I've written. Can anyone help me identify what's wrong with it? Below is the code snippet: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml ...

Extracting the value of an HTML element from a string variable in AngularJS

I am facing an issue with my application where the content of an HTML element is received as a template from the server. I am attempting to assign this template, which is essentially a string, and have the variables within the template linked to the contro ...

What is the frequency of page rendering in Angular 2 before displaying it?

I've been working with Angular 2 on a project lately. In my template, I have a simple div that triggers a function in my .ts file to display basic text like so: <div>{{ test() }}</div> test(): void { console.log("Test text") ...

CSS class 'nav nav-pills nav-justified' does not stack tabs properly on mobile devices

Currently, I am utilizing the most recent version of Bootstrap within an Angular environment. However, I have encountered an issue where the class "nav nav-pills nav-justified" does not stack the tabs properly when the screen size is reduced below 768px. ...

Could there be a tag present in the DOM that is not visible on the page?

During my browsing session, I encountered a scenario in which I located an "anchor" tag within the DOM using xpath. However, the corresponding element was mysteriously missing from the page. To add to the confusion, when I tested the tag, the click() ope ...

[Vue alert]: Issue in created function: "TypeError: Unable to assign value to undefined property"

I'm currently in the process of developing a VueJS application where I have implemented a child component called Child.vue that receives data from its parent. Child.vue export default{ props:['info'], data: function(){ ...

Steps to dynamically update a button's href based on the active status of a class

I am facing a challenge in using Js and Jquery to dynamically change the href of a button based on the presence of a specific class. I am relatively new to Javascript and unsure if I am approaching this problem in the best way. Any suggestions on how to re ...

Achieving targeted text display on specific viewports using the visible class in Bootstrap

I've recently delved into learning Bootstrap, and I'm experimenting with creating a responsive page where certain divs will only be visible on specific viewports. I used classes like visible-xs, visible-sm, visible-md, and visible-lg for the diff ...

Show the initially chosen option in a dropdown menu when updating a record using mysqli

I recently came across a PHP page that handles relocation requests and allows users to edit them as needed. While the add page features dropdown boxes, the edit page simply displays them as text fields. This makes it difficult for users to select a differ ...

What is the best way to place a popover above a selected text section?

Imagine a situation where a body of text is present. When a word is highlighted within this text, a popover should appear with a tooltip positioned directly at the highlighted word. Think of it as similar to how a Mac displays definitions of words within ...

Can you point me to the location of the HTML emoji storage?

Initially, I decided to switch my icons from .png files to standard HTML emojis in an attempt to reduce the weight of the pages. These web pages are hosted on a small microcontroller - ESP32. While the majority of devices display the emojis correctly, the ...

Ways to activate javascript following a PHP submission?

It's a bit tricky to explain. function m(val){ var element=document.getElementById('othermethod'); if(val=='others') element.style.display='block'; else element.style.display=&apo ...

Show the table within the flex container

I have a question about CSS. Is it possible to apply display: table to a flex item? Whenever I try to do this, the layout doesn't behave as expected - specifically, the second flex item does not fill the available vertical/horizontal space. .conta ...

The RouteChangeStart event is triggered when the index.html page is first loaded

When it comes to the route change event, my preference is for it to be triggered when the user navigates to the next page from the initial one, rather than on the initial page load itself. In this scenario, I have associated the directive "meeee" with the ...

When utilizing the --watch flag, Node-sass will fail to function properly

Encountering an issue with the "--watch" or "-w" flag when running node-sass. After installing node-sass as a devDependency and setting up a script to compile SCSS code, everything runs smoothly without any flags. However, adding the "--watch" flag causes ...