How to Move Image in HTML?

Moving images in HTML can be achieved through various methods, from traditional HTML tags to modern CSS properties and JavaScript. An image is added to a web page using the HTML <img> tag, which is a self-closing element that simply includes attributes.

While the HTML <marquee> tag was previously used to create scrolling images, it is now deprecated in HTML5. Modern web development favors CSS properties and JavaScript for more precise control over image positioning and movement.

Syntax

The basic syntax for adding an image in HTML is

<img src="image-url" alt="description">

To move images, you can apply CSS positioning properties

.moving-image {
   position: relative;
   left: 50px;
   top: 20px;
}

Using the Marquee Tag (Deprecated)

The <marquee> tag creates scrolling content that moves horizontally or vertically. While deprecated, it still works in most browsers. The default behavior scrolls content from right to left.

Key Attributes

  • direction Controls scrolling direction (left, right, up, down)

  • behavior Defines scrolling behavior (scroll, slide, alternate)

Example Basic Marquee Movement

<!DOCTYPE html>
<html>
<head>
   <title>Marquee Image Movement</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Scrolling Left</h3>
   <marquee behavior="scroll" direction="left">
      <img src="/html/images/html-mini-logo.jpg" alt="HTML Logo" width="100">
   </marquee>
   
   <h3>Alternate Movement</h3>
   <marquee behavior="alternate" direction="left">
      <img src="/html/images/html-mini-logo.jpg" alt="HTML Logo" width="100">
   </marquee>
</body>
</html>

The first image scrolls continuously from right to left, while the second bounces back and forth horizontally.

Moving Images Using CSS Properties

CSS provides precise control over image positioning using properties like position, margin, transform, and float. This method is preferred over deprecated HTML tags.

Example CSS Positioning and Animation

<!DOCTYPE html>
<html>
<head>
   <title>CSS Image Movement</title>
   <style>
      .container {
         position: relative;
         width: 400px;
         height: 200px;
         border: 2px solid #ccc;
         margin: 20px 0;
      }
      .moving-image {
         position: absolute;
         top: 50px;
         left: 0;
         transition: transform 2s ease-in-out;
      }
      .moving-image:hover {
         transform: translateX(250px);
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Hover to Move Image</h3>
   <div class="container">
      <img class="moving-image" src="/html/images/html-mini-logo.jpg" alt="Moving Logo" width="80">
   </div>
</body>
</html>

When you hover over the image, it smoothly slides 250 pixels to the right using CSS transform and transition properties.

Example Using Margins for Positioning

<!DOCTYPE html>
<html>
<head>
   <title>Image Positioning with Margins</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Centered Image</h3>
   <p style="text-align: center;">
      <img src="/html/images/html-mini-logo.jpg" alt="Centered Logo" width="100">
   </p>
   
   <h3>Float Right</h3>
   <img src="/html/images/html-mini-logo.jpg" alt="Right Logo" width="80" style="float: right; margin-left: 10px;">
   <p>This text wraps around the image that is floated to the right. The image stays on the right side while text flows around it naturally.</p>
   
   <div style="clear: both;"></div>
   
   <h3>Margin Positioning</h3>
   <img src="/html/images/html-mini-logo.jpg" alt="Positioned Logo" width="80" style="margin-top: 50px; margin-left: 100px;">
</body>
</html>

This example shows centering with text-align, floating right with text wrap, and absolute positioning using margins.

Moving Images with JavaScript

JavaScript enables dynamic image movement based on user interactions or automated animations. This provides the most flexibility for complex movement patterns.

Example Mouse-Following Image

<!DOCTYPE html>
<html>
<head>
   <title>JavaScript Image Movement</title>
   <style>
      .js-container {
         position: relative;
         width: 400px;
         height: 300px;
         border: 2px solid #333;
         margin: 20px 0;
         cursor: move;
      }
      .js-image {
         position: absolute;
         transition: all 0.3s ease;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Move Mouse Inside Container</h3>
   <div class="js-container" id="container">
      <img class="js-image" id="movingImage" src="/html/images/html-mini-logo.jpg" alt="Interactive Logo" width="60">
   </div>
   
   <script>
      const container = document.getElementById('container');
      const image = document.getElementById('movingImage');
      
      container.addEventListener('mousemove', function(event) {
         const rect = container.getBoundingClientRect();
         const x = event.clientX - rect.left - 30; // Center image on cursor
         const y = event.clientY - rect.top - 30;
         
         // Keep image within container bounds
         const maxX = container.offsetWidth - 60;
         const maxY = container.offsetHeight - 60;
         
         const finalX = Math.max(0, Math.min(x, maxX));
         const finalY = Math.max(0, Math.min(y, maxY));
         
         image.style.left = finalX + 'px';
         image.style.top = finalY + 'px';
      });
   </script>
</body>
</html>

The image follows the mouse cursor within the container boundaries, creating an interactive movement effect.

CSS Properties for Image Movement

Following are the key CSS properties used for positioning and moving images

Property Description Example Usage
position Defines positioning method (static, relative, absolute, fixed) position: absolute;
top, left, right, bottom Sets position offset from respective edges top: 50px; left: 100px;
margin Controls outer spacing around element margin-left: 50px;
transform Applies 2D/3D transformations transform: translateX(100px);
float Floats element left or right float: right;

Example Combining Multiple Techniques

<!DOCTYPE html>
<html>
<head>
   <title>Combined Movement Techniques</title>
   <style>
      .demo-section {
         margin: 30px 0;
         padding: 20px;
         border: 1px solid #ddd;
      }
      .rotate-move {
         position: relative;
         left: 50px;
         transform: rotate(15deg) translateY(20px);
         transition: all 1s ease;
      }
      .rotate-move:hover {
         transform: rotate(-15deg) translateY(-10px) translateX(100px);
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <div class="demo-section">
      <h4>Rotate and Translate on Hover</h4>
      <img class="rotate-move" src="/html/images/html-mini-logo.jpg" alt="Transform Logo" width="80">
   </div>
</body>
</html>

This example combines rotation, translation, and smooth transitions to create complex movement effects on hover.

Image Movement Methods Comparison Marquee (Deprecated) ? Simple to use ? Automatic scrolling ? Limited control ? Not recommended ? Poor accessibility CSS Properties ? Precise control ? Smooth animations ? Good performance ? Modern approach ? Accessible JavaScript ? Dynamic movement ? User interactions ? Complex animations ? Full flexibility ? Requires scripting

Conclusion

Moving images in HTML can be accomplished through multiple approaches, from the deprecated

Updated on: 2026-03-16T21:38:54+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements