As the background image shifts, it gradually grows in size

I'm attempting to create an interesting visual effect where a background image moves horizontally and loops seamlessly, creating the illusion of an infinite loop of images.

Using only HTML and CSS, I've run into an issue where the background image gradually increases in size over time without any clear reason why. I suspect that JavaScript or jQuery may be needed to solve this problem, but as a novice in these languages, I find myself stuck and unable to make progress. Here's a link to a CodePen showcasing what I've been able to achieve so far:

https://codepen.io/Agnis/pen/aEQjPB

* {
  margin: 0px;
  padding: 0px;
}

body {
  background-color: white;
}

@-webkit-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@-moz-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@-o-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}
...
<div id="back"></div>
<div id="front"></div>

The goal is to have the background image move smoothly to the left without increasing in size, contrary to its current behavior. Any assistance would be greatly appreciated!

Answer №1

One reason for this is the increase in size. By using the background-position:cover; property, the image will always fill the element, adjusting its size to match the element's dimensions. So, if the element becomes twice as wide, the image will also become twice as wide.

As your

<div id="back"></div>
continues to grow, the background grows along with it.


It is possible to animate a background image. Below is a simple example:

.elem{
    /* properties here */

    transition-property: background-position;
    transition-duration: 1s;

    animation-name: rotate; 
    animation-duration: 2s; 
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes rotate {
    from { background-position: -100%;}
    to {   background-position:    0%;}
}

Answer №2

By adjusting the left and top properties, you are gradually expanding the element and its background.

Instead, try animating the background-position property.

@keyframes bgMove {
  from {
    background-position: left top;
  }
  to {
    background-position:right bottom
  }
}

Also, don't forget to remove any unnecessary prefixes!

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

In the frontend, I seem to have trouble accessing elements of an array using bracket notation, yet strangely it works flawlessly in the backend

I am encountering a peculiar issue as a newcomer to coding. I have an array retrieved from the backend database, and my goal is to access individual elements of this array in the frontend using bracket notation. While I can successfully access the elements ...

troubleshooting responsive design issues with Bootstrap and PHP

this is the issue I'm facing and what I require: Please provide assistance with fixing the code for my website project. What exactly is the problem and how can it be resolved? Thank you. <?php include 'init.php'; $stmt = $con->prepar ...

Leveraging Angular CLI in conjunction with the newest AspNetCore Angular 4 Single Page Application template

I'm currently experimenting with using Angular CLI alongside the latest JavaScriptServices AspNetCore Angular Spa template. In the past, I would simply copy and paste a .angular-cli.json file into my project's root directory, change "root" to "C ...

Bootstrap slider aligned to the right side

I'm currently utilizing bootstrap 3 for my website. However, I encountered an issue with the slider when viewing the page on a mobile device as the image was displaying outside the viewport, shifted to the right. Additionally, in Firefox, the slider ...

Upon attempting to fetch input by name, Puppeteer reported the error message: 'Node is not clickable or not an HTMLElement'

This is the structure of my HTML: <div id="divImporte"> <p class="btn01"> <input type="button" name="Enviar Tasas" value="Enviar Tasas"> </p> </div> Here are the diffe ...

Tips on handling a nested HTML hyperlink in Selenium using Python with IE11

Can anyone assist me in resolving the issue of clicking on a hyperlink within an HTML page that contains nested HTML elements up to 4 levels deep? I have attempted various methods using XPATH, but they do not seem to work during runtime. I am utilizing Sel ...

Converting a CSS Submenu from Horizontal to Vertical Orientation

Below is the current code snippet for my menu navigation bar: To access my live blog, please click on this link: www.4time2fun.com <div id="topmenu"> <div id="navigation"> <ul class="categories"> <li class="articles"> <a href ...

Ways to update the title of a Magento widget

I've added a widget to showcase new products on my Home page and it's displaying perfectly. However, I want to get rid of the title "New Product" that comes with it. This pesky little thing needs to go! I'm having trouble locating the corr ...

What is the best way to implement a multi-row form in Vue.js?

Form Structure: <card-wrapper v-for="(passenger, index) in form.passenger" :key="index" :icon="['fas', 'user']" :title="`Passenger ${index + 1}`" class="mb-5" > <validation-observer ...

Div scrolling allows for parallel viewing of PDFs in a side by side arrangement

I have two scrollable elements on my webpage: one positioned on the left side and the other on the right side. The left element is a regular div, while the right element is an object containing an embedded PDF. <object width="100%" height=&quo ...

Is there a way to automatically change the display of an element once the user has closed the menu?

How can I ensure that the display of an element remains unchanged when a user opens and closes my website menu using JavaScript? ...

What is the best way to deliver hefty files to users? Utilize the node-telegram-bot-api

bot.sendDocument(id, 'test.zip'); I am facing an issue while trying to send a 1.5GB file using the code above. Instead of being delivered to the user, I receive the following error message: (Unhandled rejection Error: ETELEGRAM: 413 Request En ...

Is it possible to retrieve the final digit from a URL using NUXT/Vue?

My dilemma involves utilizing the SWAPI API to display a single result, whether it be a person or a planet. However, instead of providing a direct ID for each item, the API returns a complete URL in this format: "url": "http://swapi.dev/api/ ...

Unsuccessful attempt at implementing knockout data-binding on a dynamic webpage utilizing the Hot Towel API

I'm facing an issue while trying to bind knockout data to a dynamically generated input field. To briefly explain the canActivate(id) function, it retrieves details of all necessary objects for the page and stores them in vm.OPCLayoutVm. ...

Obtaining an image from a URL, saving it, and then animating it? That definitely

Looking to create a dynamic animation with images that update every 15 minutes from the following URL: How should I go about storing and looping through the most recent 24 images for the animation? Is using a MySQL database the best option or are there o ...

Explore lengthy content within Angular 2 programming

I have a lengthy document consisting of 40000 words that I want to showcase in a visually appealing way, similar to HTML. I aim to include headers, paragraphs, and bold formatting for better readability. Currently, I am developing an Angular application. D ...

Verifying that the data has been successfully saved using JavaScript

When a user submits a small chunk of data via AJAX using a standard update action and a remote form, the information is sent to the action as javascript. The response is then rendered in javascript utilizing format.js. def update @message = Message.wher ...

how to efficiently extract data from an XML file using JQuery and JSON

When I use the alert function, it returns a string like this: data "<?xml version="1.0" encoding="utf-8" ?> <xml xmlns="http://www.opengis.net/kml/2.2"> <Document> <Name>John Smith</Name> < ...

Encasing a series of AJAX calls in a function: Implementing JavaScript and JQuery

I am facing an issue with a chain of two AJAX requests within a forEach() loop, where the second request relies on the response of the first. I have tried to wrap this code block in a function for reusability, but it seems to stop working. As a beginner in ...

Looking to use regex to search through a webpage's text content written in JavaScript

I am struggling with the re.search function and how to use it effectively. I have a block of text/javascript that I need to extract in order to convert it to json using json loads. $(function() { $product = $('#product-' + 12272257171); ...