Jump to content

Auto-Resize to user's browser


Graxeon

Recommended Posts

I made this new website: http://www.top-media.it.tc/

 

I need to figure out how to make the image fit within all browser dimensions while still keeping the aspect ratio of whatever content I put in there (let's say 1000x1000 lowers to 100x100 if that's the size of the user's browser).

 

 

Does anyone know the code for this? I tried searching for it for an hour but can't find one :(

Link to comment
Share on other sites

not that easy - you could use svg (scaleable vector graphics) but I think olnly opera supports that at the mo - not checked other browsers for a bit so ff3 and safari could now support them.

 

in the current state of affairs your only option is to use some js to detect viewport size and serve out different images sized accordingly for each...

Link to comment
Share on other sites

<img src="whatever.jpg" id="image" />
<script type="text/javascript">
function resizeImage(image) {
vh = window.innerHeight;
vw = window.innerWidth;
if (""+vh+"" == "undefined") vh = document.documentElement.offsetHeight;
if (""+vw+"" == "undefined") vw = document.documentElement.offsetWidth;
if (image.width > image.height) {
	cRat = image.height / image.width;
	image.height = vh * cRat; 
	image.width = vw;
} else if (image.height > image.width) {
	cRat = image.width / image.height;
	image.height = vh;
	image.width = vh * cRat;			
} else {
	image.height = Math.min(vh,vw);
	image.width = Math.min(vh,vw);
}
}
resizeImage(document.getElementById("image"));
</script>

Link to comment
Share on other sites

?????

 

why do all that? if you are just going to scale the image then width="100%" would do the job.

 

if you wanted a nice un-pixelated image at different resolutions then you would have to produce and image of a size for each range of window resolution - otherwise the design will look amateurish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.