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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:04:12+00:00 2026-06-16T00:04:12+00:00

I want to add an image inside a div(#bg) using jquery to create an

  • 0

I want to add an image inside a div(#bg) using jquery to create an auto resizing webpage. So far I have coded like this but the auto resizing have some problems. If I manually add the image to div then everything works fine. I am a novice in jquery if you can elaborate your answers that will be great. Thanks in advance.

HTML

<div id="bg">​
</div>`

CSS

#bg { position: fixed; top: 0; left: 0; }
.bgwidth { width: 100%; }
.bgheight { height: 100%; }`

jquery

$(window).load(function() { 

    $('#bg').prepend('<img src="Background.jpg" id="bg" alt="" />')

var theWindow        = $(window),
    $bg              = $("#bg"),
    aspectRatio      = $bg.width() / $bg.height();

function resizeBg() {

    if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
        $bg
            .removeClass()
            .addClass('bgheight');
    } else {
        $bg
            .removeClass()
            .addClass('bgwidth');
    }

}

theWindow.resize(resizeBg).trigger("resize");

});
  • 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-16T00:04:12+00:00Added an answer on June 16, 2026 at 12:04 am

    I’ve written some days ago a similar function:

    HTML

    <img src="http://ge.tt/api/1/files/1jN8IzR/0/blob?download" class='bg'>
    

    CSS
    not really needed

    .bg {
        position: fixed;
        top: 0;
        left: 0;
        z-index: -1
    }
    

    JS

    var fitPic = (function() {
        var winW = $(window).width(),
            winH = window.innerHeight || $(window).height(); // `innerHeight` for mobile safari
    
        $(".bg").each(function() {
    
            var elem = $(this),
                elemW = elem.width(),
                elemH = elem.height(),
    
                imgW = (elemW * winH) / elemH,
                imgH = (elemH * winW) / elemW,
    
                newW = imgH < winH ? imgW : winW,
                newH = imgH < winH ? winH : imgH;
    
            this.style.width = newW + "px";
            this.style.height = newH + "px";
    
        });
    });
    
    $(window).on('load resize', fitPic);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a <div> inside a <div class=ad-image> - How can it
I want to add an image, instead of the default button. I already have
I want to add a background image to a bar chart using Highcharts and
I'm having an image inside a div. I want to show tooltip1 on mouseover
I have an UIImageView with an image (map) inside of a UIScrollView. I add
I have an ImageView and I want to add EditText inside it where a
I am setting an image as a background inside a DIV and and want
James here. I have a quick question. I want to add a similar image
i want to add image inside a span here's what i tried ( inside
I have a jQuery autocomplete entry that I want to make look more like

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.