Dynamically set width and height of image using javascript

I want to dynamically set the height and width of an image using javascript so that the image looks proportionally fine according to different screen resolutions…

The original image is of 252*62 pixels…

I have used the following code…but it doesnt seem to work.

<script type="text/javascript">
function resizeheight() {
    var height = ((62*screen.height)/800);
	
	return height;
}
function resizewidth() {
    var width = ((252*screen.width)/1280);
	
	return width;
}


<img id="enter" src="enter.PNG" height="resizeheight()" width="resizewidth()" >

the width and height attributes in HTML cannot accept javascript as parameters… to do what you are trying to do, you’d have to write out your image WITH the javascript (document.write) or latch onto it by id to resize it. In fact, few if any HTML attributes OTHER than the event based ones accept javascript as parameters.

I’d also point out that you can simply return a value instead of storing it in a variable first.

for example:


<img id="enter" src="enter.PNG" alt="enter" />

<script type="text/javascript">
target=document.getElementById('enter');
target.style.width=((252*screen.width)/1280)+'px';
target.style.height=((62*screen.height)/800)+'px';
</script>

this does’nt work too.

It worked. I had done some mistake. Thanks a lot :slight_smile:

It seems that this article is designed just for you then.
Perfect Full Page Background Image

@paul_wilkins

Yeah thanks :). I’ll check it out.