Web Hosting Offers
See the new scripts recently added to Dynamic Drive. Click here.
Original, practical CSS codes and examples such as CSS menus for your site.
    Search code samples  
 
  Keyword
 
 
 
   
 
 
  Code Samples
 
ASP
CSS
Dhtml
Html
Javascript
PHP
 
   
     LOGIN HERE  
 
Username
Password
Forgot Password
Verify Signup
 
 
 

Javascript >> Images

How can I have an IMG sized to a certain percentage of the window/fram

Visitor Ratings (0) :

Description
method1: HTML knows percentage settings for WIDTH attributes e.g. <IMG WIDTH="60%" ...>
or for instance in

<TABLE WIDTH="100%" HEIGHT="100%">
<TR>
<TD ALIGN="center" VALIGN="middle">
<IMG SRC="http://www.itechies.net/hotscripts/images/itechies-logo.gif" WIDTH="80%">
</TD>
</TR>
</TABLE>

That the way the image is sized according to the current window's size
and is resized when the window is resized.

method2: An alternative way is to document.write the WIDTH as needed after checking the window dimensions:

var windowWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;
document.write(
'<IMG WIDTH="' + parseInt(0.6 * windowWidth) + '" ...>'
);

If you wanted to have the image resized on window resize you need to add

function reload () { window.location.reload(); }
window.onresize = reload;

IE also can assign expressions to style attributes which are reevaluted every time a subexpression changes so with IE
<IMG STYLE="width:expression(parseInt(0.6 *
document.body.clientWidth));" ...>
should be another way.
Code
Select All
 Rate this Resource