css - Center Oversized Image in Div

ID : 20053

viewed : 2

Tags : csscss

Top 5 Answer for css - Center Oversized Image in Div

vote vote

95

Try something like this. This should center any huge element in the middle vertically and horizontally with respect to its parent no matter both of their sizes.

.parent {     position: relative;     overflow: hidden;     //optionally set height and width, it will depend on the rest of the styling used }  .child {     position: absolute;     top: -9999px;     bottom: -9999px;     left: -9999px;     right: -9999px;     margin: auto;  } 
vote vote

85

This is an old Q, but a modern solution without flexbox or position absolute works like this.

margin-left: 50%; transform: translateX(-50%); 

.outer {   border: 1px solid green;   margin: 20px auto;   width: 20%;   padding: 10px 0;   /*   overflow: hidden; */ }  .inner {   width: 150%;   background-color: gold;   /* Set left edge of inner element to 50% of the parent element */   margin-left: 50%;    /* Move to the left by 50% of own width */   transform: translateX(-50%);  }
<div class="outer">   <div class="inner">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos exercitationem error nemo amet cum quia eaque alias nihil, similique laboriosam enim expedita fugit neque earum et esse ad, dolores sapiente sit cumque vero odit! Ullam corrupti iure eum similique magnam voluptatum ipsam. Maxime ad cumque ut atque suscipit enim quidem. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Excepturi impedit esse modi, porro quibusdam voluptate dolores molestias, sit dolorum veritatis laudantium rem, labore et nobis ratione. Ipsum, aliquid totam repellendus non fugiat id magni voluptate, doloribus tenetur illo mollitia. Voluptatum.</div> </div>

So why does it work?
At first glance it seems that we shift 50% to the right and then 50% to the left again. That would result in zero shift, so what?
But the 50% are not the same, because context is important. If you use relative units, a margin will be calculated as percentage of the width of the parent element, while the transform will be 50% relative to the same element.

We have this situation before we add the CSS

       +---------------------------+        | Parent element P of E     |        |                           |        +-----------------------------------------+        | Element E                               |        +-----------------------------------------+        |                           |        +---------------------------+ 

With the added style margin-left: 50% we have

       +---------------------------+        | Parent element P of E     |        |                           |        |             +-----------------------------------------+        |             | Element E                               |        |             +-----------------------------------------+        |             |             |        +-------------|-------------+        |>>>>> a >>>>>|                    a is 50% width of P 

And the transform: translateX(-50%) shifts back to the left

       +---------------------------+        | Parent element P of E     |        |                           | +-----------------------------------------+ | Element E         |                     | +-----------------------------------------+ |<<<<<<<<< b <<<<<<<|              |        |            |              |        +------------|--------------+        |>>>>> a >>>>|                    a is 50% width of P        b is 50% width of E 

Unfortunately this does only work for horizontal centering as the margin percentage calculation is always relative to the width. I.e. not only margin-left and margin-right, but also margin-top and margin-bottom are calculated with respect to width.

Browser compatibility should be no problem: https://caniuse.com/#feat=transforms2d

vote vote

77

Put a large div inside the div, center that, and the center the image inside that div.

This centers it horizontally:

HTML:

<div class="imageContainer">   <div class="imageCenterer">     <img src="http://placekitten.com/200/200" />   </div> </div> 

CSS:

.imageContainer {   width: 100px;   height: 100px;   overflow: hidden;   position: relative; } .imageCenterer {   width: 1000px;   position: absolute;   left: 50%;   top: 0;   margin-left: -500px; } .imageCenterer img {   display: block;   margin: 0 auto; } 

Demo: http://jsfiddle.net/Guffa/L9BnL/

To center it vertically also, you can use the same for the inner div, but you would need the height of the image to place it absolutely inside it.

vote vote

68

Late to the game, but I found this method is extremely intuitive. https://codepen.io/adamchenwei/pen/BRNxJr

CSS

.imageContainer {   border: 1px black solid;    width: 450px;   height: 200px;   overflow: hidden; } .imageHolder {   border: 1px red dotted;    height: 100%;   display:flex;   align-items: center; } .imageItself {   height: auto;   width: 100%;   align-self: center;  } 

HTML

<div class="imageContainer">   <div class="imageHolder">     <img class="imageItself" src="http://www.fiorieconfetti.com/sites/default/files/styles/product_thumbnail__300x360_/public/fiore_viola%20-%202.jpg" />   </div> </div> 
vote vote

56

i'm a huge fan of making an image the background of a div/node -- then you can just use the background-position: center attribute to center it regardless of screen size

Top 3 video Explaining css - Center Oversized Image in Div

Related QUESTION?