Ensure that the background image adjusts appropriately to different screen sizes while maintaining its responsiveness

Currently in the process of developing a single page application using CRA. My goal is to create a hero image with an overlay at the top of the application using a background image. However, I'm facing issues with maintaining the responsiveness of the background image across different screen sizes. I want users to be able to see the entire background image regardless of their screen size.

You can view the desired effect I'm aiming for by following this link: https://www.youtube.com/watch?v=gfzWLbAbcCw&feature=youtu.be

Here is my JSX code:

import React from "react";
import "./styles.scss";

export default function App() {
  return (
    <>
      <div className="container">
        <div className="container-background"> </div>
        <div className="content">
          <div style={{ textAlign: "center", paddingTop: 20 }}>Welcome</div>
        </div>
      </div>
    </>
  );
}

And here is my CSS code:

body {
  margin: 0;
  padding: 0;
}

.container {
  position: relative;
}
.container .container-background {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-size: cover;
  background-repeat: no-repeat;
  background-image: url('https://i.imgur.com/iyFtMNA.jpg');
  opacity: .2;
  height: 1200px;
}
.container .content {
  position: relative;
  z-index: 1;
}

I am struggling with which CSS properties to use to achieve my desired effect. Any suggestions are greatly appreciated!

You can access a debugging sandbox through this link: https://codesandbox.io/s/vigilant-wozniak-t8wmx?file=/src/styles.scss:0-391

Answer №1

+1 for using codeboxsandbox I would implement it like this

React

   <div className="container">
      <div className="hero">
        <img src="https://i.imgur.com/iyFtMNA.jpg" alt="background" />
        <div className="overlay">
          <h1 className="title">Title</h1>
          <h2 className="subtitle">Subtitle</h2>
        </div>
      </div>
      <div className="content">
        <div style={{ textAlign: "center", paddingTop: 20 }}>Welcome</div>
      </div>
    </div>

CSS

* { margin: 0; padding: 0; }
.hero {
  position: relative;
}
.hero img {
  width: 100%;
  opacity: .2;
}
.hero .overlay {
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%);
}
.container .content {
  position: relative;
}

Check out the bg-overlay-example

Answer №2

To achieve this, consider setting a unique image for the responsive version or utilize the background-size: 100% auto; property to ensure the entire image is visible. However, it may be necessary to reduce the height of the respective div element.

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

Maximizing the Potential of Bootstrap 5's Grid System

I'm struggling a bit with the Bootstrap grid system. I want to create 6 divs or containers that span the full width on smaller devices like mobiles, use 25% of the width on medium devices like tablets, and display all 6 in a single row on large deskto ...

Transform JSX into JSON or a string, then reverse the process

I am looking to store the state of a React Component in a database. Json.stringify(myComponent); However, when I attempt to reuse the component using JSON.parse, I encounter Error: Objects are not valid as a React child (found: object with keys {type, k ...

Looks like there was an error with the start script for [email protected]

I am facing an error in my React application's client side directory where node_modules are installed. The app was working fine until 2 weeks ago, but now when I try to run 'npm start', it fails to launch. The error message in the terminal i ...

Fetching response headers object in redux using React.js

Currently, I am using Redux in React.js to fetch the most starred repositories from the past 30 days. However, I now want to implement pagination using the headers provided by the GitHub API. How can I modify my code to extract the headers from the respons ...

What is the process for animating classes instead of ids in CSS?

My webpage currently features a setup similar to this. function wiggle(){ var parent = document.getElementById("wiggle"); var string = parent.innerHTML; parent.innerHTML = ""; string.split(""); var i = 0, length = string.length; for (i; i ...

Establishing a default selection for a react dropdown menu filled with data retrieved from an API request

class Select extends React.PureComponent { constructor(props) { super(props) this.state = { value: this.props.initialValue } this.handleChange = this.handleChange.bind(this) } handleChange(e) { e.persist() ...

Having trouble getting the smooth scroll-behavior to work properly in Tailwind CSS?

I've been working on my portfolio using next.js and tailwind CSS. I've added scroll-behavior: smooth in the globals.css file, and in the Navbar, I used https://i.stack.imgur.com/wqb4t.png I really want to achieve that smooth scrolling effect thr ...

The sliding effect in react-owl-carousel seems to be malfunctioning once new data is dynamically incorporated into the carousel

Hey there, I'm currently working on a React JS project where I am utilizing react-owl-carousel. Initially, I'm loading 5 items through an API call, and then upon clicking the next button, I make another API call to fetch more data. However, I&apo ...

Utilizing Flowtype with enums in a Redux action

I'm currently facing a challenge with implementing Flow in my react/redux application. While defining my actions, I encountered the following: type MessageSentAction = { type: 'MSG_SENT', message: string, } type Action = MessageSentAc ...

struggling to align menu items on both sides of a navbar

I am currently facing an issue with positioning elements in my HTML code. I have a File drop-down that I want to display on the left side, while the rest of the drop-downs should appear on the right side. It seems like my CSS files might be causing some in ...

How to access a shadowroot element using R Selenium

Currently, I'm dealing with web scraping where one of the websites presents a 'Accept cookies' button within a shadow root. To address this issue, I sought assistance on how to click this button in discussions found here: Click on accept co ...

When inner pages in Next.js reload, they display the same content as the home page upon page refresh

During the development of our website using Next Js, we encountered an issue where the home page and inner pages load correctly locally and in the deployed build on AWS S3. However, when refreshing an inner page or opening it in a new tab, the content of ...

Changing the height using JQuery UI resizable() can also affect the width during resizing

Having an issue with resizing here. Using the JQuery .resizable() on a div with specific css: div#resizable { float: left; border: solid 1px white; box-shadow: 0px 0px 0px 1px #F2F2F2; box-sizing: border-box; padding: 15px; } This div cont ...

The placement of the div only shifts in Firefox

Looking to place a small 25px - 25px image at the end of a line of text? Using a DIV (re1DOC) to position the element can make it easier. However, the issue arises when the element displays correctly in IE & Chrome, but not in Firefox. The text shifts slig ...

Trick to Use Custom Fonts in Email Clients with CSS and HTML

With certain email clients not supporting web fonts, most solutions involve using a backup font like Arial that is compatible with the client. However, the issue is that the default web font is needed specifically because it is a barcode font. Are there a ...

Ensuring consistent column height in HTML Tables

Hey there, I'm just starting out with CSS and I have a question about table design. I have three separate tables below and I want to ensure that all the columns in each table are of equal height. Is there an easy way to achieve this? Any suggestions o ...

Error! React is unable to find the window object

I recently added the "react-speech" package to my application in order to incorporate text-to-speech functionality. However, upon importing the package, I encountered an error that has been challenging to resolve despite extensive research. Any assistance ...

The issue with text color not displaying properly within a Material-UI Theme

While designing a color theme using Material-UI, I specified the contrast text as white (#fff). Surprisingly, it seems to work for buttons with the primary color but not for those with the secondary color. I attempted overrides following the suggestions p ...

Password confirmation on the login screen will be displayed in a single line

As someone who is relatively new to HTML/CSS, most of what I have learned has come from this platform or by searching for answers online. I am nearing completion of my assignment, but I am struggling to figure out how to display two label words on the same ...

Guide to integrating google-map-react with Next.js

I can't seem to figure out what's causing the issue. I'm attempting to utilize google-map-react in my Next.js application. I followed the example provided on their npm page almost exactly. Here is the code snippet: import React from "re ...