[html] Center image horizontally within a div

This is probably a stupid question but since the usual ways of center aligning an image are not working I thought I would ask. How can I center align (horizontally) an image inside its container div?

Here's the HTML and CSS. I've also included the CSS for the other elements of the thumbnail. It runs in descending order so the highest element is the container of everything and the lowest is inside everything.

_x000D_
_x000D_
#thumbnailwrapper {_x000D_
      color: #2A2A2A;_x000D_
      margin-right: 5px;_x000D_
      border-radius: 0.2em;_x000D_
      margin-bottom: 5px;_x000D_
      background-color: #E9F7FE;_x000D_
      padding: 5px;_x000D_
      border: thin solid #DADADA;_x000D_
      font-size: 15px_x000D_
}_x000D_
    _x000D_
#artiststhumbnail {_x000D_
      width: 120px;_x000D_
      height: 108px;_x000D_
      overflow: hidden;_x000D_
      border: thin solid #DADADA;_x000D_
      background-color: white;_x000D_
}_x000D_
    _x000D_
#artiststhumbnail:hover {_x000D_
      left: 50px_x000D_
}
_x000D_
<!--link here-->_x000D_
_x000D_
<a href="NotByDesign">_x000D_
  <div id="thumbnailwrapper">_x000D_
_x000D_
    <a href="NotByDesign">_x000D_
_x000D_
      <!--name here-->_x000D_
      <b>Not By Design</b>_x000D_
      <br>_x000D_
_x000D_
      <div id="artiststhumbnail">_x000D_
_x000D_
        <a href="NotByDesign">_x000D_
_x000D_
          <!--image here-->_x000D_
          <img src="../files/noprofile.jpg" height="100%" alt="Not By Design" border="1" />_x000D_
        </a>_x000D_
      </div>_x000D_
_x000D_
      <div id="genre">Punk</div>_x000D_
_x000D_
  </div>
_x000D_
_x000D_
_x000D_

Okay, I have added the markup without the PHP in so should be easier to see. Neither solution seems to work in practice. The text at top and bottom cannot be centered and the image should be centered within its container div. The container has overflow hidden so I want to see the center of the image as that's normally where the focus is.

This question is related to html css

The answer is


I have tried a few ways. But this way works perfectly for me

<img src="~/images/btn.png" class="img-responsive" id="hide" style="display: block; margin-left: auto; margin-right: auto;" />

Put the picture inside a newDiv. Make the width of the containing div the same as the image. Apply margin: 0 auto; to the newDiv. That should center the div within the container.


##Both Vertically and Horizontally center of the Page
.box{
    width: 300px;
    height: 300px;
    background-color: #232532;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

This also would do it

#imagewrapper {
    text-align:center;
}

#imagewrapper img {
    display:inline-block;
    margin:0 5px;
}

Put an equal pixel padding for left and right:

<div id="artiststhumbnail" style="padding-left:ypx;padding-right:ypx">

I am going to go out on a limb and say that the following is what you are after.

Note, the following I believe was accidentally omitted in the question (see comment):

<div id="thumbnailwrapper"> <!-- <<< This opening element -->
    <div id="artiststhumbnail">
...

So what you need is:

#artiststhumbnail {
    width:120px;
    height:108px;
    margin: 0 auto; /* <<< This line here. */
    ...
}

http://jsfiddle.net/userdude/XStjX/3/


I just found this solution below on the W3 CSS page and it answered my problem.

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

Source: http://www.w3.org/Style/Examples/007/center.en.html


_x000D_
_x000D_
.document {
  align-items: center;
  background-color: hsl(229, 57%, 11%);
  border-radius: 5px;
  display: flex;
  height: 40px;
  width: 40px;
}

.document img {
  display: block;
  margin: auto;
}
_x000D_
<div class="document">
  <img src="./images/icon-document.svg" alt="icon-document" />
</div>
_x000D_
_x000D_
_x000D_


Center a image in a div

