What is the best way to create a dynamic page hero image that animates upon page loading?

My website has a banner image that loads using the following code:

@keyframes animatedBanner {
  from {
    background-position-y: bottom;
  }
  to {
    background-position-y: top; 
  }
}

.page-hero {
  background-image: url(https://via.placeholder.com/428x100);
  background-repeat: no-repeat;
  padding-top: 214px;
  padding-bottom: 214px;
  text-align: center;
  animation: 2s ease-out 0s 1 animatedBanner;
  background-size: auto;
}
 <div class="page-hero">
    <div class="inside-page-hero grid-container grid-parent">
        <h2>this is an image</h2>
    </div>
</div>

While this code successfully animates the image upwards when the page loads, I'm facing an issue. I want the image to span the entire width of the screen regardless of the device's width. I tried using 'cover', but then the image doesn't scroll at all.

Is there a way to make this design responsive for the image?

Answer №1

Working with backgrounds can be a bit challenging, especially when it comes to animating the position. Specific sizes are required for a seamless effect. I have provided a comprehensive explanation in my answer (Using percentage values with background-position on a linear-gradient) outlining the reasoning behind the values used:

@keyframes animatedBanner {
  from {
    background-position: 50% 200%;
  }
  to {
    background-position: 50% 50%; 
  }
}

.page-hero {
  background-image: url(https://picsum.photos/id/1068/800/400);
  background-repeat: no-repeat;
  padding:100px 0;
  text-align: center;
  animation: 2s ease-out animatedBanner both .5s;
  background-size: auto 200%;
}
<div class="page-hero">
    <div class="inside-page-hero grid-container grid-parent">
        <h2>this is an image</h2>
    </div>
</div>

To achieve the opposite direction

@keyframes animatedBanner {
  from {
    background-position: 50% -100%;
  }
  to {
    background-position: 50% 50%; 
  }
}

.page-hero {
  background-image: url(https://picsum.photos/id/1068/800/400);
  background-repeat: no-repeat;
  padding:100px 0;
  text-align: center;
  animation: 2s ease-out animatedBanner both .5s;
  background-size: auto 200%;
}
<div class="page-hero">
    <div class="inside-page-hero grid-container grid-parent">
        <h2>this is an image</h2>
    </div>
</div>

If you prefer to avoid using background-position and background-size, consider utilizing pseudo-elements

@keyframes animatedBanner {
  from {
    transform: translateY(100%); /* or -100% */
  }
}

.page-hero {
  padding: 100px 0;
  text-align: center;
  position: relative;
  z-index: 0;
  overflow: hidden;
}

.page-hero:before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  background: url(https://picsum.photos/id/1068/800/400) center/cover;
  animation: 2s ease-out animatedBanner both .5s;
}
<div class="page-hero">
  <div class="inside-page-hero grid-container grid-parent">
    <h2>this is an image</h2>
  </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

JavaScript function for automatic scrolling to the bottom of the page is not functioning as expected

I'm working on incorporating a terminal/console feature into my website. I came across the JavaScript functions for scrolling down a page, namely window.scrollTo(0,document.body.scrollHeight); and window.scrollTo(0,document.querySelector(".fakeSc ...

Title Bar: Excessive gap; inaccurate navigation to nearby link

I've been trying to align a horizontal navigation bar right below a banner and link directly to specific sections on the same page. No matter what I do, I can't seem to remove the white space between the banner image and the navigation bar. It ...

Exploring the functionalities of jQuery and Local Storage: Understanding the Selector's various states

I am currently developing a new feature for font customization on a website. The drop-down menu offers several font options to choose from. When a user clicks on one of these options, the following code is executed: jQuery(function($) { if (localStorage.g ...

Incorporate descriptive text to my HTML slider

How do I customize my carousel to display unique titles or captions for each slide? Here is the code snippet provided: <h1>Incredibly Basic Slider</h1> <div id="slider"> <a href="#" class="control_next">>></a> < ...

How to align all children at flex-start while positioning one child at the end?

I am currently working on a flex container that has 3 children. My goal is to have all the children align at flex-start, with the final child positioned at the bottom of the container. Can align-content be combined with align-self to achieve this layout? ...

Is it possible to swap out a webpage link for a different one based on the size of the screen?

I designed a website for a friend: www.donjas-wellbeingforkids.co.uk I set up a Facebook Messenger Contact Us Link directly from the contact page here: donjaswell-beingforkids contact It's functional on desktop, where it seamlessly opens Facebook Me ...

The hyperlink for social media is not functioning properly within a list element

Having some trouble with the href-tags for social media on my contact page. Clicking on them doesn't seem to do anything. Any ideas on what might be causing this issue? html { height: 100%; box-sizing: border-box; background-color: #001725; ...

The progress bar fails to update once it hits 100%

My circle progress bar is not resetting to start over when it reaches 100%. The code seems to work fine in my browser, but for some reason it's not working on JSFiddle. Here is the link: https://jsfiddle.net/BrsJsk/5e9hs69y/5/ window.onload = fu ...

Endless spinning using snap.svg

How do I make an SVG element rotate endlessly? Is there a way to make it rotate continuously without stopping? While Snap provides a way to rotate an SVG element by a specific amount, I want to know how to create an infinite rotation effect. Is there a so ...

Ensure that selected options are unique across multiple selections

Could you help me with a question that involves matching pairs of words in both Russian and English? <div class="form-group" id="question4"> <label for="q4FirstSelectEN">4</label> <div class="row"> <div class="col-lg ...

Adjust the background color of a column in HTML based on its corresponding color name

Is there a way to change the background color of a column in an HTML table if the value is green? We want the background color to reflect the color name. The data will be retrieved from a web service. Here is the HTML code used to display the data in the ...

Elements overlapping within div elements

Div container and p element are overlapping another Div that contains an image. The image needs to respond to a :hover CSS event but only a small portion of that Div container is able to sense the mouse hovering over it. Any suggestions on how to solve t ...

What is the best class to implement in my jQuery code?

Currently in the process of developing a customized Google Chrome extension. Encountering an issue or experiencing confusion regarding the selection of a specific class to integrate into my jQuery script, particularly with numerous classes associated with ...

Creating a button with multiple background gradients using CSS

When working on a button element that already boasts a gradient effect, my aim was to integrate a small play image directly in the center. However, achieving this without compromising the current layout and ensuring compatibility across various browsers pr ...

Issue with connecting Drupal 8 theme styling to website pages

Although the theme is functioning properly, the CSS styles are not being applied. Even when inspecting the developer console in Firefox, there seems to be a disconnect with the page. I verified that manually applying the CSS through the developer console i ...

Issue with Tooltip Position when Scrolling Sidebar is causing display problems

My goal is to create a Sidebar with Tooltip attached to its <li> elements similar to this example: Screenshot - Good Tooltip However, I am facing an issue where the position of the Tooltip breaks when scrolling to the bottom of the sidebar, causing ...

Looking to minimize the unnecessary spaces within your html tags?

I encountered an issue while creating my custom toast notification in React. There are unnecessary gaps between the elements, causing the size to be larger than intended. React Code import React from 'react' import './CustomToast.css' ...

Tips for making the height of <select> and <button> elements equal even with padding

I'm trying to make sure that both the 'select' and 'button' elements have the same height. Check out my JS Fiddle here Here's the HTML code: <select><option>...</option></select><button>Button& ...

Automatically adjusting tab heights with jQuery

Having an issue with the jQuery tabs on a school website project. Looking for some assistance with JavaScript. Check out the page here: 71.50.205.125/staugie.net/school/admin, it's my current work in progress. Created a user account with username "tes ...

Transforming react.js into HTML and CSS programming languages

I have a small experiment I need help with. As I am not familiar with react.js, I would like to convert my main.jsx file into pure HTML/CSS/JS without using react. The issue I'm facing is how to handle form data submission to the server in vanilla HTM ...