• 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

    • Not such a great deal, Ultimate, which gives you full 5080 features is $181.99 CAD per month, that's $2183.88 per year, I can buy the 5080 for $1809.99 CAD, then it goes up to $279.99 per month after the first billing cycle. Typical cloud rental, costs more than buying the hardware.
    • Wow, spoken like a true blind hater, you don't even provide arguments. Please, go check my comment above to @seacaptain and you'll find out why what you say doesn't make sense in this context...
    • Get used to this, with AI tooling now uncovering new vulns and getting them exploitable far faster than has ever been possible before software is going to need to be updated far more frequently. Back in the day it may take reseachers weeks or months to do what AI can now do in hours. Once its a threat is discovered it's weaponsized far more quickly, meaning you simply can't be waiting 2, 3, 4 weeks to deploy a patch, it needs to be patched immediately. Going to be interesting handling this in the enterprise space where traditionally patching has been steady, but very staged (and rightly so up until now), that is going to have to change.
    • You don't need to "close all browser sessions constantly" or wait for updates to install. The updates download in the background while you use the browser, without interrupting you, they install automatically the next time you launch the app. And they install very fast (depending on your storage speeds, of course), you have to wait at most 2-3 extra seconds, if any. Seems like you haven't used Edge in a loooooooong time...
    • Segra 1.6.0 by Razvan Serea Segra is a free, open-source OBS-powered game recorder offering fast gameplay capture, instant clips, AI highlights, deep game integration, and seamless uploads—perfect for gamers, streamers, and content creators. Lightweight, fast, zero bloat. Segra key features: Automatic Game Recording: Begin capturing gameplay the moment your game launches, with zero manual setup. Instant Clipping: Save important moments instantly using a customizable hotkey—perfect for highlights, montages, or quick shares. Segra AI Highlights: Let Segra automatically detect kills, assists, deaths, and key events to generate polished highlight reels without manual editing. Gameplay Uploads: Upload recordings and clips directly to Segra.tv for fast sharing and cloud access. Deep Game Integration: Enjoy advanced game-data tracking across hundreds of supported titles, enabling smart highlight generation and stat-informed clipping. High-Performance Capture: Record up to 4K at 144 FPS using OBS-powered technology with minimal performance impact, supporting NVENC, AMD VCE, and custom quality controls. Segra Editor: Edit recordings easily with timeline controls, segment management, and event-based navigation to build the perfect clip. Customization Options: Adjust hotkeys, output formats, storage paths, codecs, capture quality, and performance settings for a tailored recording experience. Segra 1.6.0 changelog: Recording: Added HDR support. Grand Theft Auto: Added game integration for deaths (FiveM and RAGE MP supported). Highlights: Added customizable padding for highlights. Replay Buffer: Added a shockwave visual effect when a replay buffer clip is saved. Audio: Increased the maximum sound effects volume from 100% to 200%. Hotkeys: Fixed hotkeys not triggering while unrelated keys were held. Installer: Added code signing to verify publisher identity, branded the installer, and reduced OS security warnings. OBS: Updated the supported OBS version to 32.1.2. Download: Segra 1.6.0 | 74.4 MB (Open Source) View: Segra Homepage | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      PsYcHoKiLLa
      175
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      80
  • Tell a friend

    Love Neowin? Tell a friend!