What is the best way to bring my image to life and move it to

Just starting out with HTML and CSS. Can anyone help me learn how to animate my image so that it moves from its current position to another location? For example, transitioning from "top: 280px; left: 600px;" to "top: 180px; left: 500px;"

I'm looking for someone to assist me through this process. Thank you. Below is the code I currently have:

#robot {
  position: fixed;
  top: 280px;
  left: 600px;
  border-radius: 50%;
  width: 50px;
  height: 60px;
}

body {
  height: 100%;
  width: 100%;
  background-image: url('TPHRG floorplan1.png');
  background-repeat: no-repeat;
  background-attachment: fixed;
  /* background-position: center; */
  background-size: 980px 400px, cover;
}
<img id="robot" src="https://img.favpng.com/20/7/18/clip-art-robot-free-content-image-vector-graphics-png-favpng-pJhfbKDrGy0yuQsKVTrjEu7br.jpg">

Answer №1

Here is an easy-to-understand demonstration of how a transition works between two positions.

The important thing to remember is to include the transition property in your element (#robot), specifying the property you want to animate, the duration of the animation, and the easing function, among other things. For more examples, refer to the official documentation.

If using the all keyword for convenience in the transition rule, it's recommended to explicitly define which property to animate in the separate transition-property rule for better performance.

In this example, I'm utilizing the :hover pseudo-class to initiate the animation. However, it could also be triggered when the page loads or when a specific class is applied to your element via JavaScript.

Hopefully, this explanation clarifies the concept for you!

.container {
  width: 500px;
  height: 150px;
  position: relative;
  padding: 10px;
  border: 5px solid green;
}

.cat {
  position: absolute;
  top: 50px;
  left: 50px;
  transition: all 1s ease-in;
  transition-property: top, left;
}

.container:hover .cat {
  top: 10px;
  left: 300px;
}
<div class="container">

  Hover over me!
  <img class="cat" src="https://placekitten.com/100/100" alt="A cute cat">
  
</div>

Answer №2

Indeed, achieving this effect is possible with the use of CSS.

<html>
  <head>
  <meta charset="utf-8">
  <title></title>

  <style>

    #robot {
      position: fixed;
      top: 280px;
      left: 600px;
      border-radius: 50%;
      width: 50px;
      height: 60px;
      -webkit-animation-name: example; /* Safari 4.0 - 8.0 */
  -webkit-animation-duration: 4s; /* Safari 4.0 - 8.0 */
  animation-name: example;
  animation-duration: 4s;
    }

    body {
      height: 100%;
      width: 100%;
      background-image: url('TPHRG floorplan1.png');
      background-repeat: no-repeat;
      background-attachment: fixed;
      /* background-position: center; */
      background-size: 980px 400px, cover;
    }
    
/* Safari 4.0 - 8.0 */
@-webkit-keyframes example {
  from {top: 280px;left:600px;}
  to {top: 180px;left:500px;}
}

/* Standard syntax */
@keyframes example {
  from {top: 280px;left:600px;}
  to {top: 180px;left:500px;}
}

  </style>

  <body>

    <img id="robot" src="https://www.w3schools.com/images/compatible_chrome.gif">

    <?php ?>

  </body>
</html>

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

Graphic aligned with textual content

Within my table cell resides text and an accompanying image icon. However, the presence of the image causes the text to shift downwards, not aligning in the center as desired: I am now seeking a solution that will allow the text to be centered within the ...

Creating dynamic text overlay on images using CSS with adjustable position

I have a collection of 11 divs, with 8 containing images and 3 displaying ':'. https://i.sstatic.net/Vfljg.png All 11 boxes are enclosed within a class called "timer", structured as shown below: <div class="timer"> <div><img&g ...

Creating an array of logos in ReactJS with the help of TailwindCSS

After seeing multiple implementations of this feature, I find myself struggling to search for a solution. I can only access the HTML and CSS through inspecting elements, wondering if others are facing the same issue as me. Typically, we aim to implement t ...

Is there a way to adjust the scrolling speed of a background image to be slower than the rest of

I'm searching for an example similar to this: Are there any efficient javascript solutions available? I've had difficulty implementing the code I've come across so far. ...

