I have a page with one image that I need to center both horizontally and vertically in the browser window and also to fit vertically without going past the viewport of the screen. It needs to be able to do this on IE7+/Firefox 3+ and all variations of screen resolutions and sizes.
The following code is most promising but does not work in IE7. (I don't have 8 yet.) I think the line
iw=document.body.clientWidth;ih=document.body.clientHeight;}
is not correct for IE. Can someone know the fix for this or have a better way of achieving my goal? You can see the test page at http://www.catholicadultfaith.com/test2.html.
Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type='text/javascript'>
function showpic(src, w, h, alt, aln, pw, ph, bw, bh) {
// fit a graphic within the current window or frame
// showpic provided by [url]www.DragonQuest.com[/url]
if (src==null) return;
var iw, ih // Set inner width and height for NS and Explorer
if (window.innerWidth==null) {
iw=document.body.clientWidth;ih=document.body.clientHeight;}
else {iw=window.innerWidth;ih=window.innerHeight}
if (w==null) w=iw; // width
if(h==null) h=ih; // height
if(alt==null) alt="Picture"; // alt text
if(aln==null) aln="left"; // alignment
if(pw==null) pw=100; // percentage width
if(ph==null) ph=100; // percentage height
if(bw==null) bw=0; // border width
if(bh==null) bh=0; // border height
var sw=Math.round((iw-bw)*pw/100);
var sh=Math.round((ih-bh)*ph/100);
if ((w*sh)/(h*sw)<1) sw=Math.round(w*sh/h);
else sh=Math.round(h*sw/w);
document.write('<img src="'+src+'" alt="'+alt+'" width="'+sw+'" height="'+sh+'" align="'+aln+'">');
}
</script>
<style type="text/css">
div#page {
width:100%;
margin:0 auto;
padding:5px;
text-align:center;
}
div#content {
width:100%;
margin: 0 auto;
font-family:Georgia, 'Times New Roman', Times, serif;
font-size: 1em;
}
</style>
</head>
<body bgcolor="#FFFFF0">
<div id="page">
<div id="content">
<a href="angels_and_demons.html">
<script type='text/javascript'>
showpic ("images/front%20page%20graphic.jpg", 900, 997,"collage", "center")
</script>
</a></div>
</div>
</body>
</html>