How to Align Image in HTML?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

Image alignment is a technique for repositioning images on our web pages (top, bottom, right, left, and centre). We could align the image using the <img> align attribute or using CSS. In this article, we are going to see both methods of image alignment.

Align Image in HTML Using CSS

Center Alignment (horizontally center)

Set the left and right margins to auto and make the image into a block element to center it.

Output :

Center Alignment

Middle Alignment (vertically center)

Set the vertical-align as middle.

Output :

Middle Alignment

Top Alignment

Set the vertical-align as text-top.

Output :

Top Alignment

Bottom Alignment

Set the vertical-align as text-bottom.

Output :

Bottom Alignment

Left Alignment

Set the text-align as left.

Output :

Left Alignment

Right Alignment

Set the float as right.

Output :

Right Alignment

Align Image in HTML Using the Align Attribute

The align attribute indicates how an image should be aligned concerning its container. To align the image, we use the align attribute. <img> element is an inline element. However, the align attribute has been deprecated since HTML4 as HTML is not meant for formatting. So you should always use CSS for image alignment.

Syntax :

Attribute ValuesDescription
leftIts purpose is to align the image to the left.
rightIts purpose is to align the image to the right.

Left Alignment

We Use the attribute value left to align the image to the left.

Output :

align image in html Using Left Alignment

Right Alignment

We use the attribute value right to align the image to the right.

Output :

align image in html Using right Alignment

Browser Support

BrowserVersion
Google Chrome38 and higher
Firefox38 and higher
Safari9.1 and higher
Microsoft Edge13 and higher
Internet Explorer10

Conclusion

  • We have looked at many examples of image alignment using CSS and the align attribute.
  • align attribute is not supported since HTML4, so we should always use CSS for image alignment.