_x000D_
_x000D_
/* standar */_x000D_
div, .flexbox-div {_x000D_
  position: relative;_x000D_
  width: 100%;_x000D_
  height: 100px;_x000D_
  margin: 10px;_x000D_
  background-color: grey;  _x000D_
}_x000D_
_x000D_
img {_x000D_
  border: 3px solid red;_x000D_
  width: 75px;_x000D_
  height: 75px;_x000D_
}_x000D_
/* || standar */_x000D_
_x000D_
_x000D_
/* transform */_x000D_
.transform {_x000D_
  position: absolute;_x000D_
  top: 50%;_x000D_
  left: 50%;_x000D_
  transform: translate(-50%, -50%);_x000D_
  -ms-transform: translate(-50%, -50%); /* IE 9 */_x000D_
  -webkit-transform: translate(-50%, -50%); /* Chrome, Safari, Opera */ _x000D_
}_x000D_
/* || transform */_x000D_
_x000D_
_x000D_
/* flexbox margin */_x000D_
.flexbox-div {_x000D_
  display: -webkit-flex;_x000D_
  display: flex;_x000D_
  background-color: lightgrey; _x000D_
}_x000D_
_x000D_
.margin-img {_x000D_
  margin: auto;_x000D_
}_x000D_
/* || flexbox margin */_x000D_
_x000D_
_x000D_
/* flexbox justify align */_x000D_
.flexbox-justify {_x000D_
  justify-content: center;_x000D_
}_x000D_
_x000D_
.align-item {_x000D_
  align-self: center;_x000D_
}_x000D_
/* || flexbox justify align */
_x000D_
<h4>Using transform </h4>  _x000D_
<div>_x000D_
  <img class="transform" src="http://placeholders.org/250/000/fff" alt="Not By Design" border="1" />_x000D_
</div>_x000D_
_x000D_
<h4>Using flexbox margin</h4>  _x000D_
<div class="flexbox-div">_x000D_
  <img class="margin-img" src="http://placeholders.org/250/000/fff" alt="Not By Design" border="1" />_x000D_
</div>_x000D_
_x000D_
<h4>Using flexbox justify align</h4>  _x000D_
<div class="flexbox-div flexbox-justify">_x000D_
  <img class="align-item" src="http://placeholders.org/250/000/fff" alt="Not By Design" border="1" />_x000D_
</div>
_x000D_
_x000D_
_x000D_


This is what I ended up doing:

<div style="height: 600px">
   <img src="assets/zzzzz.png" alt="Error" style="max-width: 100%; 
        max-height: 100%; display:block; margin:auto;" />
</div>

Which will limit the image height to 600px and will horizontally-center (or resize down if the parent width is smaller) to the parent container, maintaining proportions.


A responsive way to center an image can be like this:

.center {
    display: block;
    margin: auto;
    max-width: 100%;
    max-height: 100%;
}

you can align your content using flex box with minimum code

HTML

<div class="image-container">
<img src="https://image.freepik.com/free-vector/modern-abstract-background_1048-1003.jpg" width="100px"> 
</div>

CSS

.image-container{
  width:100%;
  background:green;
  display:flex;

_x000D_
_x000D_
.image-container{_x000D_
  width:100%;_x000D_
  background:green;_x000D_
  display:flex;_x000D_
  justify-content: center;_x000D_
  align-items:center;_x000D_
}
_x000D_
<div class="image-container">_x000D_
<img src="https://image.freepik.com/free-vector/modern-abstract-background_1048-1003.jpg" width="100px"> _x000D_
</div>
_x000D_
_x000D_
_x000D_

js fiddle link https://jsfiddle.net/7un6ku2m/


yeah, the code like this work fine

<div>
 <img>
</div>

but just to remind u, the style for image

object-fit : *depend on u*

so the final code be like Example

<div style="border: 1px solid red;">
    <img
      src="./assets/images/truck-toy.jpg"
      alt=""
      srcset=""
      style="
       border-radius: 50%;
       height: 7.5rem;
       width: 7.5rem;
       object-fit: contain;"
    />
</div>

Final result


CSS flexbox can do it with justify-content: center on the image parent element. To preserve the aspect ratio of the image, add align-self: flex-start; to it.

HTML

<div class="image-container">
  <img src="http://placehold.it/100x100" />
</div>

CSS

.image-container {
  display: flex;
  justify-content: center;
}

Output:

_x000D_
_x000D_
body {_x000D_
  background: lightgray;_x000D_
}_x000D_
.image-container {_x000D_
  width: 200px;_x000D_
  display: flex;_x000D_
  justify-content: center;_x000D_
  margin: 10px;_x000D_
  padding: 10px;_x000D_
  /* Material design properties */_x000D_
  background: #fff;_x000D_
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);_x000D_
}_x000D_
.image-2 {_x000D_
  width: 500px;_x000D_
  align-self: flex-start;  /* to preserve image aspect ratio */_x000D_
}_x000D_
.image-3 {_x000D_
  width: 300px;_x000D_
  align-self: flex-start;  /* to preserve image aspect ratio */_x000D_
}
_x000D_
<div class="image-container">_x000D_
  <img src="http://placehold.it/100x100" />_x000D_
