Center an image with CSS

To center an image, use the margin-left, margin-right and block CSS properties. You can try to run the following code to center an image

Example

<!DOCTYPE html>
<html>
  <head>
   <style>
     img {
      border: 2px solid orange;
      border-radius: 3px;
      padding: 7px;
     }
     img {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 50%;
     }
   </style>
  </head>
  <body>
   <img src = "https://www.tutorialspoint.com/videotutorials/images/coding_ground_home.jpg" alt="Online Compiler" width="300" height="300">
  </body>
</html>
Updated on: 2020-06-24T07:24:59+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements