Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center image in HTML

Understanding the Basics

Before we delve into the subject of centering images in HTML, let's start by understanding a few basic concepts. HTML (Hyper Text Markup Language) is the backbone of any webpage. It is used to structure content on the web. An HTML document contains a series of elements, which define the different parts of a webpage.

An image is one such element in HTML, typically defined by the <img> tag. The source of the image, which is the URL where the image is located, is specified in the src attribute. For instance, <img src="image.jpg"> would display the image named "image.jpg" on your webpage.

Now, let's consider the task of centering this image in the webpage. It's like hanging a painting right in the center of a wall. You want the painting (the image) to be at an equal distance from both sides of the wall (the webpage).

Centering an Image - The CSS Way

While HTML structures the content, CSS (Cascading Style Sheets) is used for styling the webpage. It's like the paint and decor for our analogy of the house. To center an image, we'll primarily be using CSS.

Using the margin Property

One way to center an image is by using the margin property in CSS. This property sets the margin area on all four sides of an element. It's like the space between the edges of the painting and the wall. The value auto adjusts the left and right margins automatically, centering the image.

<img src="image.jpg" style="display: block; margin: auto;">

Here, display: block; is used to display the image as a block-level element (like a standalone piece of furniture in the room). This is necessary because margins can't be applied to inline elements (which is the default display value for images).

Using the text-align Property

Another method is to use the text-align property, which sets the horizontal alignment. If you think of your webpage as a piece of paper, this property aligns your image like text on the paper.

<div style="text-align: center;">
  <img src="image.jpg">
</div>

Here, we're wrapping the image inside a <div> element (think of it as an invisible box) and setting the alignment of the content inside this box to center.

Centering an Image - The Flexbox Way

Flexbox is a CSS layout module that allows easy alignment of elements. It's like having an adjustable wall where you can easily move the painting around.

<div style="display: flex; justify-content: center;">
  <img src="image.jpg">
</div>

Here, display: flex; makes the div a flex container, and justify-content: center; centers the image horizontally inside this container.

Centering an Image - The Grid Way

CSS Grid is another layout system that allows the creation of complex layouts. It's like a grid on the wall where you place the painting.

<div style="display: grid; place-items: center;">
  <img src="image.jpg">
</div>

Here, display: grid; makes the div a grid container, and place-items: center; centers the image both horizontally and vertically inside this container.

Conclusion

Centering an image or any other element in HTML is a common task, and there are multiple methods to achieve it. Each approach has its own advantages and use-cases. The CSS way with the margin or text-align property is straightforward and works well for simple layouts. The Flexbox and Grid methods offer more flexibility and control, which is beneficial for more complex designs.

Remember, practicing these methods is crucial to understanding them better. So, go ahead, experiment with the code, and have fun learning!