Troubleshooting: CSS Animation Not Showing up on Screen

When I was attempting to create a JavaScript game in HTML, I encountered a problem. Despite ensuring that the syntax was correct and conducting some research online, the animation refused to display. No matter how many times I tried, it just would not work.

If you are willing to take a look, here is the HTML code:

#game {
  width: 500px;
  height: 200px;
  border: 1px solid;
  border-color: black;
  border-radius: 7px;
}

#player {
  width: 20px;
  height: 50px;
  background-color: red;
  position: relative;
  top: 150px;
}

#brick {
  width: 20px;
  height: 20px;
  background-color: blue;
  position: relative;
  top: 130px;
  left: 480px;
  animation: block 5s is infinite;
}

@keyframes block {
  0% {
    left: 480px;
  }
  100% {
    left: 40px;
  }
}
<html>

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/style.css">
  <title>
    javascript game!
  </title>
</head>

<body>
  <div id="game">
    <div id="player">

    </div>
    <div id="brick">

    </div>
  </div>
</body>

</html>

Any help or advice would be greatly appreciated. Thank you!

Answer №1

Even though you claim to have confirmed the syntax's correctness, your devtools should display an 'invalid property value' when setting the animation for #brick in the CSS.

This issue may stem from an 'is' included in the code. Perhaps you intended to use 1s instead, or maybe it was not supposed to be there at all?

Below is the snippet without the problematic section:

<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="css/style.css">
        <title>
            javascript game!
        </title>
        <style>
        #game {
    width:500px;
    height:200px;
    border: 1px solid ;
    border-color: black;
    border-radius: 7px;
}
#player {
    width: 20px;
    height: 50px;
    background-color: red;
    position: relative;
    top: 150px;
}
#brick {
    width: 20px;
    height: 20px;
    background-color: blue;
    position: relative;
    top: 130px;
    left: 480px;
    animation: block 5s infinite;
}

@keyframes block {
    0%{left:480px;}
    100%{left:40px;}
  }
  </style>
    </head>
    <body>
        <div id="game">
            <div id="player">
                
            </div>
            <div id="brick">
                
            </div>
        </div>
    </body>
</html>

By the way, using transform translateX instead of resetting the left position in an animation may result in more efficient processor usage.

Answer №2

adjusting various animation properties can make it work, such as animation-name: block;, animation-duration: 5s;, and

animation-iteration-count: infinite;

#game {
    width:500px;
    height:200px;
    border: 1px solid ;
    border-color: black;
    border-radius: 7px;
}
#player {
    width: 20px;
    height: 50px;
    background-color: red;
    position: relative;
    top: 150px;
}
#brick {
    width: 20px;
    height: 20px;
    background-color: blue;
    position: relative;
    top: 130px;
    left:0;
    animation-name: block;
    animation-duration: 5s;
    animation-iteration-count: infinite;
}

@keyframes block {
    0%{left:480px;}
    100%{left:40px;}
  }
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="css/style.css">
        <title>
            creating a javascript game!
        </title>
    </head>
    <body>
        <div id="game">
            <div id="player">
                
            </div>
            <div id="brick">
                
            </div>
        </div>
    </body>
</html>

Answer №3

#game {
    width:500px;
    height:200px;
    border: 1px solid ;
    border-color: black;
    border-radius: 7px;
}
#player {
    width: 20px;
    height: 50px;
    background-color: red;
    position: relative;
    top: 150px;
}
#brick {
    width: 20px;
    height: 20px;
    background-color: blue;
    position: relative;
    top: 130px;
    left: 480px;
 animation: block 5s infinite;
}

@keyframes block {
    0%{left:480px;}
    100%{left:40px;}
  }

There was a minor issue in your code with the usage of "is" in the animation property.

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

Tips on inline password field placement for a better user interface experience?

While creating a registration form, I ran into a user interface problem. There are two specific changes I'd like to make: The label for Confirm Password should be inline on the same line. The password input should have an eye icon embedded within it ...

Stylish Navigation Menu Logo/Site Name

Currently in the process of upgrading my website and I have my website name included as part of my menu, as opposed to a logo. This has been an easy solution that has worked well for me. For mobile screens, the template I purchased includes a slicknav men ...

