LoginSignup
1
2

More than 3 years have passed since last update.

Material-UIのComponentのスタイルを上書きしたいとき

Posted at

Material-UIのComponentのスタイルを上書きしたい!!

Material-UIのAvatar Componentを使用して、縦長の画像を表示しようとしていました。
すると、Avatarのスタイルに、object-fit : 'cover'がデフォルトで適用されており、画像全体が入らず、クロップされてしまいました。

そこで、まずmakeStyleを用いて、スタイルの適用を試みましたが、上書きができませんでした。
そこで、色々調べていると、

createMuiThemeを使用して、スタイルを上書きできることが分かりました。

実際のコードがこちらです。

const theme = createMuiTheme({
    overrides: {
        MuiAvatar: {
        img: {
            objectFit : 'contain',
        },
      },
    },
  })

上記のように、overridesと上書きしたいComponentを Mui+'Component名' で指定すると上書きができます。

<MuiThemeProvider theme={theme}>
      <Avatar variant="square" src={img}>
      </Avatar>
</MuiThemeProvider>

上記のように、上書きを適用させたいcomponentを囲んであげると、objectFit : 'contain'が適用されました!!
上手くいって良かったです。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2