How to responsively position and scale a background image for a full-width div

Here is the situation: I have an SVG with dimensions of 1920*1920 pixels ()

This is what I require:

  1. I intend to use it as a background-image for a div that spans the full width of the screen.
  2. The background image for the div must be responsive.
  3. An essential requirement is that the top of the background should always be visible on screens of all sizes, meaning the background-image should start at the top of the image.
  4. It should not repeat.

(The concept is to display a sea background designed and linked as shown above.)

This is what I've attempted:

body {
  padding: 20px;
}

div{
  height: 1000px;
  background: url(https://signtalkindia.s3.ap-south-1.amazonaws.com/assets/windbg.svg) bottom top;
  background-size: 150%;
  background-repeat: none;
}
<div>
</div>

There are several requirements mentioned in the "What I need" section that are not being met. It should be simpler than I am perceiving it to be, but I would appreciate any assistance. I am using React, but I don't believe that should impact this case.

Answer №1

Here are a couple of errors to note:

  • In the code, bottom and top cancel each other out on the vertical axis with percentages of 100% and 0%, respectively. The horizontal axis remains undefined, making the background shorthand invalid and unable to display the background-image.

  • Instead of using background-size : 100% auto, consider adjusting only the horizontal sizing for better results.

Check out this demo that reflects what I believe you intended to achieve:

body {
  padding: 20px;
}

div{
  height: 1000px;
  background: url(https://signtalkindia.s3.ap-south-1.amazonaws.com/assets/windbg.svg) top center;
  background-size: 100% auto;
  background-repeat: no-repeat;
}
<div>
</div>

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

Applying a Webkit Scroll Bar to a specific element on the webpage

I've been attempting to apply the Scroll Bar webkit styling to a particular element but haven't had any success. Despite searching extensively, I have come across 2 threads on Stack Overflow that do not provide a solution. Most of the available ...

The button now has a stylish 5px border, making it slightly larger in size. However, it seems

How can I add an active class with a 5px border-bottom on a button without increasing the button size by 5px? The box-sizing property is not working for me. Is there a simple solution to achieve this? *, *:after, *::before { -webkit-box-sizing: border ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

Encountered an issue in Typescript with error TS2554: Was expecting 0 arguments but received 1 when implementing useReducer and useContext in React

I've encountered several errors with my useReducers and useContext in my project. One specific error (TS2554) that I keep running into is related to the AuthReducer functionality. I'm facing the same issue with each Action dispatch. I've tri ...

Incorporating an NPM module into a React file: Webpack encounters resolution issues

After reviewing information from this source and here, the process of publishing a react module to NPM and then using it in another project while having the component in the node_modules directory should be as follows: Create and export a module Specify ...

How can you ensure seamless synchronization while logging in a user with ReactJS and retrieving data from the API?

Is there a way to synchronize and run these two functions in succession before calling console.log(user) and setLogin()? The goal is to ensure that all the information retrieved from the API is available in the user context before activating the setLogin() ...

Looking for a solution to align the header properly in your React app?

Struggling with aligning a header? The CSS property of the header is inherited from the app's CSS file. For example, in the image below, "Our Mission" is the header that needs to be left-aligned. Looking for some assistance here. You can find the app. ...

What is the method for displaying map tooltips by default rather than on mouseover?

Currently, I have a script set up to display a map with two markers. Whenever I hover over one of the markers, a popup tooltip appears with location information. My question is, how can I make the information appear by default without needing to hover ov ...

Is it possible to implement browser-specific CSS in Next JS without affecting other browsers?

Will Next JS automatically add all necessary browser prefixes to CSS styles defined with style jsx, or do I need to write the browser supports manually in my styles when using Next JS? transform: translate(50%, 50%); -webkit-transform: translate(50%, 50%); ...

React app experiencing inconsistent loading of Google Translate script

In my React application, I have integrated the Google Translate script to enable translation functionality. However, I am facing an issue where the translator dropdown does not consistently appear on every page visit. While it sometimes loads properly, oth ...

What's the reason behind the absence of applied bootstrap style in jsFiddle?

Link to the code: Click here I'm having trouble understanding why the default style of <ol> from Bootstrap is not being applied in this particular code. Any insights? Here's a snippet of the HTML code taken from the fiddle. <ul> ...

I'm having trouble getting npm to work in the terminal. I made sure to verify that I have the most recent version of

PS C:\Users\acer\Desktop\demo> npm -v PS C:\Users\acer\Desktop\demo> npm run start click here for image reference For some reason, the npm command is not functioning properly in my terminal. I have even ...

Is there a way to make the Material UI drawer scroll along with the page, instead of having the scrolling confined within the drawer

I have set up the material UI Drawer based on the responsive example provided on Material UI's official website. However, on larger screens, I am encountering an issue with scrolling. How can I make the drawer scroll along with the entire page instead ...

React - modeling technique

As I explore some React code in my learning journey, I've encountered the shape method within PropTypes. From what I gather, this method is used to verify if a given value aligns with a specific structure that we define as an argument. However, I&apos ...

Show a loading icon as the data is being fetched

I am currently facing an issue with displaying a loader icon while data is being fetched from an API. Despite my efforts, I have not been successful in showing the loader icon during the data retrieval process. My folder structure looks like this: compo ...

Struggling with showcasing Trips in my React Native project and looking for guidance on the best approach to implement it beautifully

API: app.get('/trip', async (req, res) => { try { const employeeId = req.query.user; const empId = new ObjectID(employeeId); // Retrieving Fleet associated with the driver's ID const fleet = await Fleet.findOne({ a ...

What are the steps for setting up PancakeSwap V2 on a testnet environment?

I am currently testing the Pancakeswap v2 frontend repository on my local host, but I am encountering an issue when trying to switch to the testnet. (https://github.com/pancakeswap/pancake-frontend) I receive an error message when I change the value of NEX ...

Dealing with an empty array in a Next.js React application

While working with React, I encountered an error message stating "error: type is not supported." It seems like this error occurs when one of the array items is empty and the script cannot read that value. Specifically, if blog.author is empty, the syntax u ...

Troubleshooting: Twitter Bootstrap dropdown feature experiencing issues within navigation bar

I am facing an issue with getting the bootstrap dropdown to function properly. I have tested it on my mobile device and noticed that the menu does not drop down as expected. Here is a DEMO Below is the code snippet: <nav class="navbar navbar-inverse ...

Error encountered: A blank page appears when attempting to deploy a React app created with Vite

I've been facing a challenge trying to solve this issue and I'm still unable to find the correct solution. When attempting to deploy my React app created with Vite to Github Pages, all I see is a blank page. However, it works perfectly fine loca ...