views:

38

answers:

6

I was wondering if in the width and height attributes, I could specify width and height as percentages?

Well, I guess that is obvious, because when I try so, it resizes, but it appears to skew the quality of my image.

Here is an example of my markup with fixed attributes:

<img src="#" width="346" height="413">

Now, while trying to scale this down, say by half, via percentages:

<img src="#" width="50%" height="50%">

I get something completely different than:

<img src="#" width="173" height="206.5">

I think I'm just fundamentally mistaking my percentage markup or something because there is a noticeable difference between my second and third example visually.

UPDATE: Hey thanks everyone for all of the helpful posts!

I really actually like the jQuery suggestion made by Pat just because with one snippet as he suggested, I can actually modify the selector and make it the apply to all of my fancybox affected images like-so:

$('img.FancyBox').each(function(){
  $(this).width($(this).width() * 0.25);
});

How awesome is that!

You can check out the live effect here at my site: http://www.marioplanet.com/product.htm

It works pretty well, considering the fact that once I connect it to my SQL Server db then I can just apply that one jQuery snippet to all of the FancyBox class images on my product pages.

Thanks guys!

A: 

You can set one or the other (just not both) and that should get the result you want.

<img src="#" height="50%">
Fosco
Oh wow! :) Thanks for that, I wonder why that is? Is it because having 40% of an image's height and 70% of the width lets say, just doesn't really occur very much?
BOSS
Unfortunately it doesn't work that way - your image will be 50% height of its parent container, not 50% of its pixel dimensions. However Fosco is right for skewing - only specifying one will cause your image to remain proportional to its original dimensions.
Pat
+1  A: 

From W3Schools (http://www.w3schools.com/tags/att_img_height.asp):

The height in percent of the containing element (like "20%").

So I think they mean the element where the div is in?

JavaPete
+1  A: 

Hi,

width="50%" and height="50%" sets the width and height attributes to half of the parent element's width and height if I'm not mistaken. Also setting just width or height should set the width or height to the percentage of the parent element, if you're using percents.

pkauko
But I'm guessing if you only specify one or the other, then it affects only the element to which the attribute is attributed to.
BOSS
@BOSS: I made a simple test.html and img with width="50%" was exactly the same size as img with width="50%" height="50%".
pkauko
That's odd, it could be because you don't have any formatting on your container, because mine might be screwing up the image.
BOSS
@BOSS: I think that in my test case this was because the image I used happened to have the same aspect ratio as the containing element. Anyway the percentage setting sets the image size to the percentage of the containing element's size in both cases (with only width or height vs with width and height) and not to the percentage of the image size.
pkauko
+1  A: 

Those percentage widths in your 2nd example are actually applying to the container your <img> is in, and not the image's actual size. Say you have the following markup:

<div style="width: 1000px; height: 600px;">
    <img src="#" width="50%" height="50%">
</div>

Your resulting image will be 500px wide and 300px tall.

jQuery Resize

If you're trying to reduce an image to 50% of its width, you can do it with a snippet of jQuery:

$('img').each(function(){
    $(this).width($(this).width() * 0.5);
});

Just make sure you take off any height/width = 50% attributes first.

Pat
A: 

Given the lack of information regarding the original image size, specifying percentages for the width and height would result in highly erratic results. If you are trying to ensure that an image will fit within a specific location on your page then you'll need to use some server side code to manage that rescaling.

Lazarus
Whoever down-voted, care to explain?
Lazarus
+1  A: 

Here is the difference:

This sets the image to half of its original size.

<img src="#" width="173" height="206.5">

This sets the image to half of its available presentation area.

<img src="#" width="50%" height="50%">

For example, if you put this as the only element on the page, it would attempt to take up 50% of the width of the page, thus making it potentially larger than its original size - not half of its original size as you are expecting.

If it is being presented at larger than original size, the image will appear greatly pixelated.

Sohnee