ARTICLE AD BOX
Asked 13 years, 8 months ago
Viewed 1.0m times
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.
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.
10
Here's my solution in: http://jsfiddle.net/marvo/3k3CC/2/
241k41 gold badges348 silver badges350 bronze badges
9 Comments
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:
3 Comments
@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; }2 Comments
This also would do it
#imagewrapper { text-align: center; } #imagewrapper img { display: inline-block; margin: 0 5px; }7 Comments
@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
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.
3 Comments
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.
21.4k11 gold badges123 silver badges147 bronze badges
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. */ ... }2 Comments
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
9,4051 gold badge71 silver badges56 bronze badges
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.
Put an equal pixel padding for left and right:
<div id="artiststhumbnail" style="padding-left:ypx;padding-right:ypx">21.4k11 gold badges123 silver badges147 bronze badges
To center an image horizontally, this works:
<p style="text-align:center"><img src=""></p>21.4k11 gold badges123 silver badges147 bronze badges
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.
7,3999 gold badges47 silver badges62 bronze badges
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
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;" />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;js fiddle link https://jsfiddle.net/7un6ku2m/
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"
5 Comments
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
3 Comments
Style.css
img#center-img{ display: block; margin: auto; }Html
<html> <body> <div> <img src='pic.png' id='center-img'> </div> </body> </html>Explore related questions
See similar questions with these tags.
















