Home  >  Article  >  Web Front-end  >  How to Center a Cropped Image Horizontally Using CSS?

How to Center a Cropped Image Horizontally Using CSS?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 02:57:03429browse

How to Center a Cropped Image Horizontally Using CSS?

How to Horizontally Align an Image Using CSS

When trying to center an image horizontally, CSS provides an effective solution. In this question, the user displays an image using HTML and crops it using CSS. However, they encounter difficulty in centering the cropped image.

To resolve this issue, the following CSS rules can be applied:

<code class="css">.center img {        
  display:block;
  margin-left:auto;
  margin-right:auto;
}</code>

By adding the center class to the image element, it can be centered horizontally:

<code class="html"><div id="loading" class="loading-invisible">  
    <img class="loading center" src="logo.png">
</div></code>

These CSS rules achieve the desired effect by setting the image to display as a block element and automatically adjusting the left and right margins. This results in the image being centered within its container.

The above is the detailed content of How to Center a Cropped Image Horizontally Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn