tags:

views:

428

answers:

9

I want to center an image in an area, without resizing... I am using HTML.

Example:

I have an image <img src='img1.png' width='64' height='64'> - the image is actually 64x64. It displays perfectly.

Now, I have another image <img src='img2.png' width='64' height='64'> however, the image is not as big as it should be, its 32x32 - what happens here is it resizes the image to 64x64 and makes it look like $%^&.

How do I make images smaller then the desired width and height centered in the 'img' area without any resizing what so ever?

A: 

You could try putting the image inside a DIV that is 64x64 and not specifying the image dimensions. Then you could style the div so its contents are centered and any overflow is hidden.

Lawrence Barsanti
A: 

A solution I once used: Add a table with a single row, sincle cell. Set it's width and height to 64, set no borders, no spacing, no padding and make it align and valign to center. Put your image in the cell of this table.

Workshop Alex
A: 

You can dynamically get an image size using the getimagesize() php function:

<?php
  $size = getimagesize('imgX.png');
  $height = $size[1];
  $width = $size[0];
?>
<div style="text-align: center">
  <img src="imgX.png" width="<?php print($width) ?>" height="<?php print($height) ?>" />
</div>
Julien Poulin
A: 

An img tag with width and height attributes is saying "stretch or shrink the image to this size regardless of its actual size". use something like:

<div style="text-align:center;">
 <img src="x.jpg">
</div>

and no i don't know why text-align would work, but it appears to in my experience.

David Archer
A: 

I've had to do something similar with 36x36 images. Users were able to upload any size but the thumbnails were only to show the center 36 square pixels.

Markup:

<li><div><span></span>
    <img src="_media/objects/jessica-bowman.png" alt="Jessica Bowman" /></div>
    <p><a href="#">Jessica Bowman</a></p>
</li>

The span was just there to get rounded corners on the image, it's not necessarily needed.

CSS:

ul.recent-list li div {
    position: relative;
    float: left;
    width: 36px;
    height: 36px;
    overflow: hidden;
}

ul.recent-list li div span {
    position: relative;
    z-index: 100;
    display: block;
    width: 36px;
    height: 36px;
    background: url("../_media/icons/icon-overlay.png") top left no-repeat;
}

ul.recent-list li div img {
    position: relative;
    top: -36px;
    z-index: 0;
    float: left;
}

JavaScript:

$(window).load(function() {
$("ul.recent-list div img").each(function() {
 var moveX = ($(this).width() / 2 * -1) + 18;
 var moveY = ($(this).height() / 2) * -1 - 18; // 18 is 1/2 the default offset of 36px defined in CSS
 $(this).css({'top' : moveY, 'left' : moveX});
});
});
Mark Hurd
A: 

The solution is a simple bit of CSS + HMTL

<img src="transparentpixel.gif" 
width="64" 
height="64" 
style="
 background-image:url('path/to/image.jpg');
 background-repeat:no-repeat;
 background-position:center center;
" />

the transparentpixel.gif is a simple 1x1px transparent gif image

Ramuns Usovs
surely you shouldn't have to load 2 images everytime you want to show just one
David Archer
You should know that if you use that the picture is "less accessible" to users should they want to copy/paste/save the file anywhere. I would generally recommend against this method as it's not really semantic: the elements don't do what they say they do.
Alex Mcp
well that of course depends on your needs, but the transparentpixel.gif image is loaded only once per page (and can be cached and is tiny) so it does not really clog up the page. As for the "less accessible" part - well there are cases where you don't want people to have it easy to copy your images.
Ramuns Usovs
True, it is a tiny bit of work to load the transparent image, but that is not the point. Html and css have the ability to accomplish this task without having to load up secondary images. Also I still strongly agree with @Alex regardless of your reasoning against his
David Archer
CSS background images should only be used for presentational graphics, if you're using an <img> tag it means the image is part of the page content and should be treated as such.
roryf
"there are cases where you don't want people to have it easy to copy your images" - if your images are viewable in a web browser you cannot stop them copying it. Trying to stop them is a completely wasted exercise...
roryf
+2  A: 

What you will need is something like this:

<div class="box">
    <img src="whatever size image you'd like" />
</div>

And for the styling (in an external stylesheet, natch) you'd apply:

/* Image centering */
div.box {
 border: 1px black solid;
    height: 64px;
    width: 64px;
    background: #444;
 display: table-cell;
 vertical-align: middle;
}
.box img {
 display:block;
 margin: 0px auto;
}

This works for images with dimensions <= 64x64px, and is easily modifiable to work with larger pics. The key elements here are

  • set dimensions on the div
  • display as a table-cell (allows vertical align)
  • vertical align (aligns on the Y-axis w/out weird hacks)
  • display:block on the img element
  • margin: auto centers the image laterally
Alex Mcp
Any alternatives to display: table-cell; if IE support is required?
roryf
This is the best resource I could find, but delves into pretty hacky territory:http://www.brunildo.org/test/img_center.html
Alex Mcp
How about a solution that works in the real world full of old internet explorers?
Ramuns Usovs
A: 

Use CSS to render the image using background:

<div style="background: url(img1.png) no-repeat center center; height: 64px; width: 64px;"></div>

This will show the image in the center, without scaling it.

Jon Benedicto
+1  A: 

Solution without IE-unfriendly display:table-cell:

<!DOCTYPE html>
<style>
div {
  line-height:64px; /* that's the secret sauce */
  text-align:center;
  width:64px; height:64px;
}
img {vertical-align:middle}
</style>
<div><img …></div>
porneL
Interesting, I tried this approach but couldn't get it to work, apparently I was doin it wrong...
roryf
Make sure there are no block elements in there. If you need block elements, you'll need inline-block (and work around issues in IE).
porneL