issues with alignment between bootstrap div and canvas

I am currently working on a project with a canvas inside a div using three.js. My goal is to display a purple bar for user settings on the right side of the three.js window. However, I am encountering an issue where Bootstrap is not recognizing that I want ...

Creating a personalized stepper component (using custom icons) using HTML

Seeking guidance on how to achieve a design similar to the image below using HTML and CSS. After conducting extensive research, I have not been able to find a suitable solution. I attempted to modify this answer to fit my requirements, but it did not prod ...

Implementing image max-width and maintaining aspect ratios for responsiveness

Within the code snippet below and utilizing Bootstrap, a grid layout is defined with several .block components, each comprising an image and a title. The images are designed to be larger than the column size so that they can expand to full width for respon ...

Mobile version experiencing issue with Navbar not collapsing

The dropdown navigation bar on the mobile version of my website is not functioning properly. Despite several attempts, I have been unable to figure out a way to make it work effectively. <!DOCTYPE html> <html lang="en"> <head> & ...

Encountering a problem creating a hover overlay effect on a transparent PNG image within a parent div that has a background color

I'm struggling with creating an overlay hover effect on an image that has transparency. The issue I'm facing is that the black background of the parent div element is filling in the transparent parts of the PNG image, making it look like those ar ...

Ways to efficiently convert HTML form data into JSON format

Currently, I am in the process of developing an ecommerce platform. As part of this project, I have created a billing form to gather essential information such as email addresses and physical addresses from users. The intention behind collecting this data ...

Creating a dynamic image carousel using jQuery

Today, I completed the jQuery course on Code Academy and am now trying to create an image slider using it. While I have a general idea of how it should work, I feel like I'm missing a key element. My goal is to have the slider continuously running whe ...

Position a dynamic <div> in the center of the screen

My goal is to design a gallery page with a list of thumbnails, where clicking on a thumbnail will open the related image in a popup div showing its full size. The issue I'm facing is how to center the popup div on the screen, especially when each pic ...

Challenge with the contrast of text color on dark mode with a backdrop of light color texture (image)

Currently, I am using Bootstrap 4 and I have set a light coloured .png image as the background for the navbar and body. The footer area has a dark background with off-white text colour. By default, the text colour is black to complement the light-coloured ...

How can I navigate to a specific section within a Bootstrap modal without being redirected back to the origin of the modal?

Currently in the process of creating a basic front end website with Bootstrap for styling. One section I've developed includes modals, where users click a button to open a centrally located modal window. Desired Functionality I aim to enable users t ...

Scrollbars in Jquery are not visible anymore when adjusting the size of a div element, causing them to expand

I'm facing an issue with a layout containing a div that expands to the screen size upon clicking a button. Initially, the div has enough content to necessitate scrolling. However, after collapsing back to its original size, the scroll bar disappears a ...

A fixed header list using CSS with a single scroll bar

I am looking to create a scrollable list with a fixed header, where the scroll bar extends the full height of the parent but only scrolls through the nav items (keeping the logo in place). My current setup involves: <div className="home-page-nav"> ...

Issues with image sizing on mobile devices

After finalizing my header design, I encountered an issue with the mobile version of the website. The images in the header are not responsive and do not adapt well to different screen sizes. I need assistance converting the header design into functional co ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...

Ensure that every line of code within the button element contains all the required attributes

Struggling to get the underline on every section of my button. Attempted utilizing the -webkit-box-decoration-break property, but unfortunately it was unsuccessful. It is necessary for both the initial line (New Delhi,) and final line (India) to have an u ...

Struggling to align the push menu properly within the Bootstrap framework

I am currently utilizing Bootstrap as my UI framework and attempting to create a push menu on the left side of the page. While I have made progress towards achieving this goal, there are some bugs in the system that I am encountering. Specifically, I am ha ...

How to style material-ui input with outlined variant using css

Currently, I am attempting to replicate the material-ui outlined input. The background color and input styles are different, so simply setting the label position to absolute and pushing it up is not working for me. Any suggestions on how to achieve this? ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...