What is the easiest way to import a CSS file into just one specific page in Ionic2?

Take, for instance, my desire to import the following: <link rel="stylesheet" href="mycss.css"> into example_page. I attempted importing it from index.html, but I fear that by doing so, the CSS will be loaded on every page each time I run my app. I ...

Is there a way to modify the browser's user agent using JavaScript or HTML?

I currently have an application that is running on IE7. However, an analytics tool I rely on has recently been updated and now requires a minimum of IE8. Is there a way to modify the User Agent in IE7 using JavaScript or HTML to trick it into being recogni ...

jquery-enhanced tabs with versatile functionality

HTML: <ul class="tabs"> <li><a href="#tab-one" class="current">Residential</a></li> <li><a href="#tab-two">Commercial</a></li> <li><a href="#tab-three">Agricultural</a></li> < ...

Utilizing Cookies within an HTML Page

My current code is functioning perfectly, accurately calculating the yearly income based on the input "textmoney." I have a link to a more advanced calculator for a precise prediction. My goal is to find a way for the website to retain the data input from ...

Offspring containers fail to adjust in size relative to their parent container

I am working with a parent div that contains 5 child divs. The parent div does not fill the entire width of the window, but the 5 children share the same space and properly fill their parent. <div class="parent"> <div class="child ...

Incorporate a PHP file from an external website, as plain text or HTML

My dilemma involves a php file that resides on a particular website (for example: ). This script generates html content, primarily consisting of text formatted with tags like <h2>, <p>, and so on. The task at hand is to take this text and embe ...

Nested Tables in JavaScript: Creating Tables within Tables

Recently, I have been analyzing student data and noticed a recurring structure. While preparing to present information on student performance within the discipline, I also became interested in showcasing a history of new students. It was suggested that hav ...

Ways to ensure next/image takes up all available grid space

Is there a way to make the next/image element span from grid 1 through 8? It seems to work fine with the imgtag but not with next/image. .project { display: grid; margin-bottom: 4rem; grid-template-columns: repeat(12, 1fr); align-items: center; } ...

The Django server fails to display the CSS files upon activation

Despite having all the static files in place, only the plain HTML loads without any styles for some unknown reason. I've attempted using collectstatic and even setting up a new project, but to no avail. STATIC_URL is also properly specified along with ...

Is it considered undesirable to have HTML source code all condensed into a single, uninterrupted line?

I have been thinking about my approach in my PHP CMS application. I currently capture most of the generated HTML into buffers and then flush their content at the appropriate location in the template page using my custom buffer class. Recently, I added a me ...

Is there a glitch in my CSS code or am I simply misunderstanding how to center a div?

After doing some research, I noticed that all the tutorials I came across recommended following the same steps. However, when I implemented the code below: .center{ width: 50%; margin: 0px auto; } It centered all six items but didn't align them in 3 ...

Build a Container Div using DOM/XPATH techniques

I have just initialized a new DOM XPATH OBJECT. After performing several operations, I saved my result using SaveHtml $String[] = $dom->saveHTML(); Next, I placed the content into a file. file_put_contents($filename, $string); The HTML Structure l ...

What is the best way to prevent blank space when splitting a div into two parts?

Currently in the process of working on a school project that involves creating a website with an integrated database. The foundation for this project is an existing website from a previous assignment, created using PHP/HTML. The layout of my prior website ...

The multi-line declaration prettier indicates that a newline is expected after the colon

When using Prettier to format code in a scss file, it is formatting this specific part: @font-face { font-family: 'Encode Sans Condensed'; src: url('/pf/resources/fonts/EncodeSansCondensed/EncodeSansCondensed-Thin.ttf') format('tr ...

Tips for applying personalized CSS to individual Toast notifications in Angular

MY QUESTION : I am looking to customize the CSS of a single toast used in Angular components. While there may be multiple toasts, I specifically want to style one particular toast differently. For example, the toast image can be viewed here: example toast ...

Error loading the website on Firefox

The background image in my CSS code is not displaying in the browser. I am currently working in the CodeIgniter framework and using Firefox. #star ul.star { list-style:none; margin:0; padding: 0; width:85px; height:20px; left: 10px; top:4px; pos ...