73

I am trying to center a image horizontally using css.

I am displaying my image on the screen with the following HTML code:

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

I am croping my image as I only want to display some of the image and I am using the following css:

img.loading 
{
position:absolute;
clip:rect(0px,681px,75px,180px);
}

however I can't work out how to center the image once it has been croped.

Anyone able to help ?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Aaron
  • 3,389
  • 12
  • 35
  • 48

5 Answers5

148

Try this for your CSS:

.center img {        
  display:block;
  margin-left:auto;
  margin-right:auto;
}

and then add to your image to center it:

class="center"
Beta Projects
  • 426
  • 1
  • 8
  • 18
Cynthia
  • 5,273
  • 13
  • 42
  • 71
33

use position absolute and margin like this

img.loading{
  position: absolute;
  left: 50%;
  margin-left: -(half ot the image width)px
}
Zendy
  • 1,664
  • 1
  • 17
  • 24
19

You can use different method:

Method 1:

Its a simple method, use "text-align: center;" for your parent div.

#loading {
  text-align: center;
}
<div id="loading" class="loading-invisible">  
    <img class="loading" src="https://dummyimage.com/200x100/000/fff.png&text=Logo">
</div>

FIDDLE

Method 2:

Please check the code:

#loading img {
  margin: 0 auto;
  display: block;
  float: none; 
  /*width: 200px; if its a large image, it need a width for align center*/
}
<div id="loading" class="loading-invisible">  
    <img class="loading" src="https://dummyimage.com/200x100/000/fff.png&text=Logo">
</div>

FIDDLE

Method 3:

Using "display: flex;"

#loading {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div id="loading" class="loading-invisible">  
    <img class="loading" src="https://dummyimage.com/200x100/000/fff.png&text=Logo">
</div>

FIDDLE

Method 4:

You can use "position: absolute;",

#loading {
        position: relative;
        }
#loading img{
        position: absolute;
        top: 0;
        left: 50%;
        margin-right: -50%;
        transform: translate(-50%, 0%);
        -ms-transform: translate(-50%, 0%);
        -webkit-transform: translate(-50%, 0%);
        -moz-transform: translate(-50%, 0%);
        -o-transform: translate(-50%, 0%);
}
<div id="loading" class="loading-invisible">  
    <img class="loading" src="https://dummyimage.com/200x100/000/fff.png&text=Logo">
</div>

FIDDLE

Hope this will help you.

Ali
  • 1,326
  • 1
  • 17
  • 38
5

Use margin

margin-left:auto;
margin-right:auto;
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
-1

Ways:

  1. Setting Left and Right Margin to “auto”
  2. Using display "Flex"
  3. Using the Text Align property

You can take a look on this blog for more descriptive answer

https://ecedev.com/post/How%20to%20center%20an%20image%20horizontally%20using%20HTML%20and%20CSS

  • 1
    helpfulUser, a link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Yunnosch Nov 12 '22 at 02:35