Implement a fade-in animation with opacity on the background image when the page is loaded

Is there a way to create a background image that fades in from invisible opacity to 50% opacity and remains there using only CSS, without any JavaScript? I came across a similar question on Stack Overflow, but the solution seemed too complicated for me. Perhaps it could help someone with a similar issue.

In my style.css file, I have attempted the following:

body {
    background-image: url("img/me.png");
    background-color: #cccccc;
    background-repeat: no-repeat;
    background-position: top;
    background-attachment: fixed;
    animation: fadeInBG 5s; 
}

@keyframes fadeInBG {
    0% { opacity: 0.0; }
    100% { opacity: 0.5; }
}

However, this applies the animation to the entire body of the page, including any text, rather than just the background image. The simple text in my HTML file successfully undergoes the animation, while the background remains static at 100% opacity. How can I apply the animation logic specifically to the background image?

Additionally, once the animation ends, the text abruptly returns to 100% opacity instead of staying at 50%, as intended. Any suggestions on how to address this issue would be greatly appreciated.

Answer №1

Below is the CSS code you need to include in your stylesheet:

body {
    animation-fill-mode: forwards;
}

For more information, check out this helpful link: https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode

If you want to achieve this effect, I recommend adding a div element:

.bg-img {
    background-image: url("https://media.istockphoto.com/photos/ginger-cat-portrait-picture-id1290233518?b=1&k=20&m=1290233518&s=170667a&w=0&h=C-eVqPpxcxCFqJsykl4rTzq0Kl995ZHCaLB9BgSgEzk=");
    background-color: #cccccc;
    background-repeat: no-repeat;
    background-position: top;
    background-attachment: fixed;
    animation: fadeInBG 5s;
    /* add */
    animation-fill-mode: forwards;
    position: fixed;
    inset: 0;
    z-index: -1;
}

@keyframes fadeInBG {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 0.5;
    }
}
<h1> Hello world! </h1>
<div class="bg-img"></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

Different width, same height - images arranged side by side in a responsive layout

The problem lies in the images: https://i.sstatic.net/rydNO.png Here is an example of desktop resolution: https://i.sstatic.net/uv6KT.png I need to use default size pictures (800x450px) on the server. This means I have to resize and crop them with CSS to ...

The table is too wide for its parent mat-nav-list, stretching to 100%

Here is the code snippet I am using to display a table inside a mat-nav-list: <mat-nav-list> <table> <tr>Node Information</tr> <tr> <td>Name</td> <td *ngIf="activeNod ...

What is the best way to display a message when a record cannot be found?

I have created a HTML PHP table page with full codes. The code is working fine, but I am facing an issue where it doesn't display anything if the record does not exist. I want to show a message when the record does not exist. <!DOCTYPE html> < ...

What is the best way to design bootstrap-style menus in React using TailwindCSS or styled-components?

As I dive into learning React, I find myself torn between using TailwindCSS and styled-components. While I am attracted to the simplicity of styled-components, I am hesitant about the lack of pre-built features that TailwindCSS offers, especially in terms ...

Error message notifying user that an index is not defined in an ajax

https://i.sstatic.net/AbqOp.pngThis may appear to be a repetitive question, but I assure you it's not. Despite my efforts on Google, the bug persists. The problem lies in the php script's inability to set the $_POST array to the value passed by ...

Establishment - Maintaining menu positioning without disrupting sticky functionalities

This is my first time using Foundation and I came across the "Magellan" menu feature, which makes the navigation sticky after scrolling down. Although this feature works well for me, I encountered a challenge. I need to position the menu at the bottom of ...

I have a feature where clicking a button subtracts 1 from the database, and every subsequent click adds 1 more

I have a button that, on click, decreases the value in the database by 1. Every time I click again, it alternates between increasing and decreasing the value by 1. HTML and PHP code: <form action="ind.php" method="post"> <inpu ...

Issues with Font-Face Implementation

Hey there! I'm currently working on incorporating a new font into my website using font-face. However, it seems like something might be missing from my code because the style isn't being applied correctly. Here is what I have so far: <div id= ...

Is the top margin of the page not properly aligned?

Upon loading my page, I noticed that the bar at the top covers the header. However, by clicking the hide bar arrow, the header becomes visible. When I click the reappear arrow, the margin adjusts automatically to place the header underneath the bar. I&apo ...

Ways to centrally position elements in a Bootstrap table

I came across this query and tried implementing the style mentioned, but unfortunately, my table items are not aligned vertically: https://i.sstatic.net/rMqSR.png This is how the HTML appears: .table > tbody > tr > td { vertical-align ...

CSS for Page Header with Scroll-Over Content

Having trouble creating a "collapsing header" design where the page content scrolls over the banner image. I've tried adjusting z-index and positioning properties in CSS, but can't achieve the desired effect. Any help or guidance would be greatly ...

Implementing a Fixed Navbar in VueJS on Scroll

I am seeking help with creating a Fixed Navbar on Scrolling using Vue.js. I initially wrote some jQuery code for this functionality, but now I want to transition it to Vue.js. The updated code can be found in a file named navbar.js. Previous jQuery CODE ...

The SVG image does not display in browsers other than Internet Explorer (IE)

I am encountering an issue with adding a menu toggle image in SVG format to my HTML. Unfortunately, the image is not displaying as expected. Here are screenshots for comparison between Explorer and Chrome: https://i.stack.imgur.com/74sqh.png https://i. ...

Pondering in AngularJS - what is the best way to alter the DOM during an AJAX call?

I'm completely new to AngularJS and trying to transition from a jQuery background. After reading through "Thinking in AngularJS if I have a jQuery background?" on Stack Overflow, I understand the concept but am struggling to implement it without depen ...

Submitting a particular entry in a form that has been dynamically generated through iteration

This code snippet pertains to the admin panel of a particular website. The page displays appointments requested by customers, allowing the admin to make changes based on availability. <?php while($row = mysqli_fetch_assoc($result)){ ?> <form ...

Opera and Internet Explorer have trouble properly applying css text-decoration

It appears that the CSS text-decoration style is not being correctly displayed in Opera 11 and IE 9, but works perfectly fine in Chrome, Firefox, and Safari. Can anyone offer a solution to fix this issue? Incorrect Rendering: Correct Rendering: Below is ...

Guide on how to save the selected user option in a PHP form

I'm having trouble getting the form to automatically set itself based on the state selected by the user from a drop-down menu of 50 states. The user's info, including their chosen state, is stored in an info array with $state. Everything else dis ...

The collapsed button on the Bootstrap 4 accordion flickers slightly as it expands, not reaching its full expansion

I'm currently working on implementing an accordion feature. I found inspiration from this component here on fiddle which utilizes bootstrap 4. While attempting to troubleshoot a bug in the SO forum, I noticed that on my page, the component seems to "b ...

What are some best practices for integrating CSS grid layouts with React for optimal results?

My current project involves developing a Single Page Application using ReactJs and CSS grid layouts for styling components. However, I've encountered an issue where the two technologies do not seamlessly integrate: CSS grid layouts are typically appli ...

A JavaScript syntax problem arises when attempting to fetch data (an object) from a Laravel controller

Encountering a JavaScript syntax error in my .blade.php file when trying to access $data->modal This is my controller function: public function buku_all($page, $modal) {$data = (object) [ 'sidebar' => "pelayanan", ...