How TO – Center Images


Previous


Next

Learn how to center an image with CSS.





Center an Image

To center an image, set left and right margin to auto, and make it into a block element.

Paris

How To Center Images

Step 1) Add HTML:

Example

<img src=”paris.jpg” alt=”Paris” class=”center”>

Step 2) Add CSS:

To center an image, set left and right margin to auto and make it into a block element:

 

Example

<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>

<h2>Center an Image</h2>
<p>To center an image, set left and right margin to auto, and make it into a block element.</p>

<img src=”img_paris.jpg” alt=”Paris” style=”width:50%;”>

</body>
</html>


Previous


Next

Scroll to Top