How to Align Image in HTML?
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 :
Middle Alignment (vertically center)
Set the vertical-align as middle.
Output :
Top Alignment
Set the vertical-align as text-top.
Output :
Bottom Alignment
Set the vertical-align as text-bottom.
Output :
Left Alignment
Set the text-align as left.
Output :
Right Alignment
Set the float as right.
Output :
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 Values | Description |
---|---|
left | Its purpose is to align the image to the left. |
right | Its 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 :
Right Alignment
We use the attribute value right to align the image to the right.
Output :
Browser Support
Browser | Version |
---|---|
Google Chrome | 38 and higher |
Firefox | 38 and higher |
Safari | 9.1 and higher |
Microsoft Edge | 13 and higher |
Internet Explorer | 10 |
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.