There is empty space both at the top and bottom of the background image

Figuring out how to make my background image responsive on mobile devices has been a challenge. Whenever I try changing properties in the CSS code, new issues emerge. Sometimes, the image becomes responsive, but then I notice white space appearing at the top and bottom of the image. Other times, no matter what changes I make, the image still refuses to adapt responsively.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
    body {
        background-image: url('hand.jpg');
        background-size: cover;
        background-position: center center;
        background-repeat: no-repeat;
        background-attachment: fixed;

    }
</style>

Answer №1

The task at hand is to make sure that the image fills the entire viewport, regardless of the body's size.

Considering the issue with background-attachment: fixed on IOS, this code snippet takes a different approach by placing the background on a pseudo element which remains fixed to the viewport as you scroll, ensuring it stays in place.

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
  body::before {
    content: '';
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    background-image: url('https://i.sstatic.net/XWDio2gc.jpg');
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    z-index: -1;
  }
</style>

<body>
  line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br></body>

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

The CSS class in jQuery Mobile is not being properly displayed on the input field

I'm having trouble getting the .ui-input-text class to apply to the input field, despite using the jQuery Mobile class for text inputs. The border radius is not changing unless I manually add it in with CSS code. Here's my current setup: <!DO ...

The acceleration increases following a pause and resume

My goal is to design a media bar component with features like pause and resume. The red vertical line moves from left to right, and when I press the pause button, it halts the movement; while pressing the play button resumes the animation. I have also cre ...

Send information through a form contained within a jQuery modal pop-up

This question has been brought up before, but none of the solutions provided seem to be effective for my situation. Within a jQuery UI dialog box, I have a form: <!-- Dialog: Register new user--> <div id="dialogForUser" title="Register new user" ...

How to perfectly position various height <a> elements in a WordPress mega menu

I'm currently working on a WordPress mega menu with four columns, each with a heading or top menu item. The headings should have a gradient border at the bottom and differ in length. However, I'm facing an issue where some headings span two lines ...

Tracking changes in real time and calculating the sum with AJAX, PHP, and MySQL for efficient processing

Initially, I kindly request you to read this until the end. I am in dire need of assistance with my problem as I have searched for solutions but still remain clueless. https://i.sstatic.net/DAb1q.png Referring to the image provided, the first 'Produ ...

Using GSAP to merge texts from left to right

I'm curious about the animation technique used by the developer to make his name come together from right to left. I'd love to try implementing it myself to improve my CSS skills. It looks like GSAP ScrollTrigger was involved in this animation, ...

Struggling to get Scrollspy to work with list group in Bootstrap 4, but it's just not cooperating

I'm having trouble with a scrollspy where the elements I am spying on and their content appear separate, instead of being aligned. #list-example{ position: relative; overflow-y: scroll; height: 300px;} <script src="https://cdnjs.cloudflare ...

What is the best way to determine the size of the URLs for images stored in an array using JavaScript?

I am working on a project where I need to surround an image with a specific sized 'div' based on the image's dimensions. The images that will be used are stored in an array and I need to extract the height and width of each image before disp ...

Guide to positioning a div in the center while implementing animations through transition and transformation using scale

Creating a popup for my React app without relying on external libraries is my current project. I am experimenting with using transition and transform with scale to size the popup dynamically based on screen size and content, then center it on the screen. ...

Creating header menus with section labels in Windows 8 Metro can be easily accomplished by utilizing Javascript

Is there a way to create a navigation menu in Windows 8 Metro JavaScript that includes header menus and section labels, similar to the example shown below? ...

Leverage JSON data in JavaScript

Having some trouble figuring out how to incorporate JSON values into my JS script. Any assistance would be greatly appreciated as I am still new to this! Below is the snippet of code where I need the values (lat and lon) to be inserted: var map; functio ...

I need to adjust the alignment of the text within my list item that includes a time element

I am struggling with aligning my elements vertically downward as the text following the "-" appears disorganized. I attempted to enclose the time tags within div elements and align them so that they evenly occupy space, but this approach did not work. Ins ...

What is the best way to center an <h1> element and align a <p> element to the right within a single div?

My goal is to create a div using Bootstrap 4 that contains an <h1> aligned in the center, with a <p> tag aligned to the right. You can see exactly what I'm aiming for here: https://i.sstatic.net/dhgqX.jpg I am looking for a solution using ...

Customize Nivo LightBox to apply to only one video separately on the page

I've recently implemented the Nivo LightBox script on my website for a video popup effect. However, as a newcomer to jQuery, I'm struggling to customize the lightbox to only trigger when clicking a specific link for my video, rather than every li ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

Using Jquery to sequentially add a class to individual <li> elements in a list, with a delay of 5 seconds for each addition

Currently, I am working on a website that requires me to assign a class of 'active' to a series of <li> elements. The number of <li> elements varies - sometimes there are six, other times 17. This needs to be adaptable and dynamic. ...

Stack the text on top of each other beside an image

Here is some HTML code that I am working with: <div class="row"> <div col-md-9> <div> <img class="img-valign" src="an image"> <span class="text1" Style="font-size: 8 ...

I noticed that after updating my notification count, there is an unexpected gap in the box. How can I go about fixing this issue?

Would it be helpful to include an image to illustrate my point? I am trying to remove the extra space on the left of the numbers that appear after clicking on a notification to mark it as read and update the notification count. Below is the code snippet I ...

What options do I have for sorting through my inventory using the search feature?

Having some trouble setting up isotope filtering on my search bar. I have managed to get the Isotope function working on checkboxes, but for some reason, my search bar isn't functioning as expected. I found a solution online for filtering results bas ...

What is the best way to conceal a menu that automatically scrolls to the content when it is clicked?

Below is a Codepen with a menu that isn't behaving as expected. var menu = document.querySelector('.nav__list'); var burger = document.querySelector('.burger'); var doc = $(document); var l = $('.scrolly'); var panel = $ ...