• 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

    • Google Chrome 149.0.7827.115 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Oh, it's happening on more than one dialog? This is the dialog that I'm unable to interact with.
    • WSCC - Windows System Control Center 10.0.3.8 by Razvan Serea Windows System Control Center is a free, portable program that allows you to install, update, execute and organize the utilities from various system utility suites. WSCC can install and update the supported utilities automatically. Alternatively, WSCC can use the http protocol to download and run the programs. WSCC is portable, installation is not required. Extract the content of the downloaded zip archive to any directory on your computer. Free for personal use. The setup packages and updates are downloaded directly from their author's website! This edition of WSCC supports the following utility suites: Windows Sysinternals Suite (including support for "Sysinternals Live" service) NirSoft Utilities Mitec and more... WSCC - Windows System Control Center 10.0.3.8 changelog: [NEW] update progress is now visible on the Windows taskbar [FIXED] fixed an issue with the Update dialog [FIXED] minor fixes Download: WSCC (64-bit) | 5.4 MB (Free for personal use) Download: WSCC (32-bit) | 6.3 MB View: WSCC Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The level of stupidity at Google and Microsoft is beyond belief. All they are doing by killing this is forcing people to jump over to Firefox and ditch them all together, therefore giving Firefox the market boost it needs to finally crush them all in the end.
    • HWMonitor 1.64 by Razvan Serea HWMonitor is a hardware monitoring program that reads PC systems main health sensors : voltages, temperatures, fans speed. The program handles the most common sensor chips, like ITE® IT87 series, most Winbond® ICs, and others. In addition, it can read modern CPUs on-die core thermal sensors, as well has hard drives temperature via S.M.A.R.T, and video card GPU temperature. Special hardware monitors such as abit® uGuru and Gigabyte® ODIN™ power supplies serie are supported too. HWMonitor 1.64 changelog: Intel Arc G3 & G3 Extreme (Panther Lake). Intel Core Ultra 5 250KF Plus (Arrow Lake Refresh). AMD Ryzen 7 7700X3D (Raphael). AMD Ryzen AI Max+ 495, 492, 488 (Gorgon Halo). AMD Ryzen AI Max 490, 485 (Gorgon Halo). AMD Ryzen AI Max PRO 495, 490, 485, 480 (Gorgon Halo). AMD Ryzen 9 9950X3D2 (Granite Ridge). AMD Ryzen 9 PRO 9965X3D, PRO 9945 (Granite Ridge). AMD Ryzen 7 PRO 9755, PRO 9745 (Granite Ridge). AMD Ryzen 5 PRO 9645 (Granite Ridge). AMD Ryzen AI 7/PRO 450G/GE (Gorgon Point 2). AMD Ryzen AI 5/PRO 440G/GE (Gorgon Point 2). AMD Ryzen AI 5/PRO 435G/GE (Gorgon Point 3). Support of HUDIMM and HSODIMM memory modules. New themes. New real-time graphs. Download: HWMonitor 1.64 | 3.4 MB (Freeware) Download: Portable HWMonitor 1.64 | 2.7 MB View: HWMonitor Homepage | 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
      495
    2. 2
      PsYcHoKiLLa
      171
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      77
  • Tell a friend

    Love Neowin? Tell a friend!