https://blog.hubspot.com/website/center-an-image-in-html#how-to-horizontally-center-an-image-in-css -=-=- Method 1. Using the Text-Align Property To center an image horizontally, I can use the CSS text-align property. Since this property only works on block-level elements and not inline elements, I’ll need to wrap the image in a block element. how to center an image in css, text align Here's how: I start by opening up my HTML file. I then locate the image on the page, identifiable by its tag. I wrap the image in a div. From there, I can add a style declaration to the opening tag of your div, i.e.,
. Inside the quote marks, I set the text-align property to center, i.e., text-align: center; It’s simple, but it's important to note that it only works if the image is smaller than my viewport. Here’s the HTML with inline CSS and the result: Try it yourself! The code module above is editable. Toggle between the HTML and CSS tabs, edit the code, and click rerun in the bottom right-hand corner. Pro tip: As I edit my code, I take advantage of the live preview option throughout the process to observe the changes as I apply them. Live preview can be used in most web development IDEs, such as Visual Studio Code. This feature lets me see real-time updates from my code in my browser. -=-=- HTML

Centering an Image with the Text Align Property

In this example, the image is wrapped in a block element (a div, specifically). The div is styled with the text align property to center the image below.

This is the simplest but most limited method: it only works if the image is smaller than the viewport.

-=-=- CSS body { margin: auto; width: 640px; padding: 50px; font-family: 'Arial', sans-serif; color: #33475b; } img{ padding: 10px; }