How can I center align (horizontally) an image inside its container div?

Here's the HTML and CSS. I have 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.

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

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.

Jesse Nickles's user avatar

asked Jun 12, 2012 at 0:40

Jacob Windsor's user avatar

10

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

Here's my solution in: http://jsfiddle.net/marvo/3k3CC/2/

user229044's user avatar

user229044

241k41 gold badges348 silver badges350 bronze badges

answered Jun 12, 2012 at 0:46

Marvo's user avatar

9 Comments

I don't think this is a good idea, and also I believe there's some caveats like margin: auto is dependent on the containing element having a designated width value.

2012-06-12T00:50:06.467Z+00:00

I'm still improving my CSS skills, so thanks for the interesting study point. But in this case, he is using a fixed width on the container.

2012-06-12T00:55:52.56Z+00:00

What exactly does designated width mean in this context? Definitely seems like useful knowledge to have.

2012-06-12T01:09:18.77Z+00:00

See this fiddle I made; I'm not sure what to think. What you're suggesting won't do anything, and the img I believe would need a width defined anyways to have any effect with auto.

2012-06-12T01:20:19.643Z+00:00

You didn't actually implement the solution I presented. Second, the enclosing div has a width of 120px, which is about the same as the width of the image, making it hard to see if it's actually centering the image. Here's my solution: jsfiddle.net/marvo/3k3CC/2

2012-06-12T02:38:11.08Z+00:00

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:

answered Dec 10, 2015 at 13:04

m4n0's user avatar

3 Comments

Excellent solution when the browser support this, which mine does. Can also use align-items to center vertically. The various margin solutions don't work for me because the element to be centered doesn't have width and height attributes.

2017-11-16T22:59:57.487Z+00:00

In my case, the div has a given size (e.g. 50px) and this will stretch the image to be at 50px.

2018-08-07T21:39:20.13Z+00:00

@Max In that case, you can apply align-self to the image element like this jsfiddle.net/3fgvkurd

2018-08-08T09:38:43.777Z+00:00

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

TylerH's user avatar

TylerH

21.3k85 gold badges84 silver badges122 bronze badges

answered Aug 29, 2014 at 13:35

mk.hd's user avatar

2 Comments

I'm using max-width: 100%; max-height: 100%; to resize the image if the screen smaller than image, but in this case it's not centered. Any suggestions on how to center with those two attributes applied

2016-06-08T00:07:39.813Z+00:00

This does not work, if the image does not fit inside the div

2024-10-24T17:11:19.393Z+00:00

This also would do it

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

Kostas Minaidis's user avatar

answered Oct 6, 2014 at 11:13

Mehmet Yaden's user avatar

7 Comments

Did you mean text-align:center; and margin:0 5px; ? I added a ;

2017-04-22T23:25:36.98Z+00:00

@jagb semicolons are not required for the last property in a selector block.

2018-01-20T23:11:22.617Z+00:00

@TylerH Thats true, semicolons are not required for the last property in a selector block but it's always saver to use the semicolons.. What if I write a CSS file, another developer edits my file later, they add some code (after the line with the missing semicolon, the line I wrote before in that CSS file) and their height, width or other declaration isn't working (or worse yet, they doesn't notice that it's not working). I would say It's safer to leave the semicolons in. This is why I use the semicolons and never leave the semicolons out.

2018-01-23T02:40:37.713Z+00:00

@jagb If another developer edits your file later, they will add in semicolons where necessary, or it will be their problem for writing code that has errors. The response to your initial comment is still: "semicolons on the last line in CSS are a style choice".

2018-01-23T15:52:47.383Z+00:00

I agree with jagb. This site should promote good style choices. A good style does not leave code behind that is easily broken. Putting the semicolon on every line costs you maybe 1/10th of a second, debugging to find a single missing semicolon can cost you hours. Thats why the big tech companies insist on it: Google HTML/CSS Style Guide - Declaration Stops

2019-11-29T08:46:26.053Z+00:00

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.

answered Jan 20, 2018 at 22:59

Kevin's user avatar

3 Comments

did the job, but for browser compatibility, you will need to use the other forms of transform.

2018-01-22T12:44:04.063Z+00:00

I also found this link to be helpful. It shows a similar transform: translate(-50%) trick you show here, but uses it in conjunction with margin-left: 50%, rather than left:50%. It also contrasts solutions for when the item has` relative` vs absolute positioning. This is mostly for my future reference, so I can refer here to your Answer, and a solution I previously used, in 1 (upvoted) location. :-) css-tricks.com/centering-css-complete-guide

2020-06-30T11:25:43.217Z+00:00

Oh, here's another tidbit I found regarding positioning on the element & container [at w3.org] (w3.org/Style/Examples/007/center.en.html) for using the transform: translate trick: Essential rules are: 1) Make the container relatively positioned, which declares it to be a container for absolutely positioned elements. 2) Make the element itself absolutely positioned. 3) Place it halfway down the container with 'top: 50%' (or horizontally with left:50%). 4) Use a translation to move the element up by half its own height (or horizontally by half its width).

