Window backdrop being filled

I am attempting to set a background image that fills the entire window regardless of its size.

I have implemented html, css and script as shown below:

// Function adaptImage()
// Parameters: targetimg 
function adaptImage(targetimg) {
  var wheight = $(window).height();
  var wwidth = $(window).width();


  targetimg.removeAttr("width")
  .removeAttr("height")
  .css({ width: "", height: "" });

  var imgwidth = targetimg.width();
  var imgheight = targetimg.height();

  var destwidth = wwidth;
  var destheight = wheight;


  if(imgheight < wheight) {


    destwidth = (imgwidth * wheight)/imgheight;

    $('#fundo img').height(destheight);
    $('#fundo img').width(destwidth);
  }

  


  destheight = $('#fundo img').height();
  var posy = (destheight/2 - wheight/2);
  var posx = (destwidth/2 - wwidth/2);


  if(posy > 0) {
    posy *= -1;
  }
  if(posx > 0) {
    posx *= -1;
  }


  $('#fundo').css({'top': posy + 'px', 'left': posx + 'px'});
}


$(window).resize(function() {
  adaptImage($('#fundo img'));
});


$(window).load(function() {
  $(window).resize();
});
#fundo-externo {
  overflow: hidden; 
  width: 100%;
  height: 100%;
  position: relative; 
}

#fundo {
  position: fixed; 
  width: 100%;
  height: 100%;
}

#fundo img {
  width: 100%; 
  position: absolute;
}

#site {
  position: absolute;
  top: 40px;
  left: 50%;
  width: 560px;
  padding: 20px;
  margin-left: -300px; 
  background: #FFF; 
  background: rgba(255,255,255,0.8); 
}
<body>
  <div id="fundo-externo">
    <div id="fundo">
      <img src="http://placehold.it/1920x1080" alt="" />
    </div>
  </div>
</body>

However, on some devices, the background does not completely fill the screen, leaving a white space at the bottom.

We would appreciate any suggestions or ideas. Thank you!

Answer №1

Implement the following code to set your background image

.background_image  {
  background-size: cover; 
  background-size: 100%;
  background-repeat: no-repeat;  
   height: 100%;
}   

Answer №2

Avoid using the <img> tag; instead, set the image as the background image for your body:

body {
  background: url('https://example.com/image.jpg') no-repeat;
  background-position: fixed;
  background-size: cover;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Tagging multiple content snippets within a label element to create a truncated text

How can I truncate a specific inner span of a label, containing multiple span elements, to prevent overflow into the next line? To address this issue, I have created a JSFiddle available at https://jsfiddle.net/keltik/k18892xe/3/. Here is a snippet of the ...

a guide on expanding a submenu in a shiny dashboard sidebar without using automated functions

I am facing a challenge in manually expanding a submenu within a sidebar on shiny dashboard. The function updateTabItems does not seem to work with nested menus, only with normal menus. For example, when I click on 'Switch tab', it switches the ...

Send PHP data through AJAX and retrieve it on a different PHP page

I need to send a variable through an AJAX URL to another PHP page and retrieve it using: $id=$_GET['id']; Here's an example where the value is passed, for instance $id=6. <a id="pageid" href="ifsc-bank.php?id=<?=$id?>"><im ...

When using JSX, it's important to wrap adjacent elements within an enclosing tag to avoid errors. Make sure to properly wrap the JSX tags to

import React, { useState } from 'react'; import ReactDOM from 'react-dom'; function DisplayData(props) { //creating the DataList const dataList = data.map(data => ( <><span>{data.name}</span> nbsp; <span> ...

Difficulty executing for loop due to multiple buttons, resulting in malfunctioning buttons in JavaScript code

Currently encountering an issue while trying to implement modal windows on my first website. Everything works fine without the for loop, but I wanted to have multiple buttons and windows, so I decided to use a for loop to handle each button. Strangely, the ...

Is there a way to remove a portion of a string?

Looking to remove a section of a string using JavaScript? I attempted var sentence = "C:\mnt\c\User\Foo\Bar"; var updatedSentence = sentence.replace("mnt\c", ""); But the result was not as expected ...

Steer clear of directly accessing views in AngularJS using angular-ui-router

In my AngularJS App setup, I have the following configuration: angular .module('MyApp') .config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvi ...

After pressing the login button, my intention is to transition to a different page

I am relatively new to web development and working with angular. I have a login component that, upon hitting the LOGIN button, should redirect to another component on a different page. However, currently, when I click the button, it loads the data of the o ...

Customizing the color of a select dropdown in Bootstrap using CSS styling

I am having difficulty changing the default blue color for Bootstrap select dropdowns. Despite trying :hover and :active selectors on both option and select, I have not been successful in making the desired changes. Could anyone provide insight regarding ...

Invoking servlet using Ajax

I have created a JSP file with Javascript functions and AJAX calls to invoke a servlet (ReadprojectInfo). <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE ...

Purging information from JavaScript object

Looking for a way to remove just one piece of data from a JavaScript object in my React app. Here's the structure of the object: state = { data: [] } const contactData = { Datas: { name: "william", email: "<a href="/cdn-cgi/l/email-pr ...

Is it possible for Prototype to enhance SVG components?

Having some issues developing an interactive SVG map with Prototype. Extending inline SVG elements seems to be causing problems. Take a look at my code snippet below (path data removed for brevity): <svg id="map" xmlns="http://www.w3.org/2000/svg" xmln ...

What is the best way to switch from rows to cards in Bootstrap version 5.3?

In my current project, Next JS is the framework I am working with. I am faced with the challenge of creating a card layout that adapts based on device size - for smaller devices, I want a plain card with the image on top and text below, similar to Bootstra ...

Creating an installation package for an Electron application on Windows

Is it possible to create a Mac installer for an Electron app using a Windows PC? I have tried running npm make, but it only generates a Windows installer. ...

How can I define the boundaries for the scrolling of a static background image?

Is there a way to limit how far a fixed background image can scroll down a page? Essentially, is it possible to change the background attachment to static when the bottom of the background image is 10px away from the bottom edge of its div container? In t ...

Enhancing Your Model with Additional Details using Angular Formly

I have been utilizing Angular Formly to display forms generated from the JSON response received from services. However, I have a few inquiries. Is it feasible to retrieve data in a field (templateOptions.label, for instance) to include in the model? ...

Showing various information in tab form depending on a specified variable

In my current project, I am developing a user-friendly view that allows users to easily switch between different sets of data. The specific scenario involves a manager overseeing multiple stores with various franchises. Our dashboard presents aggregated re ...

Conceal a Tag with CSS

Looking to conceal a label using CSS <p class="half_form "> <label for="property_bathrooms">Bathrooms (*only numbers)</label> <input type="text" id="property_bathrooms" size="40" class= ...

Send the ID of the checkbox to a PHP file using AJAX

Is it possible to generate a network graph by selecting checkboxes? When I choose one or more checkboxes and click the button, I expect to see a network graph with the selected checkboxes. It almost works, but when I select multiple checkboxes, only one ...

The main menu vanishes when you hover over it after the affix class is activated

I am currently working on creating a horizontal dropdown menu that expands on mouseover and collapses on mouseout under one of the main menu items. The functionality of this effect is working fine, except for one issue. When I scroll down and activate the ...