Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:47:13+00:00 2026-06-13T12:47:13+00:00

Here is my example HTML: <div class=container> <img src=http://placehold.it/500×500 alt=500×500 /> </div> <div class=container>

  • 0

Here is my example

HTML:

<div class="container">
    <img src="http://placehold.it/500x500" alt="500x500" />
</div>
<div class="container">
    <img src="http://placehold.it/100x500" alt="100x500" />
</div>
<div class="container">
    <img src="http://placehold.it/500x100" alt="500x100" />
</div>
<div class="container">
    <img src="http://placehold.it/500x800" alt="500x800" />
</div>
<div class="container">
    <img src="http://placehold.it/800x500" alt="800x500" />
</div>
<div class="container">
    <img src="http://placehold.it/100x100" alt="100x100" />
</div>
<div class="container">
    <img src="http://placehold.it/200x100" alt="200x100" />
</div>​

CSS:

div.container { width: 200px; height: 200px; line-height: 200px; overflow: hidden; border: 10px solid white; box-shadow: 0 1px 8px #BBB; margin: 10px; text-align: center; }
            div.container img { position: relative; vertical-align:middle; }
            div.container img.wide { max-height: 100%; }
            div.container img.tall { max-width: 100%; }
            div.container img.square { max-width: 100%; }​

Javascript:

$(window).load( function(){
var $imgs = $("div.container img");

$imgs.each( function(){
    var cW = $(this).parent().width();
    var cH = $(this).parent().height();
    var w = $(this).width(); //I want the CURRENT width, not original!!
    var h = $(this).height(); //I want the CURRENT height, not original!!
    var dW = w - cW;
    var dH = h - cH;

    console.log( cW + " " + cH + " " + w + " " + h + " " + dW + " " + dH );

    if ( w > h ){ 
        $(this).addClass("wide");
        $(this).css("left",  -dW/2);
    }
    else if ( w < h ){
        $(this).addClass("tall");
        $(this).css("top",  -dH/2);
    }
    else { $(this).addClass("square"); }                    
});
});​

I’m adding, via javascript, a class to the image elements based on their proportions. The 3 classes will set the max-width and max-height css properties for the images, thus resizing them.
After that, I need the resized image’s dimensions, but all I can get is the original image’s dimensions. That’s no good! I tried using width(), outerWidth, naturalWidth, every kind of width, but I just can’t get it right.

So, is there a way to get the image’s dimensions after CSS rules like max-width and max-height have been applied to it? Preferably a consistent way, not using timers and intervals, or stuff like that.

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:47:14+00:00Added an answer on June 13, 2026 at 12:47 pm

    Check out this jsfiddle. You are trying to get widht/height BEFORE you apply a class with max-height/max-width instead of after it is applied.

    Here is test code (check second console.log):

    $(window).load( function(){
        var $imgs = $("div.container img");
    
        $imgs.each( function(){
            var cW = $(this).parent().width();
            var cH = $(this).parent().height();
            var w = $(this)[0].clientWidth; //I want the CURRENT width, not original!!
            var h = $(this)[0].clientHeight; //I want the CURRENT height, not original!!
            var dW = w - cW;
            var dH = h - cH;
    
            console.log( cW + " " + cH + " " + w + " " + h + " " + dW + " " + dH );
    
            if ( w > h ){ 
                $(this).addClass("wide");
                $(this).css("left",  -dW/2);
            }
            else if ( w < h ){
                $(this).addClass("tall");
                $(this).css("top",  -dH/2);
            }
            else { $(this).addClass("square"); }     
            var w = $(this).width(); //I want the CURRENT width, not original!!
            var h = $(this).height(); //I want the CURRENT height, not original!!
            console.log( w + " " + h );
    
        });
    });​
    

    width(), outerWidth – this always return current width.

    naturalWidth/naturalHeight – this is to get an original,like with no any styles applied, width/height of image (available for IMG tag only in newer browsers)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample here: http://jsfiddle.net/YUhgb/ HTML <html> <body> <div class=left></div> <div class=right> <div class=container> <div class=item></div>
here is link to an example: http://techchorus.net/demos/jquery/hiding-input-elements-in-a-div.html It actally disables few elements on click
Consider the following example: ( live demo here ) HTML: <div class=board> <div class=row>
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
here's a basic example of what I'm trying to do: <div class=container> <h2>Greetings</h2> <p>All
I am using the jQuery spinner tool found here: http://btburnett.com/spinner/example/example.html When I try using
Checking the datagrid example here http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html see the datagrid section It says that if
So I am trying to work through the example here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/SurfaceViewOverlay.html and on the
Here is an example: $(function() { $('#test').change(function() { $('#length').html($('#test').val().length) }) }) <script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script> <textarea
I would like to translate or 301-redirect urls such as: www.domain.com/example.html to www.domain.com/example Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.