</div>_x000D_
_x000D_
<div class="image-container image-2">_x000D_
  <img src="http://placehold.it/100x100/333" />_x000D_
</div>_x000D_
_x000D_
<div class="image-container image-3">_x000D_
  <img src="http://placehold.it/100x100/666" />_x000D_
</div>
_x000D_
_x000D_
_x000D_


If you have to do this inline (such as when using an input box),
here is a quick hack that worked for me: surround your (image link in this case)
in a div with style="text-align:center"

<div style="text-align:center">

<a title="Example Image: Google Logo" href="https://www.google.com/" 
target="_blank" rel="noopener"><img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" alt="Google Logo. Click to visit Google.com" border="0" data-recalc-dims="1" /></a>

<h6><strong>This text will also be centered </strong></h6>

</div> /* ends centering style */

Style.css

img#center-img{

 display: block;
 margin: auto;
}

Html

<html>
  <body>
    <div>
      <img src='pic.png' id='center-img'>
    </div>
  </body>
</html>

Use positioning. The following worked for me... (Horizontally and Vertically Centered)

With zoom to the center of the image (image fills the div):

div{
    display:block;
    overflow:hidden;
    width: 70px; 
    height: 70px;  
    position: relative;
}
div img{
    min-width: 70px; 
    min-height: 70px;
    max-width: 250%; 
    max-height: 250%;    
    top: -50%;
    left: -50%;
    bottom: -50%;
    right: -50%;
    position: absolute;
}

Without zoom to the center of the image (image does not fill the div):

   div{
        display:block;
        overflow:hidden;
        width: 100px; 
        height: 100px;  
        position: relative;
    }
    div img{
        width: 70px; 
        height: 70px; 
        top: 50%;
        left: 50%;
        bottom: 50%;
        right: 50%;
        position: absolute;
    }

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">


      <style>
      body{
  /*-------------------important for fluid images---\/--*/
  overflow-x: hidden; /* some browsers shows it for mysterious reasons to me*/
  overflow-y: scroll;
  margin-left:0px;
  margin-top:0px;
  /*-------------------important for fluid images---/\--*/
      }
      .thirddiv{
      float:left;
      width:100vw;
      height:100vh;
      margin:0px;
      background:olive;
      }
      .thirdclassclassone{
      float:left;   /*important*/
      background:grey;
      width:80vw;
      height:80vh; /*match with img height bellow*/
      margin-left:10vw; /* 100vw minus "width"/2    */
      margin-right:10vw; /* 100vw minus "width"/2   */
      margin-top:10vh;
      }
      .thirdclassclassone img{
      position:relative; /*important*/
     display: block;  /*important*/
    margin-left: auto;  /*very important*/
    margin-right: auto;  /*very important*/
    height:80vh; /*match with parent div above*/

    /*--------------------------------
    margin-top:5vh;
    margin-bottom:5vh;
    ---------------------------------*/
    /*---------------------set margins to match total  height of parent di----------------------------------------*/
      }
    </style>           
</head>  
<body>
    <div class="thirddiv">
       <div class="thirdclassclassone">
       <img src="ireland.png">
    </div>      
</body>
</html>

Add this to your CSS:

#artiststhumbnail a img {
   display: block;
   margin-left: auto;
   margin-right: auto;
}

Just referencing a child element which in that case is the image.


To center an image horizontally, this works:

<p style="text-align:center"><img src=""></p>

The best thing I have found (that seems to work in all browsers) for centering an image, or any element, horizontally is to create a CSS class and include the following parameters:

CSS

.center {
    position: relative;          /* where the next element will be automatically positioned */
    display: inline-block;       /* causes element width to shrink to fit content */
    left: 50%;                   /* moves left side of image/element to center of parent element */
    transform: translate(-50%);  /* centers image/element on "left: 50%" position */
}

You can then apply the CSS class you created to your tag as follows:

HTML

<img class="center" src="image.jpg" />

You can also inline the CSS in your element(s) by doing the following:

<img style="position: relative; display: inline-block; left: 50%; transform: translate(-50%);" src ="image.jpg" />

...but I wouldn't recommend writing CSS inline because then you have to make multiple changes in all your tags using your centering CSS code if you ever want to change the style.