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

Guide to building a React site that showcases customized content from users in a visually appealing way

Imagine you're designing a website where users can share their writing, whether it's a blog, a platform like Medium, or even StackOverflow. You want to allow users to format text with bold and italic styles, insert pictures within the text, and m ...

Is there a way to attach a CSS class to a BoundField in order to locate it using jQuery?

I need to assign a specific class name to certain BoundFields within the GridView control. This way, after the GridView has been populated with data and displayed, I would like to have something similar to the following: <td class="Tag1">Some data c ...

In the game of rock paper scissors, the icons become unclickable once you achieve a score of 5

I have created a Rock Paper Scissors game where the score resets if either the user or computer reaches 5 points, but I want to prevent any further play after hitting 5. Currently, the game displays a message and reloads after 5 seconds, during which tim ...

Ref cannot be assigned to function components. Trying to reference a ref in a function component will not work

Having trouble passing a ref in the MenuItem component while using react-beautiful-dnd. I tried creating a HOC with React.forwardRef but it didn't work. Any help in fixing this issue would be greatly appreciated. Error: https://i.stack.imgur.com/bLIN ...

Several components utilizing a popup module

I have created a modal popup example where scrolling is disabled in the background but enabled within the pop-up itself. Check out my demo here: View Demo My challenge lies in implementing a grid of images on the website: Take a look at the type of grid ...

Roundabout Navigation Styles with CSS and jQuery

My goal is to implement a corner circle menu, similar to the one shown in the image below: https://i.stack.imgur.com/zma5U.png This is what I have accomplished so far: $(".menu").click(function(){ $(".menu-items").fadeToggle(); }); html,body{ colo ...

React Router v6 is throwing an error stating that within the <Routes> component, all children must be either a <Route> or <React.Fragment>

While the code snippet below may have worked perfectly fine in React Router v5, it's causing an error in React Router v6. Error: [Player] is not a <Route> component. All component children of <Routes> must be a <Route> or <React ...

How can we effectively implement alert notifications for validating image sizes and formats prior to uploading?

code playground : https://codesandbox.io/s/quizzical-lamport-ql5ep I'm encountering an issue with the code provided in the CodeSandbox link. https://i.sstatic.net/xg3aK.png I've attempted to resolve this issue using various methods, but unfortu ...

Having trouble adjusting the size of a Font Awesome icon with React, Tailwind, and Next.js?

Struggling to adjust the size of a Font Awesome icon while learning React and Tailwind. Despite trying various options like size="sm" and h-px w-px in Tailwind, I can't seem to make any changes. Can anyone offer some assistance? Currently using Next. ...

Unable to change background image using jQuery

Snippet: const imageUrl = 'img/star.png'; $("#venueId-"+nodeId).css('background','url("'+imageUrl+'") no-repeat top right'); This piece of code is the start of a function that triggers upon clicking. The goal is to ...

What methods can I employ to limit the MUI TimePicker to specific time options?

I am currently utilizing the MUI TimePicker component. In a specific scenario, I need to limit selected time options to either the top of the hour (00 min) or half past the hour (30 min). Is it feasible to achieve this functionality? While I understand t ...

Create custom styles for Android applications using CSS or themes programmatically

As I work on developing an application, I am interested in implementing a unique approach... To retrieve a CSS file from the server and apply it to the activity layout. To create a string file for styling or theming and integrating it into the layout. I ...

What is the best way to initiate a function upon each page load?

(Apologies in advance for any English mistakes) Hello everyone, I am currently developing a simple Chrome extension to edit certain graphics and text fields on a website (Freshdesk) that cannot be modified directly on the site due to proprietary code. ...

Encountering issues with rendering in React JS when utilizing state variables

I've been attempting to display content using the render method in React JS, but for some reason, the onClick code isn't executing. I'm currently enrolled in a course on Udemy that covers this topic. import React, { Component } from 'r ...

Utilizing a function as a prop with varying parameter types in React using Typescript

I am encountering an issue while attempting to pass a function that updates state in React. VSCode is prompting me with a typing problem. The error message states Type '(value: string) => void' is not assignable to type '(value: string | ...

Achieve perfect alignment of an HTML table by positioning it precisely 36 pixels away from the right edge of the window

I'm struggling to achieve a table that is precisely 36px away from both the left and right edges of the browser window, regardless of CSS. While I can easily set the left margin, getting the exact right margin seems impossible as the `margin-right` CS ...

Tips on filtering out empty rows when sorting in a material ui datagrid

I need some help with filtering out empty rows in the response I am receiving. I am currently using material UI's data grid component and would like to know how to exclude these empty rows when sorting. Any suggestions or advice would be greatly appre ...

Having trouble setting default values with React Hook Forms when using Material UI Checkboxes with FormControlLabel

Using React Hook Forms, I am encountering an issue with retrieving the Checkbox value, which is initially set to true. Despite setting the checkbox value to true by default and registering the component successfully, if I do not manually interact with it, ...

React warning: Make sure every child element in a list has a distinct "key" property assigned

Here is a breakdown of my code that consists of 2 components and index.tsx: NewsFeed.tsx import React, { useState } from 'react' import Feeds from './Feeds' export default function NewsFeed({ news }: any) { const [date, setDate] ...

Horizontal scroll through Bootstrap 4 navigation tabs

I'm currently using the newest Bootstrap 4 nav-tabs feature to create tabs and I'm aiming to keep all the tabs in a single horizontal line that can be scrolled. I've attempted to use various JavaScript plugins, but none of them seem to func ...