Tips on adjusting the height of a background image based on the content of the page

Struggling with getting my background image to adjust based on page content size. The app has a main div and sidebar navigation. Background styles are set for the main div:

const styles = makeStyles(theme => ({
  backgroundStyle: {
    flexGrow: 1,
    padding: theme.spacing(3),
    height: '100vh',
    textAlign: 'center',
    backgroundImage: `url(${Background})`,
    backgroundRepeat: "no-repeat",
    backgroundPosition: "center center",
    backgroundSize: "cover",
    backgroundAttachment: "fixed",
    [theme.breakpoints.down('md')]: {
      height: '100%'
    }
  }

height: '100vh' works until the navigation item extends beyond view height, cutting off the image. Setting height: '100%' solves it but doesn't fill screen when content is small. Issue persists in responsive view.

Conditionals were added:

return <main className={navListItem === 'Topic1' ? backgroundStyle : backgroundStyle2 }>

This solution feels repetitive as it introduces backgroundStyle2 class duplicating same style but with '100%' height instead of '100vh'.

Looking for a more efficient method to change background image based on content size without using fixed heights. Any suggestions?

Answer №1

To ensure a height of at least 100vh while also allowing the height to expand based on content if it exceeds the viewport height, apply the following CSS: minHeight:100vh; height:100%

const styles = makeStyles(theme => ({
  backgroundStyle: {
    flexGrow: 1,
    padding: theme.spacing(3),
    height: '100%',
    minHeight:'100vh',
    textAlign: 'center',
    backgroundImage: `url(${Background})`,
    backgroundRepeat: "no-repeat",
    backgroundPosition: "center center",
    backgroundSize: "cover",
    backgroundAttachment: "fixed",
    [theme.breakpoints.down('md')]: {
      height: '100%'
    }
  }

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

What are some methods for enabling communication between two sibling components without altering the parent's state?

Recently diving into ReactJS, I encountered an issue that's been puzzling me. I have a Parent component with two child components - List and Map, each containing their own child items. What I'm trying to achieve is to change the style of the corr ...

Having trouble navigating to the bottom of a VUEJS app?

I've been working on developing a chatbot app that utilizes a REST API to stream content. While the functionality of the app is running smoothly, I have encountered an issue with the scroll to bottom feature. Instead of automatically scrolling to disp ...

Ran into a security issue while implementing Web3 in a React project

When attempting to incorporate web3 into my React project, I am encountering an nth-check vulnerability while trying to install web3 packages. Despite researching the issue, I discovered that this is a common problem that can typically be ignored as it wil ...

Error: Cannot assign type 'null' to click event type Array. I have not been able to locate a solution to my issue in other related queries

Can someone assist me with my latest Angular project? I encountered an issue with the (click)="onOpenModal(null, 'add')" function call. Since it's meant to open a modal without an existing employee, I passed in null as a placeholde ...

Express app unable to receive data from Ajax request

I'm currently in the process of developing an application with React and Express. The issue I'm facing is that when I make an Ajax request from a React component to a specific Express route, the data doesn't seem to be reaching the route han ...

Guide on integrating and managing two Reactjs applications within a single web application on Azure Portal

I am currently trying to deploy two ReactJs apps to the WebApp service on Azure portal. However, I have encountered an issue where only the default app (located at / path) is working correctly, while the other app (/admin) is not functioning properly. Both ...

There appears to be some additional bottom padding in the text on React Native for

Having trouble aligning my two text components to the baseline on Android. I've tried using includeFontPadding: false, which worked for the top spacing but not for the bottom. Can anyone assist me with this issue? Check out the problem in this snack: ...

Tips for retrieving values from a field prior to form submission using mui and formik

I need to access the value of a field before submitting the form in order to verify it. I tried using the onChange event for this purpose, but the field remains empty and the value is not displayed. My stack includes Mui and Formik. This is the initial se ...

What is the most effective way to reset the state array of objects in React JS?

I'm facing a challenge in resetting an array of objects without having to rewrite the initial state value again. Initial state const [state, setState] = useState([ {name: 'abc', age: 24}, {name: 'xyz', age: 20} ]) Is there a metho ...

Node and Express are correctly logging the response, however the React frontend is logging an entirely different message

Currently, I am in the process of developing a form for an eCommerce platform that allows the administrator/owner to upload images to Cloudinary. At the moment, the image is being successfully uploaded to Cloudinary. Upon logging the response (using conso ...

Can you explain the meaning of this compound CSS selector?

.btnBlue.btnLtBlue Is this referring to an element that has both the classes btnBlue and btnLtBlue applied? ...

A guide to integrating cell updates with dropdown selection in the Material-UI Data Grid

When I need to update cells in a column, I usually use 'type: singleSelect' and 'valueOptions' in the Data Grid column definition. const columns = [ { field: "id", headerName: "ID", sortable: ...

Incorrect Alignment of CSS Menu Popout

I am currently working on designing a stylish menu that expands to display the available options, but I am encountering challenges with the CSS. Specifically, I am struggling to make the sub-menu pop out from the city name seamlessly. CSS ul { margin : 8 ...

I'm looking to keep the footer anchored at the bottom without using the absolute positioning

I am currently developing a website and have implemented a wrapper for the footer. My goal is to create a sticky footer, but when I minimize the screen, the footer ends up overlapping with the content and header. #footerWrapper { height: 177px; bottom: ...

Is it possible to display a title tooltip only when the CSS ellipsis feature is active in a React component?

Issue: I am trying to find a neat solution to display a title tooltip on items that have a CSS ellipsis applied, within a React component. Approaches Attempted: I created a ref, but it only exists after componentDidUpdate. Therefore, I force an update w ...

Adding a disabled internal Style node to the HTML5 DOM: A simple guide

According to this, the style tag can be turned off using the disabled attribute. I attempted the following: <head> <style>body { color: black; }</style> <style disabled>body {color: red; }</style> </head> <bo ...

The data stored in LocalStorage disappears when the page is refreshed

I'm facing an issue with the getItem method in my localStorage within my React Form. I have added an onChange attribute: <div className = 'InputForm' onChange={save_data}> I have found the setItem function to save the data. Here is ...

Does a <Navigate> exist in the react-router-dom library?

Within the parent component import LoginPage from "pages/admin"; export function Home() { return <LoginPage />; } Inside the child component import { useRouter } from "next/router"; export default function LoginPage() { co ...

How can I prevent a CSS class from being rendered in a specific view within MVC?

My knowledge of CSS is quite limited and I am fairly new to it. In my MVC application, I am using a theme that relies on Bootstrap. In the _layout view of my application, I have the following code: <div class="container body-content"> @Html.Par ...

Error: 'env' is not a valid command, either internally or externally, in the context of Next.js

As I was working on a project that I had cloned from a GitLab repository, I encountered an error while trying to run it locally. npm run dev > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d74797878677c306a787f307c6d6d5d2 ...