Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning image to center inside a smaller div

Tags:

html

css

I have a div with width:100px and height:100px (say) Inside that, there is just an image, for which height is always fixed to 100px.

I want to make the image horizontally center.

Here there are 3 cases:

  1. image's width is equal to div's width, no issues
  2. image's width is less than div's width, I can use margin: auto here
  3. image's width is more than div's width

I want the center part of the image to be visible inside the div.

means, if image's width is 120px and as div's width is 100px and overflow:hidden I want image's 10th px to 110th px to be visible (so, the left: 10px and right: 10px of image are hidden under the div )

Is this possible through some CSS property? (I dont know the width of image which is loading! so I want it to be dynamic. Also want to avoid javascript side calculations to find the extra amount of width and giving margin-left: -ve value bla bla.. )

Also, I can't give the image as background-image for the div!

like image 363
SuryaPavan Avatar asked Nov 16 '11 10:11

SuryaPavan


People also ask

How do I align an image to a div?

Aligning an image means to position the image at center, left and right. We can use the float property and text-align property for the alignment of images. If the image is in the div element, then we can use the text-align property for aligning the image in the div.

How align object in center in div?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I center an image in a div grid?

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 embedded image?

You can center a picture by enclosing the <img> tag in the <center></center> tags. This action centers that, and only that, picture on the web page.


1 Answers

See: http://jsfiddle.net/thirtydot/x62nV/ (and without overflow: hidden to easily see the centering)

This will work in all browsers, with the possible exception of IE6.

For .imageContainer > span, the margin-left is derived from the width, and the width is an arbitrary number which controls the maximal image width that will be supported. You could set width: 10000px; margin-left: -4950px; to support really wide images, if required.

HTML:

<div class="imageContainer">
    <span><img src="http://dummyimage.com/100x100/f0f/fff" /></span>
</div>

CSS:

.imageContainer {
    border: 1px solid #444;
    overflow: hidden;
    width: 100px;
    height: 100px;
    margin: 15px;
    text-align: center;
}
.imageContainer > span {
    display: block;
    width: 1000px;
    margin-left: -450px; /* -(width-container width)/2 */
}
.imageContainer > span > img {
    display: inline-block;
}
like image 155
thirtydot Avatar answered Oct 09 '22 01:10

thirtydot