Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant remove padding-bottom from Card Content in Material UI

When using the Card component from Material UI it seems like the CardContent has a padding-bottom of 24px that i cannot override with the following code. I can set paddingLeft, Right and Top using this method but for some reason paddingBottom won't work.

const styles = theme => ({
  cardcontent: {
    paddingLeft: 0,
    paddingRight:0,
    paddingTop:0,
    paddingBottom: 0,
  },
})

And then applying that style:

<CardContent className={classes.cardcontent}></CardContent>

this is what I see when previewing the elements in the browser:

.MuiCardContent-root-158:last-child {
    padding-bottom: 24px;
}
.Component-cardcontent-153 {
    padding-top: 0;
    padding-left: 0;
    padding-right: 0;
}

I can edit the pixels in the browser to 0. But I cannot figure out how to target MuiCardContent-root-158:last-child and override paddingBottom in my editor.

like image 224
Alexander Avatar asked Jan 17 '19 13:01

Alexander


3 Answers

Here's the syntax for v3 and v4 (v5 example further down):

const styles = {
  cardcontent: {
    padding: 0,
    "&:last-child": {
      paddingBottom: 0
    }
  }
};

Here's a working example demonstrating this:

import React from "react";
import ReactDOM from "react-dom";

import CardContent from "@material-ui/core/CardContent";
import { withStyles } from "@material-ui/core/styles";
const styles = {
  cardcontent: {
    padding: 0,
    "&:last-child": {
      paddingBottom: 0
    }
  }
};

function App(props) {
  return (
    <div>
      <CardContent
        className={props.classes.cardcontent}
        style={{ border: "1px solid black" }}
      >
        <div style={{ border: "1px solid red" }}>My Content</div>
      </CardContent>
    </div>
  );
}
const StyledApp = withStyles(styles)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);

Edit y05z1kko4j

If you look at the CardContent source code, you can find how it defines the default styles:

export const styles = {
  /* Styles applied to the root element. */
  root: {
    padding: 16,
    '&:last-child': {
      paddingBottom: 24,
    },
  },
};

This can be helpful in understanding how to override them.


For those using v5 of Material-UI, here's a v5 example (uses styled instead of withStyles):

import React from "react";
import ReactDOM from "react-dom";
import CardContent from "@mui/material/CardContent";
import { styled } from "@mui/material/styles";

const CardContentNoPadding = styled(CardContent)(`
  padding: 0;
  &:last-child {
    padding-bottom: 0;
  }
`);
function App(props) {
  return (
    <div>
      <CardContentNoPadding style={{ border: "1px solid black" }}>
        <div style={{ border: "1px solid red" }}>My Content</div>
      </CardContentNoPadding>
    </div>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit Remove bottom padding from card

like image 92
Ryan Cogswell Avatar answered Oct 20 '22 11:10

Ryan Cogswell


When setting the padding of Card Content to 0 via a theme override, the following works well:

overrides: {
  MuiCardContent: {
    root: {
      padding: 0,
      "&:last-child": {
        paddingBottom: 0,
     },
    },
  },
},
like image 38
jhthompson Avatar answered Oct 20 '22 11:10

jhthompson


Here's the syntax for Mui.V5

<CardContent sx={{ p:0, '&:last-child': { pb: 0 }}}></CardContent>
like image 8
Akshay Kumar Avatar answered Oct 20 '22 12:10

Akshay Kumar