Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center an image in HTML

The Mysteries of Centering an Image in HTML

Let's imagine HTML as a construction set. You're the builder and you have bricks (which are your 'elements') to create your masterpiece. One of these elements is the 'image' element. The challenge is to place this image element exactly in the center of your construction (web page). This might seem like a daunting task, but fear not! We're here to demystify this process.

The Basics of Positioning an Image

Before we dive into centering an image, let's understand what positioning means in the first place. When you place an image on your webpage, it's like placing a sticker on a blank canvas. By default, this sticker (your image) sticks to the left-hand side of your canvas (webpage).

The code for putting an image on your webpage looks something like this:

<img src="your-image.jpg">

However, what if you want your sticker (image) to be right in the middle of your canvas (webpage)? That's where centering comes into play.

The Old-School Way: Using the <center> Tag

In the earlier days of HTML, centering an image was as straightforward as wrapping your img element with the center tag.

<center>
  <img src="your-image.jpg">
</center>

This method is like using a magic wand that automatically pulls your image into the center of your webpage. Unfortunately, this magic wand has long been retired and is not recommended for use in modern HTML.

The Modern Way: Using CSS

CSS (Cascading Style Sheets) are like the rules of gravity for your webpage. It dictates how your HTML elements (bricks) are arranged and displayed. For centering an image, we need two CSS properties: display and margin.

The 'display' Property

The display property in CSS determines how an element should be displayed. The block value makes the element behave like a block-level element (akin to creating a separate line just for your element).

When we set the display property of an image to block, it's like giving the image its own stage to perform, separate from other elements.

<img src="your-image.jpg" style="display:block">

The 'margin' Property

The margin property is the space around an element. When we say margin: auto, it means the space on the left and right of the element should be equal. In other words, it's like asking two people to hold the image from both ends and adjust their positions so that the image is exactly in the middle.

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

With these two lines of CSS, your image will be perfectly centered!

Another Method: Using CSS Flexbox

Another modern and flexible way to center an image is by using CSS Flexbox. Think of Flexbox as a superpower that allows you to place your elements anywhere you want with precision.

First, we need to create a container (like a box) for our image and make this container a flex container (giving it our Flexbox superpower).

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

Here, display: flex turns on the flex layout for the container. justify-content: center is the magic command that tells the items (in this case, the image) to move to the center.

And there you have it! You now know how to center an image in HTML using modern techniques. Remember, practice is key in mastering these concepts. Happy coding!