Customizing Material-UI Grid: Is it possible to assign a background color solely to the cells, without affecting the surrounding empty spaces?

I've been experimenting with designing a grid system that incorporates a gradient background color. The issue I'm facing is that the background color extends to the empty spaces between the grid cells as well. Is there a way to scope the gradient effect only to the grid cells themselves? Image: https://i.stack.imgur.com/GWedu.jpg

Answer №1

To enhance the visual appeal of your container, consider applying a linear background color and setting background: 'inherit' for each grid item within it. Ensure that the images inside the grid items are displayed without any background color to maintain consistency.

Here is a suggested implementation:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1,
    background:
      "linear-gradient(90deg, rgba(34,193,195,1) 0%, rgba(253,187,45,1) 100%)"
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: "center",
    color: theme.palette.text.secondary,
    background: "inherit"
  }
}));

export default function AutoGrid() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <Grid container spacing={3}>
        <Grid item xs={3}>
          <Paper className={classes.paper}>
            <img
              height="100px"
              alt="cat"
              src="http://assets.stickpng.com/thumbs/584c3c8a1fc21103bb375ba7.png"
            />
          </Paper>
        </Grid>
        <Grid item xs={3}>
          <Paper className={classes.paper}>
            <img
              height="100px"
              alt="cat"
              src="http://assets.stickpng.com/thumbs/584c3c8a1fc21103bb375ba7.png"
            />
          </Paper>
        </Grid>
        <Grid item xs={3}>
          <Paper className={classes.paper}>
            <img
              height="100px"
              alt="cat"
              src="http://assets.stickpng.com/thumbs/584c3c8a1fc21103bb375ba7.png"
            />
          </Paper>
        </Grid>
        <Grid item xs={3}>
          <Paper className={classes.paper}>
            <img
              height="100px"
              alt="cat"
              src="http://assets.stickpng.com/thumbs/584c3c8a1fc21103bb375ba7.png"
            />
          </Paper>
        </Grid>
      </Grid>
      <Grid container spacing={3}>
        <Grid item xs>
          <Paper className={classes.paper}>
            <img
              height="100px"
              alt="cat"
              src="http://assets.stickpng.com/thumbs/584c3c8a1fc21103bb375ba7.png"
            />
          </Paper>
        </Grid>
      </Grid>
    </div>
  );
}

https://codesandbox.io/s/background-color-on-image-84dm7

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

Special symbol for the homepage in React using Material UI design

I'm having trouble using the if statement to add different icons for each ListItem. React seems to be restricting me from doing so. Any suggestions on how I can achieve this? Should I make any changes in my approach? Thank you in advance. <List ...

Utilizing CSS-in-JS to eliminate arrow buttons from a TextField

I'm currently developing a webpage using React and Material-UI. One of the components I have is a TextField element, and I need to remove the arrow buttons that typically appear on number input fields. If I were utilizing CSS, I could easily achieve t ...

What is the best way to activate the onClick function using material-ui settings?

Why is the onClick event not working in material-ui options? <Select native labelId="select-demo" id="status-select" value={value} displayEmpty onChange={handleChangeSelect} > <option aria-label="Non ...

Modify the cursor pointer style in MUI Link using React

Is there a way to customize the cursor pointer style on MUI Link? I've tried using the 'cursor: pointer' style but it doesn't seem to work. Here is my code: <Link style={{ cursor: 'pointer' }} onClick={() => { ...

Changing the appearance of a radio button dynamically upon clicking

I am currently working on a dynamic pickup date form that utilizes radio buttons. My goal is to change the style of the selected value when a user clicks on it. Below is the code I have tried, but it has not been successful: foreach ($period as $day){ ech ...

How to display Autocomplete dropdown list in Material-UI only when user starts typing something?

Is there a way to make Material-UI's Autocomplete dropdown list appear only when the user starts typing? I've integrated the Material-UI Autocomplete component into my project, but I'm facing an issue where clicking on the TextField automati ...

Centered flex item with a flexbox grid underneath

Looking to create a layout with an intro section on the left side and a sidebar on the right within a container. Below the intro section, I want four divs evenly spaced like a grid. But I'm struggling with setting up this grid, possibly due to flexbo ...

How to Delete an Added Image from a Dialog Title in jQuery

I've tried multiple solutions from various sources, but I'm still struggling to remove an image from a dialog. Whenever I attempt to do so, I end up with multiple images instead of just one. It's important to note that I only want the image ...

React Material UI - How to dynamically filter items in an Autocomplete based on a related value

Currently, I am working on creating 2 select boxes using the autocomplete component in Material UI. You can check out the documentation here: https://mui.com/material-ui/react-autocomplete/#checkboxes I have encountered a specific scenario that I am tryin ...

Is there a way to seamlessly inject a stylesheet into a React application without causing any flickering or reloading on the website?

In my React application built with next.js, I am fetching a stylesheet via a GET request and appending it to the webpage. However, whenever this stylesheet is loaded in, the elements impacted by it re-render unnecessarily, even if there are no actual chang ...

What is the best way to create titles with a background?

My goal is to have a title overlay an image with specific width and the text displayed in blocks. To better illustrate, here's an example: I prefer to achieve this effect using CSS; however, I am open to utilizing Javascript if needed. ...

Unable to enter text into input fields while using material-ui-redux-form because of constant re-rendering

I've encountered an issue while using material-ui with redux. Whenever I try to type in the input fields following the example provided at , the typing doesn't seem to work as expected. Upon inspecting the state of the inputs using Chrome Redux ...

I am looking to showcase a series of icons linked together by connecting lines

I have successfully designed the layout and added icons, but I am facing difficulty in creating connecting lines between them. I attempted to utilize CSS borders and pseudo-elements, yet I cannot achieve the desired outcome. If anyone could offer a CSS-ba ...

The CSS styles are not being applied to the PHP code

I seem to have a tangled mess of CSS / PHP / HTML to deal with. The issue I'm facing is that something works on one PHP script but not the other. Here's a snippet of my code: <?php if ($daten->getData_DB_User($get_page_num) != fa ...

Unable to attach a tooltip to a list item

As an Angular developer, I am working on a list element that displays all the cars and I am looking to add a tooltip to enhance its visual appeal. Here is what I have attempted so far: I created a span tag with the class "tooltip" to wrap around the ul el ...

Ways to create a self-contained video viewer

Is it possible to create a self-contained video player similar to jwplayer or the YouTube video player using just HTML, CSS, and JavaScript? I know that I can build a video player by utilizing the video tag along with some custom javascript and css, but ho ...

Switch up the Thumbnail Image based on the selected category

While testing a shopping site I created, I encountered an issue with changing the banners at the top of the page based on the category the user is viewing. This site is hosted on a server on my localhost, not using wordpress by the way. <div class=" ...

Differentiate among comparable values through placement regex

I'm currently tackling a challenge involving regex as I work on breaking down shorthand CSS code for the font property. Here is my progress thus far: var style = decl.val.match(/\s*(?:\s*(normal|italic|oblique)){1}/i); style = style ? style ...

Fix for fixed scrolling in the navigation bar

Having a website that receives traffic from different countries, including Portugal and other non-English speaking places, I decided to add a translation option. However, I encountered an issue with Google's translate feature which displays a banner a ...

Positioning Google Chart in HTML

I'm attempting to arrange three distinct Google pie charts in a horizontal layout. At present, I have applied inline CSS for formatting purposes. Oddly enough, the third chart always ends up on a separate line. Could anyone kindly point out my error? ...