Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

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.

like image 690
CannibalSmith Avatar asked Jul 21 '10 14:07

CannibalSmith


People also ask

How do I make an image fit the width of a container?

If your image is portrait, and your container is landscape, you must set height="100%" on the image. If your image is landscape, and your container is portrait, you must set width="100%" on the image.

How do I center a large image in a div?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

How do you centralize an image?

To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.

How do I center an image overflow in CSS?

It is pretty easy to do if the image is smaller than the surrounding div: margin-left: auto; margin-right: auto; display: block; But when the image is larger than the div it simply starts at the left edge and is off center to the right (we are using overflow: hidden ).


1 Answers

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%;  } 
like image 185
ScottS Avatar answered Oct 04 '22 04:10

ScottS