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

  • Home
  • SEARCH
  • 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 459387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:46:03+00:00 2026-05-12T22:46:03+00:00

I cobbled together this jQuery function. It’s purpose is to calculate the margins of

  • 0

I cobbled together this jQuery function. It’s purpose is to calculate the margins of all img elements inside div.article in order to balance the image’s height with the baseline grid of the document, wich is 20 px. In order to match my baseline grid, every image height should be a multiple of 20. If that’s not the case, e.g. one image’s height is 154 px, the function adds a 6 px margin to the img, so that the balance with the baseline grid is restored.

The function works correctly, so my actual question is: Since I’m not a programmer, I’m wondering if my code is very crappy although it’s working, and if so, how could the code be improved?

The jQuery code:

$('div.article img').each(function() {

    // define line height of CSS baseline grid:
    var line_height = 20;

    // capture the height of the img:
    var img_height = $(this).attr('height');

    // divide img height by line height and round up to get the next integer:
    var img_multiply = Math.ceil(img_height / line_height);

    // calculate the img margin needed to balance the height with the baseline grid:
    var img_margin = (img_multiply * line_height) - img_height;

    // if calculated margin < 5 px:
    if (img_margin < 5) {

        // then add another 20 px to avoid too small whitespace:
        img_margin = img_margin + 20;  
    }

    // if img has caption:
    if ($($(this)).next().length) {

        // then apply margin to caption instead of img:
        $($(this)).next().attr('style', "margin-bottom: " + img_margin + "px;");
    } else {

        // apply margin to img:
        $(this).attr('style', "margin-bottom: " + img_margin + "px;");
    }
});

HTML code example, img with caption:

<div class="article">
   <!-- [...] -->
    <p class="image">
        <img src="http://example.com/images/123.jpg" width="230" height="154" alt="Image alt text goes here" />
        <small>Image Caption Goes Here</small>
    </p>
   <!-- [...] -->
</div>

HTML code example, img without caption:

<div class="article">
    <!-- [...] -->
    <p class="image">
        <img src="http://example.com/images/123.jpg" width="230" height="154" alt="Image alt text goes here" />
    </p>
   <!-- [...] -->
</div>

Edit: refined code based on Russ Cam’s suggestions:

var line_height = 20;
var min_margin = 5;
$('div.article img').each(function() {
    var $this = $(this); // prefixed variable name with $ to remind it's a jQuery object
    var img_height = $this.height();
    var img_margin = ((Math.ceil(img_height / line_height)) * line_height) - img_height;
    img_margin = (img_margin < min_margin)? img_margin + line_height : img_margin;
    if ($this.next().length) {      
        $this.next().css({'margin-bottom' : img_margin + 'px'});
    } else {
        $this.css({'margin-bottom' : img_margin + 'px'});
    }
});
  • 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-05-12T22:46:04+00:00Added an answer on May 12, 2026 at 10:46 pm

    Some improvements I can recommend

    1.cache $(this) inside the each() in a local variable

    $('div.article img').each(function() {
    
        var $this = $(this); // prefixed variable name with $ 
                             // to remind it's a jQuery object
    
        // ....
    
       if ($this.next().length) {
    
       // ....
    
       }
    
    });
    

    2.instead of setting attr('style'), use the css() command

    3.No need to do this

    $($(this))
    

    whilst it won’t break jQuery, it’s an unneccessary to pass a jQuery object into another jQuery object.

    4.Use $(this).height() or $(this).outerHeight() to get the height of the element in the browser

    5.Not jQuery specific, but could use the ternary conditional operator to assign this value

    // if calculated margin < 5 px:
    if (img_margin < 5) {
    
        // then add another 20 px to avoid too small whitespace:
        img_margin = img_margin + 20;  
    }
    

    becomes

    // if calculated margin < 5 px then add another 20 px 
    // to avoid too small whitespace
    img_margin = (img_margin < 5)? img_margin + 20 : img_margin;  
    

    6.As Alex has noted in the comments, I’d also remove the superfluous comments that merely repeat what the code tells you. Even if this is the debug script, in my opinion, they reduce readability and comments should serve only to provide detail that is not inherent from reading the code.

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

Sidebar

Related Questions

$(document).ready(function() { $(span.link).mouseover(function(e){ $(this.children).css(display,inline); }); }); I'm not a javascript expert, but I've cobbled
I know very little about JavaScript but despite this I'm trying to cobble something
I'm trying to make a function template that will accept two (or more) of
I am a web guy doing mostly Perl server-side stuff, and I'm slowly coming
I've got an Enum called DoYouKnow, containing Yes , No and Unknown . I
We're changing the way users embed images in their blogs, but I need to
I have a small Spring MVC webapp (which embeds ActiveMQ) that is designed to

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.