How to Move Image in HTML?


An image can be included in a web page using the HTML img> tag.

The <image> tag has no ending tag, is empty, and simply includes attributes.

A web page can have scrollable text or images from left to right or vice versa, or from top to bottom or vice versa, by using the HTML container tag "marquee" . The new HTML version, HTML 5.1, however, deprecates the use of this tag. We can also use simple html properties for the same.

We will look on more ways to move the image using the CSS properties as well as various HTML Tags, JavaScript and some Block level CSS.

Algorithm

  • HTML file created with an <img> tag for the image to move.

  • Add CSS code to move the image. You can use the "position" and "top" or "left" properties to move the image around.

  • Save the HTML file and open it in a web browser to see the image moved to its new position.

  • You can use the different approaches given below also for moving images in HTML.

Approaches

  • Using the <marquee> tag

  • Moving Images Using CSS and HTML

  • HTML image tags

  • Using Javascript

Using the <marquee> tag

With the help of the marquee tag, moving graphics in HTML is simple. It is used to make scrolling graphics that move vertically from top to bottom or from bottom to top, or horizontally from left to right or right to left. The marquee> tag's default behavior is for the picture to scroll from right to left.

Attributes of <marquee> tag

Direction: Controls the direction in which the photos will scroll. This attribute's value can be either left, right, up, or down.

Behavior: This section describes the text's scrolling behavior. It could be alternate, scroll, or slide, among other values.

Syntax

<marquee>
   <img src="">
</marquee>

Example 1

<!DOCTYPE html>
<html>
<body>
<center>
   <!-- The image has scrolling behavior to left -->
   <marquee behavior="scroll" direction="left">		
      <img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="Mountain landscape">
   </marquee>
   <!-- The image has scrolling behavior to the upper direction. -->
   <marquee behavior="scroll" direction="up">		
      <img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="Cityscape">
   </marquee>
</center>
</body>
</html>

Example 2

<!DOCTYPE html>
<html>
<body>
<center>
   <marquee behavior="alternate" direction="left">		
      <img src= "https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="Image 1">
   </marquee>
   <marquee behavior="alternate" direction="right">		
      <img src= "https://www.tutorialspoint.com/html/images/html.jpg" alt="Image 2">
   </marquee>
</center>
</body>
</html>

Moving Images Using CSS and HTML

Being able to move graphics in HTML is a very useful skill. When the drag-and-drop editors function well, things are simple, but when they don't, it can be challenging to move photos without manually editing the HTML source code. Several outdated techniques are still functional in browsers, although they are no longer advised. HTML5 allows you to alternate between HTML and CSS. This is preferable since it allows for finer-grained control over the placement of every HTML element on a page.

CSS to position and animate images on your webpage. Here's an example code snippet −

<style>
.image-container {
   position: relative;
   width: 300px;
   height: 300px;
}
.image {
   position: absolute;
   top: 0;
   left: 0;
   transition: transform 0.5s ease-in-out;
}
.image:hover {
   transform: translateX(50%);
}
</style>

<div class="image-container">
   <img class="image" src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="Your Image">

By changing the margin values in the style attributes, images can be relocated. To shift images up, down, left, or right, you should add the CSS properties margin-left, margin-right, top, and bottom. You can also employ left or right flotation. Although it might function, the marquee HTML tag is not advised. This code creates a container div for your image with a fixed size, and positions the image absolutely inside the container. On hover, the image will move horizontally by 50% of its own width, giving the effect of sliding to the side.

HTML image tags

Your HTML image tags should always include the image source URL and an alt tag to describe the image in case the browser is unable to load it. Everything else is presentational style information. CSS is used for style in HTML5. CSS can be used inline within the style HTML tag or at the block level. <style> is the style tag.

List of attributes to move images in HTML −

  • Float left

  • Float right

  • Margin-top

  • Margin-bottom

  • Margin-left

  • Margin-right

To move images using CSS properties on HTML components, the style tag is always added after the image source when styling is done inline.

<img src="https://example.com/image1.png" style="float: right;">

To float an image to the right of any HTML element

<img src = "https://example.com/image1.png" style = “float : right ;” 

Image centering techniques

By enclosing the image source code with a center style element, you may centre an image. The centre tag was previously used, but it has since been replaced by the text-align: centre CSS style attribute. To utilize this, first insert your HTML image tag inside the paragraph element, followed by the style tag with the text-align center property.

<p style="text-align: center;"><img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg2"></p>

Moving images down

To downsize photos in HTML, add the style property after the image source location. This way you can set and modify the margin-top attribute in HTML to move images down.

<img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg"style="margin-top: 100px;">

To move images left and right

Use the margin-right property in HTML to left-justify picture positioning. The margin is the empty space, so keep that in mind. It moves the HTML element to the left by increasing the right margin.

<img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg"style="margin-left: 100px;">

An image is moved left to right on the screen when the margin-left attribute is applied to it. Use this to right-align an image.

<img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg"style="margin-left: 100px;">

To move images up

To manage the area behind an HTML element, use the margin-bottom attribute.

<img src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg"style="margin-bottom: 100px;">

Block Level CSS in HTML Style Element to move all Images

Put the class name before the source of the image URL or file location to access the style that has been defined for an image CSS class. The values set between the style tags will be called up by this. Using the style attributes found within the image HTML tag is a simple approach to shift images to where you want them to be for a single image. Set it once in the style element, then call the image class to change the position to repeat the same positioning without applying the style attribute repeatedly.

<img class="image" src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg">

Using Javascript

JavaScript can be used to change the behavior and positioning of your photos. Here is an illustration of some code −

<style>
   .image-container {
      position: relative;
      width: 300px;
      height: 300px;
   }
   .image {
      position: absolute;
      top: 0;
      left: 0;
   }
</style>
<div class="image-container">
   <img class="image" src="https://www.tutorialspoint.com/html/images/html-mini-logo.jpg" alt="Your Image">
</div>

<script>
   var image = document.querySelector('.image');
   var container = document.querySelector('.image-container');

   container.addEventListener('mousemove', function(event) {
      var x = event.clientX - container.offsetLeft;
      var y = event.clientY - container.offsetTop;
      image.style.transform = 'translate(' + x + 'px,' + y + 'px)';
   }); 
</script>

This code listens for mouse movement inside the container div, and updates the position of the image based on the mouse's position relative to the container. This gives the effect of the image following the mouse cursor.

Conclusion

We looked at various methods to move an Image in HTML. From using Javascript to CSS, HTML Tags etc. However there still remain more ways to do this which are yet to be explored. The approaches used in this article are the most significant and useful as well as short methods to achieve our desired result.

Updated on: 24-Jul-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements