• 0

How to image size inside invisible element?


Question

for example, something like this:

<div style="display:none;">
<span>
<table>
<tr>
<td>
<img id="image1" src="something.png" style="width:50px; height:100px;" alt="some image" />
</td>
</tr>
</span>
</div>
<script type="text/javascript>
var mi=document.getElementById('image1');
var height=mi.offsetHeight;
var width=mi.offsetWidth;
</script>

since the whole div is invisible, the height and width are both 0, so is there anyway to get the numbers 100 and 50 for height and width?

From my understanding, if I know which element has the display:none style, I can change the style to display:block and get the image size and then set it back to display:none, however currently there may be an arbitrary number of elements nested around the image element, and I don't know which of them is set to display:none, how may I get the image size in this case? Thanks.

10 answers to this question

Recommended Posts

  • 0
i guess this will work

document.getelementbyid("image1").width=50;

document.getelementbyid("image1").height=100;

Load these after the image have loaded

Oops, I meant to say "get image size", not "set image size", somehow I mis-deleted the "get" in the topic title :wacko:

  • 0

If the width and height are always defined in a CSS style (inline or from a stylesheet), then you should be able to retrieve them with:

var mi = document.getElementById("image1");
var width = parseInt(mi.style.width);
var height = parseInt(mi.style.height);

  • 0
wellofsouls, this is maybe something that related to what you wanted to accomplish... check this out

http://code.google.com/p/timthumb/

timthumb.php is probably what the doctor order

The thing is that I need to do it client side with Javascript, so no php allowed :(

  • 0
If the width and height are always defined in a CSS style (inline or from a stylesheet), then you should be able to retrieve them with:

var mi = document.getElementById("image1");
var width = parseInt(mi.style.width);
var height = parseInt(mi.style.height);

Unfortunately many of the images do not have size defined in CSS. :(

  • 0

Can I just ask... why you need it if it's display : none; ?

Are you going to show it?

Are you spacing elements around it?

Maybe the visibility : hidden; might be of more use than display : none;

Or script it in and preload the image, or test it in script. You can use height and width instead of offset...

http://stackoverflow.com/questions/799710/...idth-and-height

Edited by lunamonkey
  • 0
Can I just ask... why you need it if it's display : none; ?

Are you going to show it?

Are you spacing elements around it?

Maybe the visibility : hidden; might be of more use than display : none;

Or script it in and preload the image, or test it in script. You can use height and width instead of offset...

http://stackoverflow.com/questions/799710/...idth-and-height

well, I need to make a javascript function that runs at page load and checks every image in a web page and resize it if it's too large or too small. Maybe the images will be invisible at first, but later made visible by some javascript trigger.

And yeah, I also just figured that preloading the images seems to be the only way :

<script type="text/javascript>
var mi=document.getElementById('image1');
var ti = document.createElement('img');
ti.setAttribute('src', mi.getAttribute('src'));
ti.style.width=mi.style.width;
ti.style.height=mi.style.height;
document.body.appendChild(ti);
var height=ti.offsetHeight;
var width=ti.offsetWidth;
document.body.removeChild(ti);
</script>

it's quite a hassle, but seems to work fine so far.

alternatively, guess I can first try to get the style.width and style.height, and if that fails, get the height and width. That way there's no need to append and remove the image in the body, which may not be safe I think...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Win11Debloat 06.11.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.11.2026 fixes: Fix lock screen spotlight option being disabled when disabling the start recommended section by @Raphire in #619 Fix log message formatting by @Raphire Note The -RemoveCommApps and -RemoveW11Outlook command-line parameters for uninstalling a few specific apps have been removed with this release. If you previously relied on these parameters, please see this wiki page for alternative methods of removing these apps. Download: Win11Debloat 06.11.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yes for me, I installed 'old calculator' (Windows 7 calculator) in its place since it is more useful to me. I think paint is the only one I left installed
    • eh I'll wait for the June 2026 MVS ISO downloads which should be coming out next Tuesday June 16 and possibly contain build 8655 instead of 8653
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!