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 4325490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:16:25+00:00 2026-05-21T09:16:25+00:00

So I am trying to insert an image to a page with JavaScript with

  • 0

So I am trying to insert an image to a page with JavaScript with 50% of its width and 50% of its height.

I do this:

someElement.html('<img src="/path/to/img.jpg" alt="" class="sImg" />');

The sImg class is defined in stylesheet like this:

.sImg{
    border: 0;
    width: 50%;
    height: 50%;
}

Yet the image appears fullsize.

I have also checked via Firebug and the image has width and height both at 50%.

  • 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-21T09:16:26+00:00Added an answer on May 21, 2026 at 9:16 am

    The CSS you’ve got means that the width and height should be computed as half the size of the parent container, not the image itself.

    What you can do is something like this: create an Image object and give it an “onload” handler. The handler can get reliable size information (because the image will have been loaded), and can then add the image element with the proper size.

    var img = new Image();
    img.onload = function() {
      $(someElement).empty().append($('<img/>', {
        src: img.src,
        alt: '',
        'class': 'sImg',
        css: { width: Math.floor(img.width / 2) + 'px', height: Math.floor(img.height / 2) + 'px', display: 'inline-block' } // display should be set as you need it
      });
    };
    img.src = yourUrl;
    

    edit — the eerily knowledgeable Šime Vidas points out that setting the “width” or “height” attribute should make the right thing happen, with the size being reduced appropriately to maintain the aspect ratio.

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

Sidebar

Related Questions

When im trying to insert image with path like this: /wombat/htdocs/web/icon.png after insert its
I am trying to insert an Image on a HTML page on mouse button
i'm trying to insert an image in a div container using css and html,
This is the code I have. I'm trying to insert a image to show
I am trying to use CSS and HTML to insert an image into a
I am trying to insert an image in ListView and when I click on
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
I'm trying to insert an image into the database. CREATE TABLE ImageTable ( Id
I am trying to retrieving image from sql server 2005 into asp.net web page
I'm trying to insert one more image in my UIScrollView. The problem is: all

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.