Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Dynamically set width and height of image using javascript

Featured Replies

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()" >

You can't embed javascript calls in the width and height properties like that. You're best off attaching an event listener to DOM ready, and by the time you've done that you might as well use jQuery.

  • Author

I used another method. It worked. Thanks for your suggestion though :)

 

<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>

It might be worth looking into adding some script to the resize function of the body, as if the window is not the full screen size it will be too large (depending on what you're trying to achieve).

 

Adding a jQuery resize function to the "onresize" function of the body can allow the user to resize the window and have the image scale proportionally...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.