How do I center an image if it's wider than its container?

Css

Css Problem Overview


Normally, you center images with display: block; margin: auto, but if the image is larger than the container, it overflows to the right. How do I make it overflow to the both sides equally? The width of the container is fixed and known. The width of the image is unknown.

Css Solutions


Solution 1 - Css

A pure css solution

Requiring one extra wrapper (tested in FireFox, IE8, IE7):

Improved Answer

There was a problem with the original answer (below). If the image is larger than the container that outer is centered on with it's auto margins, then it truncates the image on the left and creates excessive space on the right, as this fiddle shows.

We can resolve that by floating inner right and then centering from the right. This still truncates the img off the page to the left, but it does so by explicitly pushing it that way and then centers back off of that, the combination of which is what prevents the extra horizontal scroll on the right. Now we only get as much right scroll as we need in order to see the right part of the image.

Fiddle Example (Borders in fiddle are for demo only.)

Essential CSS

div.outer {
    width: 300px; /* some width amount needed */
    margin: 0 auto; 
    overflow: visible;
}
div.inner {
    position:relative;
    float: right; /* this was added and display removed */
    right: 50%;
}
div.inner img {
    position: relative; 
    right:-50%; /* this was changed from "left" in original */
}

If you desire no right scroll at all for wide images

Then using the above, also set whatever element wraps outer (like body or a third wrapper) to have overflow: hidden.


Original Idea (for History)

Fiddle Example (Borders in fiddle are for demo only.)

HTML

<div class="outer">
    <div class="inner">
        <img src="/yourimage.png">
    </div>
</div>

CSS

div.outer {
    width: 300px; /* some width amount needed */
    margin: 0 auto; 
    overflow: visible;
}
div.inner {
    display: inline-block; 
    position:relative; 
    right: -50%;
}
div.inner img {
    position: relative; 
    left:-50%; 
}

Solution 2 - Css

Here's a 2 line CSS solution (a couple more lines might be required for cross-browser support):

img {
    margin-left: 50%;
    transform: translateX(-50%);
}

Solution 3 - Css

HTML

​<div class="image-container">
  <img src="http://www.google.com/images/logo.gif" height="100" />
</div>​

CSS

.image-container {
    width: 150px;
    border: solid 1px red;
    margin:100px;
}

.image-container img {
    border: solid 1px green;
}

jQuery

$(".image-container>img").each(function(i, img) {
    $(img).css({
        position: "relative",
        left: ($(img).parent().width() - $(img).width()) / 2
    });
});

See it on jsFiddle: http://jsfiddle.net/4eYX9/30/

Solution 4 - Css

Alternative pure CSS solution is to use transform attribute:

HTML:

<div class="outer">
    <img class="image" src="http://www.gstatic.com/webp/gallery/4.jpg" />
</div>

CSS:

.outer {
    position: relative;
    width: 100px;
    border: 1px solid black;
    height: 150px;
    margin-left: 100px; /* for demo */
    /* overflow: hidden; */
}

img.image {
    width: 200px;
    opacity: 0.7;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
}

Fiddle

Just to add a overflow:hidden to parent div to hide the extra area of the image.

Solution 5 - Css

Your best bet is to set it as background image of the container instead.

#container {
    background: url('url/to/image.gif') no-repeat center top;
}

Solution 6 - Css

In fact there is a simpler pure css/html way (without large horizontal scroll) :

Html :

<div class="outer">
  <img src="/my/sample/image.jpg">
</div>

Css :

If you don't want to see image overflow

div.outer img {
	position: absolute;
	left: -50%;
	z-index:-1;
}
div.outer {
	overflow: hidden;
	position: relative;
	height: 200px;
}

With image overflow visible

div.outer img {
	position: absolute;
	left: -50%;
	z-index:-1;
}
div.outer {
	overflow: visible;
	position: relative;
	height: 200px;
}
body, html {
	overflow-x:hidden;
}

A background solution with image overflow visible :

Html :

<div class="outer">
  <div class="inner"></div>
</div>

Css :

div.outer {
	width: 100%;
	height: 200px;
}
div.inner {
	background: url('/assets/layout/bg.jpg') center no-repeat;
	position: absolute;
	left: 0;
	width: 100%;
	height: inherit;
}

assuming outer is in a width specified container.

Solution 7 - Css

I can only think of a Javascript solution since what you need to do is relatively position the image a negative amount to the left of its container:

jQuery

$(document).ready(function(){
    
    var theImg = $('#container img');
    var theContainer = $('#container');
    if(theImg.width() > theContainer.width()){
        theImg.css({
            position: 'relative',
            left: (theContainer.width() - theImg.width()) / 2
        })
    }
})

Solution 8 - Css

I see this is an old post, so maybe everybody knows this by now, but I needed help for this and I solved it using flex:

.parent {
   display: flex;
   /* give it the width and height you like */

}

.parent img {
   min-width: 100%;
   min-height: 100%;
   object-fit: cover;
}

Solution 9 - Css

I found this to be a more elegant solution, without flex, similar to something above, but more generalized (applies on both vertical and horizontal):

.wrapper {
    overflow: hidden;
}
.wrapper img {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    /* height: 100%; */ /* optional */
}

Solution 10 - Css

I don't think there is a pure CSS solution (Except for the next answer :)). However with Javascript it would be just a matter of finding the width of the image, subtracting the container width, dividing by two and you have how far to the left of the container you need.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCannibalSmithView Question on Stackoverflow
Solution 1 - CssScottSView Answer on Stackoverflow
Solution 2 - CssSashaView Answer on Stackoverflow
Solution 3 - CssjessegavinView Answer on Stackoverflow
Solution 4 - Cssfuermosi777View Answer on Stackoverflow
Solution 5 - CssBalusCView Answer on Stackoverflow
Solution 6 - CssNoémien KocherView Answer on Stackoverflow
Solution 7 - CssPatView Answer on Stackoverflow
Solution 8 - CssNordinZView Answer on Stackoverflow
Solution 9 - CssCezar D.View Answer on Stackoverflow
Solution 10 - Cssqw3nView Answer on Stackoverflow