๐Ÿ’ป My Work/๐ŸŽจ Material Design

[Material Design] Material-UI(MUI) : Overriding nested component styles - CardContent padding customize

Jaeseo Kim 2022. 12. 1. 23:57

 

โœจ Overriding nested component styles


 

https://mui.com/material-ui/customization/how-to-customize/

 

How to customize - Material UI

Learn how to customize Material UI components by taking advantage of different strategies for specific use cases.

mui.com

 

๐Ÿ’ก์ค‘์ฒฉ๋œ ์ปดํฌ๋„ŒํŠธ style์„ override ํ•˜๊ธฐ ์œ„ํ•ด์„ 
์ปดํฌ๋„ŒํŠธ์˜ css ํด๋ž˜์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์„œ CSS selector๋ฅผ ์ž‘์„ฑ ํ›„, ์ปค์Šคํ…€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด,

ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ํด๋ž˜์Šค ์ด๋ฆ„์ด MuiSlider-thumb ์ผ ๋•Œ, & .MuiSlider-thumb ๋ผ๋Š” CSS selector๋กœ ์ปค์Šคํ…€ํ•ด์ค๋‹ˆ๋‹ค.

(์ถœ์ฒ˜ : mui)

<Slider
  defaultValue={30}
  sx={{
    width: 300,
    color: 'success.main',
    '& .MuiSlider-thumb': {
      borderRadius: '1px',
    },
  }}
/>

๊ฒฐ๊ณผ (์ถœ์ฒ˜ : mui)

 

 

 

 

CardContent padding customize


์•„๋ž˜์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

import * as React from "react";
import { Card, CardContent, Typography, Box } from "@mui/material";

export default function App() {
  return (
    <Card elevation={5}>
      <CardContent>
        <Box>
          <Typography variant="body1">padding</Typography>
        </Box>
      </CardContent>
    </Card>
  );
}

 

 

์•„! ๊ทธ๋Ÿฐ๋ฐ, ์ €๋Š” padding-top, padding-bottom ์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!

 

padding-bottom ๋ฅผ padding-top๊ณผ ๊ฐ™๊ฒŒ sx ์ธ์ž๋กœ ๋„˜๊ฒจ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿคญ

<CardContent sx={{ paddingBottom: "1.6rem" }}>

 

 

๊ทธ์น˜๋งŒ ๊ทธ๋Œ€๋กœ๋„ค์š”,, 

 

๐Ÿ’ก last child selector ์˜ style์ด padding-bottom ์ด 24px๋กœ ๊ณ ์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
override ๋กœ ์ด๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

last-child customize

๐Ÿ“ &:last-child ์†์„ฑ์„ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
๐Ÿ“ paddingBottom ๋Š” pb ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
<CardContent sx={{ "&:last-child": { paddingBottom: "1.6rem" } }}>
<CardContent sx={{ "&:last-child": { pb: "1.6rem" } }}>

 

 

๊ฒฐ๊ณผ๋Š”?! ์ปค์Šคํ…€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค~~!!!๐Ÿ˜๐Ÿ˜

import * as React from "react";
import { Card, CardContent, Typography, Box } from "@mui/material";

export default function App() {
  return (
    <Card elevation={5}>
      <CardContent sx={{ "&:last-child": { paddingBottom: "1.6rem" } }}>
        <Box>
          <Typography variant="body1">padding</Typography>
        </Box>
      </CardContent>
    </Card>
  );
}