2020-06-30T11:40:52.997Z+00:00

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.

HoldOffHunger's user avatar

HoldOffHunger

21.4k11 gold badges123 silver badges147 bronze badges

answered Apr 28, 2015 at 8:40

Cherno's user avatar

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/

answered Jun 12, 2012 at 1:37

Jared Farrish's user avatar

2 Comments

Hmm... I'll bet that works. I centered the IMAGE in my solution, but I can see how question could be interpreted as centering the div-enclosing-the-image inside another div. Either way, same solution.

2012-06-12T02:42:57.71Z+00:00

Neither solution is working in practice. I had tried both solutions before it just won't work. Please refer to the edit in the question

2012-06-12T13:27:25.77Z+00:00

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 { display: flex; justify-content: center; align-items: center; } div img { object-fit: contain; } <div style="border: 1px solid red;"> <img src="https://img.joomcdn.net/9dd32cbfa0cdd7f48ca094972ca47727cd3cd82c_original.jpeg" alt="" srcset="" style=" border-radius: 50%; height: 7.5rem; width: 7.5rem; object-fit: contain;" /> </div>

Final result

Teocci's user avatar

Teocci

9,4051 gold badge71 silver badges56 bronze badges

answered May 21, 2020 at 9:56

Thomas Bui's user avatar

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.

answered Jan 17, 2016 at 20:32

karlzdev's user avatar

1 Comment

this is good for when you still want access to the top margin, .. better than margin 0 auto

2017-03-27T23:00:22.95Z+00:00

Put an equal pixel padding for left and right:

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

HoldOffHunger's user avatar

HoldOffHunger

21.4k11 gold badges123 silver badges147 bronze badges

answered Mar 25, 2015 at 3:37

V-SHY's user avatar

To center an image horizontally, this works:

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

HoldOffHunger's user avatar

HoldOffHunger

21.4k11 gold badges123 silver badges147 bronze badges

answered Jun 6, 2017 at 8:20

kalariya parikshith's user avatar

1 Comment

Yes, but if you plan to display the image as block to prevent that boring white space under it, this wont work anymore...

2018-03-17T15:27:04.54Z+00:00

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.

prasun's user avatar

prasun

7,3999 gold badges47 silver badges62 bronze badges

answered Feb 22, 2016 at 5:21

Setu's user avatar

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; }

answered Mar 24, 2016 at 8:42

Center a image in a div

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

answered Sep 25, 2017 at 5:33

antelove's user avatar

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;" />

answered Jan 20, 2019 at 2:44

Ifwat's user avatar

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

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

answered Jun 2, 2020 at 22:09

Ali Safari's user avatar

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

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

answered Dec 19, 2016 at 12:57

Santosh Khalse's user avatar

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 */

answered Jul 11, 2017 at 20:21

SherylHohman's user avatar

.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; } <div class="document"> <img src="./images/icon-document.svg" alt="icon-document" /> </div>

answered Jun 21, 2020 at 5:02

Nick GM's user avatar

1 Comment

Welcome to stackoverflow! Please add some explanation to your answer.

2020-06-21T05:37:00.007Z+00:00

on the img put

left:0; right:0;

answered Jul 13, 2023 at 19:57

wesmartin17's user avatar

5 Comments

This is going to center the image horizontally?

2023-08-03T12:16:06.79Z+00:00

Yes, it will center the image horizontally

2023-08-04T15:18:37.097Z+00:00

Then why doesn't it work in this example: jsfiddle.net/pk0L34yv ? If you can show that this works, please put a runnable code snippet in your answer.

2023-08-04T15:43:28.707Z+00:00

Ah my bad it also needs display:block and margin:auto jsfiddle.net/no673wmh

2023-08-04T15:46:07.17Z+00:00

You also added display:block. The left and right aren't related to why that works now.

2023-08-04T15:49:38.39Z+00:00

<!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>

answered Apr 17, 2016 at 15:59

KES NORKUS's user avatar

##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; }

answered Feb 15, 2018 at 11:49

Siva Kaliyamoorthy's user avatar

3 Comments

Welcome to stackoverflow! Please add some explanation to your answer.

2018-02-15T13:04:54.57Z+00:00

If we want to fix particular <div> or <section> Both Vertically and Horizontally center of the Page for all devices, simply you can use this style code!!!!

2018-03-15T10:32:56.397Z+00:00

Looks like this answer belongs to a different question :)

2018-06-29T19:32:49.47Z+00:00

Style.css

img#center-img{ display: block; margin: auto; }

Html

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

answered Feb 12, 2021 at 14:28

Callum's user avatar

1 Comment

Please only add answers that haven't been provided. This is, as it happens, the same as the accepted answer, among others.

2021-02-12T15:03:20.863Z+00:00

To center a image use this css. You have to give width at first of the image.

img{ width: 300px; position: fixed; left0; right:0; }

answered Nov 23, 2021 at 14:33

Sk Shoyeb's user avatar

1 Comment

This is not the correct way to center an image.

2021-11-24T15:37:33.807Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.