How to display only a portion of a background image on different devices - showing half on tablets and 1/4 on phones

Objective I want to display a background image at 100% width on a computer or laptop browser, showing only half of the image when cut vertically on a tablet, and one fourth of the image when cut vertically on a phone.

What I've Attempted I tried using clip-path in CSS, but it enabled horizontal scrolling which is not desired.

Browser View

Tablet View

Phone View

On tablet and phone, the remaining part of the image should be hidden via scroll. Only vertical scrolling should be allowed for the background.

Answer №1

To make this happen, utilize media queries along with the background-size property.

/* For mobile devices */
body {
  background: url(https://i.sstatic.net/np7c0.png) no-repeat;
  background-size: 400%;
}

/* Adjustments for tablets and larger screens */
@media only screen and (min-width: 600px) {
  body {
    background-size: 200%;
  }
}

/* Optimized for desktop and wider screens */
@media only screen and (min-width: 768px) {
  body {
    background-size: 100%;
  }
}

Take a look at the live demo here: https://jsfiddle.net/zxqcgkLp/


Please keep in mind that you may need to adjust your breakpoints based on your specific requirements. CSS-Tricks has put together a helpful resource detailing common device breakpoints:

https://css-tricks.com/snippets/css/media-queries-for-standard-devices/

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

Learn how to create a visually stunning CSS menu by centering a background image from the website cssmenumaker

I found a ready-made dropdown menu on . The menu is working well, but I would like to center the buttons. Is there a way to do this? Here is the CSS code: @import url(http://fonts.googleapis.com/css?family=Open+Sans:600); /* Menu CSS */#cssmenu, #cssmenu ...

Eliminate the gap between an image and a div element

I've experimented with display: block; and vertical-align: bottom; Unfortunately, neither of these styles have successfully eliminated the gap between the image and the div. The code I am working with is based on HTML5 and utilizes an external CSS s ...

Unable to delete a dynamically inserted <select> element by using the removeChild method

As someone who is new to coding web applications, I am currently working on a project that involves adding and deleting dropdowns dynamically. Unfortunately, I have run into an issue where the delete function does not work when the button is pressed. Her ...

Server unable to start and error message shown on command prompt

I am having trouble opening the webpage from the code hosted on Github. When I try to run the server, I encounter errors that are displayed in the command prompt as shown below: Code link: https://github.com/mschwarzmueller/nodejs-basics-tutorial/tree/mas ...

Adjusting the position of the floating image on the left side will impact the height of the container

When attempting to execute the code below: ​<div id="container"> //This is a 200x200 image <img src="http://dummyimage.com/200x200/CCC/000" /> </div>​​​​​​​​​​​​​​​​​​​​​​ ...

What could be causing the default dropdown option to vanish once another selection is made?

Check out this GIF to illustrate my query. Is there a way to ensure that when an option is selected, the default choice "Watching" appears in the dropdown menu? For the direct link to the codepen, click here. <!DOCTYPE html> <html lang="en& ...

Having trouble getting AngularJS ngCloak to function properly?

My post category page is supposed to display a message if there are no posts. However, the HTML appears briefly when the page loads and then hides. I thought using ngCloak would solve this issue, but it doesn't seem to be working for me. Here's t ...

What significance do the blank quotes hold in terms of value?

Is it possible to enter multiple values for the "favoriteCities" field since the value is empty? <input name="favoriteCities[]" value=""/> ...

Prevent the AngularJS bar-footer from covering up the page content

I am currently working on developing an app using Ionic and AngularJS. I am facing a challenge with the bar-footer overlapping the content at the bottom of the page. I want the content to be fully visible before the footer appears, without any overlap. Can ...

Utilizing PHP to Insert Data onto an HTML Page

I am trying to use the php post method to insert data into an HTML file, but each time I do so, the data gets added at the bottom of the page. Is there a way to make it appear at the top instead? <?php session_start(); date_default_timezone_set('A ...

Save the dynamically generated content from the div element to ensure it is preserved when the page is refreshed

Currently in the process of developing an exam portal that requires a consistent timer for all page navigation and automatically redirects to submission once the time is up. I am currently utilizing native JavaScript for the timer functionality as shown b ...

the combination of class and type functions as a jquery selector

<button class="actionButton default" type="submit"> Save</button> Can you select this button by both its class and type using a jQuery selector? I attempted the following, but it did not work: var saveBttn = $('.actionButton.default [ty ...

Here is a unique rewrite of the text:"Implementing a feature to upload an image in PHP and AJAX without

Although it may seem repetitive, I have yet to find a solution that perfectly fits my requirements. I am looking to upload an image to a designated folder by simply selecting a file in the file browser, without the use of a submit button (which I have acco ...

Retrieving data from dynamic form components in Angular

I am currently utilizing a back-end API to retrieve flight data, which I then iterate through and exhibit. The layout of my template code is as follows... <form novalidate #form="ngForm"> <div class="flight-table"> <header fxLayou ...

Setting the default value for a useRef<HTMLInputElement> in ReactJs with Typescript

Is there a way to assign a number as the initial value on useRef<HTMLInputElement> without using useState<number>() since the field is a simple counter? Below is my TypeScript code: const MyComponent = () => { const productAmountRef = us ...

Error in JavaScript: Uncaught TypeError - Unable to access the "left" property of an undefined object

This error is really frustrating and I've come across several inquiries regarding this console issue. It's not providing enough information in the chrome console for me to effectively troubleshoot. /** * Adjustment of dropdown menu positio ...

Pressing the Button Increases the Counter, However the Counter Resets After

My goal is to create a basic counter with increment and reset buttons. When I click on the increment button, the counter properly goes from 0 to 1 (I can confirm this by checking the console log in Chrome). The issue arises when, after incrementing, the c ...

Div-based table design

I'm facing an issue that I can't seem to resolve on my own, so if anyone has any advice or solutions, I would greatly appreciate it: ________________________________________________________________________________ | body ...

How can I use JavaScript to find a keyword on a webpage that is not located within an <a> tag or its href attribute?

I'm on a mission to locate a specific keyword within a webpage. Sounds simple, right? Well, here's the tricky part - I need to disregard any instances of this keyword that are nested within an <a> tag. For example: <p>Here is some s ...

Repairing my CSS with jQuery fullpage.js

My CSS on needs fixing. The word Obert is not aligning properly in the section. Interestingly, everything works fine without Javascript: I suspect the wrapper divs created by the plugin are causing the issue. Can someone lend a hand, please? ...