Table of contents
1.
Introduction
2.
Left Alignment of the Image
2.1.
Example
3.
Right Alignment of the Image
3.1.
Example
4.
Middle Alignment of the Image
4.1.
Example
5.
Top Alignment of the Image
5.1.
Example
6.
Bottom Alignment of the Image
6.1.
Example
7.
Align Image in HTML Using CSS
8.
Align Image in HTML Using the Align Attribute
9.
Browser Support
9.1.
Notes
10.
Frequently Asked Questions
10.1.
How do I center an image horizontally in HTML?
10.2.
What is the difference between float and vertical-align?
10.3.
Can I use these methods with responsive images?
11.
Conclusion
Last Updated: Jan 31, 2025
Medium

How to Align Image in HTML?

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The alignment of images in HTML is an important aspect of web design that allows developers to control how images are positioned within a web page. Proper image alignment can enhance the layout and improve user experience. HTML provides several methods to align images, whether it's centering an image, aligning it to the left or right, or adjusting it within a specific container.

How to Align Image in HTML

In this article, we will discuss various methods to align images in HTML, covering left, right, middle, top, and bottom alignments, with detailed examples.

Left Alignment of the Image

Left alignment ensures that an image is positioned on the left-hand side of its container, with other content flowing to the right. This method is commonly used for creating layouts where text wraps around the image.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Left Align Image</title>
    <style>
        .left-aligned {
            float: left;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <img src="example.jpg" alt="Sample Image" class="left-aligned" width="200">
    <p>This text is aligned to the right of the image. Using the `float` property, you can wrap text around the image. This technique is useful for creating visually appealing content layouts.</p>
</body>
</html>


Output

Output

The image will appear on the left, and the text will flow around it to the right.

Right Alignment of the Image

Right alignment places the image on the right-hand side of the container, with text or other elements flowing to the left. This layout style is often used to emphasize content alongside visuals.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Right Align Image</title>
    <style>
        .right-aligned {
            float: right;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <img src="example.jpg" alt="Sample Image" class="right-aligned" width="200">
    <p>This text is aligned to the left of the image. The `float: right` property ensures that the image stays on the right while the text adjusts to the left, creating a clean layout.</p>
</body>
</html>


Output

Output

The image will be positioned on the right, with the text appearing to its left.

Middle Alignment of the Image

Middle alignment centers the image vertically and/or horizontally within its container. It is ideal for creating balanced layouts.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Center Align Image</title>
    <style>
        .center-aligned {
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <img src="example.jpg" alt="Sample Image" class="center-aligned" width="200">
    <p>The image above is horizontally centered using the `margin: 0 auto;` property. This method works only when the image is a block-level element.</p>
</body>
</html>


Output

Output

The image will be centered horizontally on the page.

Top Alignment of the Image

Top alignment places the image at the top of its container. This is useful for positioning images alongside vertically aligned text or other content.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Top Align Image</title>
    <style>
        .top-aligned {
            vertical-align: top;
        }
    </style>
</head>
<body>
    <p>
        <img src="example.jpg" alt="Sample Image" class="top-aligned" width="100">
        This text is aligned next to the top of the image. Using the `vertical-align: top;` property aligns the top of the image with the top of the text.
    </p>
</body>
</html>


Output

Output

The image's top edge aligns with the top of the surrounding text.

Bottom Alignment of the Image

Bottom alignment ensures that the bottom edge of the image aligns with the bottom of adjacent text or other elements.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bottom Align Image</title>
    <style>
        .bottom-aligned {
            vertical-align: bottom;
        }
    </style>
</head>
<body>
    <p>
        <img src="example.jpg" alt="Sample Image" class="bottom-aligned" width="100">
        This text aligns with the bottom of the image. The `vertical-align: bottom;` property ensures proper alignment of the bottom edges.
    </p>
</body>
</html>


Output

Output

The image’s bottom edge aligns with the bottom of the surrounding text.

Align Image in HTML Using CSS

CSS is a powerful tool for styling web pages, and it's perfect for aligning images. Let's start with a basic example. Imagine you have an image that you want to center on your page. Let’s see how you can do it using CSS.

First, you need to create an HTML file. Open your text editor and type the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Align Image in HTML</title>
    <style>
        .center {
            display: block;
            margin-left: auto;
            margin-right: auto;
            width: 50%;
        }
    </style>
</head>
<body>
    <img src="your-image.jpg" alt="Centered Image" class="center">
</body>
</html>


Output

Output

 

In this example, we use the class attribute to apply CSS styles to the image. The center class sets the display property to block, which allows us to use margin-left and margin-right properties. Setting these margins to auto centers the image horizontally. The width property ensures the image doesn't stretch too wide.

Align Image in HTML Using the Align Attribute

While CSS is the preferred method for aligning images, HTML also provides an align attribute that can be used directly within the <img> tag. This method is simpler but less flexible compared to CSS.

For example: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Align Image with HTML Attribute</title>
</head>
<body>
    <img src="your-image.jpg" alt="Left Aligned Image" align="left">
    <p>This is some text next to the left-aligned image.</p>
</body>
</html>


Output

Output

 

In this example, the align="left" attribute aligns the image to the left side of the page. The text following the image will flow around it, creating a nice layout. You can also use align="right" to align the image to the right side.

Browser Support

The properties and techniques discussed in this article are widely supported across all major browsers, including:

  • Google Chrome
     
  • Mozilla Firefox
     
  • Microsoft Edge
     
  • Safari
     
  • Opera

Notes

  • The float property is supported in all modern and legacy browsers.
     
  • The margin and vertical-align properties are also widely compatible.

Frequently Asked Questions

How do I center an image horizontally in HTML?

You can center an image horizontally using CSS by applying margin: 0 auto; and ensuring the image is a block-level element.

What is the difference between float and vertical-align?

The float property positions elements to the left or right with other content flowing around them, while vertical-align aligns elements vertically relative to their container.

Can I use these methods with responsive images?

Yes, these alignment methods work well with responsive images. Use CSS properties like max-width: 100%; and height: auto; to ensure responsiveness.

Conclusion

In this article, we discussed how to align images in HTML using various methods, including left, right, center, top, and bottom alignments. We learned techniques such as using float, text-align, and flexbox properties to achieve the desired positioning. Understanding these methods will help you create better page layouts and enhance user experience by ensuring that images are properly aligned within your web design.

Live masterclass