4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Material ui のlabelの色を変えたい!buttonのlabelの色を変えたい!という時の小ワザ

Posted at

material ui のFormControlLabelの色をデフォルトから別の色に変えたいという時や、
MuiButtonのlabelの色を変えたいという時があるかと思うんですが、
そういう場合に使えるワザを紹介します。

環境:
 react@16.8.6
 @material-ui/core@4.1.1

FormControlLabelのlabelの色を変える

material ui のデフォルトはこうです
スクリーンショット 2019-07-10 10.34.05.png

デフォルトのコード

<FormControlLabel
        control={
          <Checkbox checked={state.checkedA} onChange={handleChange('checkedA')} value="checkedA" />
        }
        label="Secondary"
      />

Secondaryという文字を青に変えたいとき

<FormControlLabel
        control={
          <Checkbox checked={state.checkedA} onChange={handleChange('checkedA')} value="checkedA" />
        }
        label={
          <span style={{ color: "blue" }}>
            Secondary
          </span>
        }
      />

label= に中括弧つけて、Secondaryをspanで挟んで色指定してあげれば、

スクリーンショット 2019-07-10 10.42.55.png

色が青に変わりました!

MuiButtonのlabelの色を変えたい!

ボタンを実装して、ボタン自体の色は変えられたけど、どうしてもlabelの色が変わんない・・・
こんなときないですか?

スクリーンショット 2019-07-10 10.53.06.png このボタンのlabelをどうしても白にしたい!

まずはmaterial uiのファイルを作る

theme.jsを作り、createMuiThemeをimportする

アプリのデベロッパーツールでボタンのlabelのところをクリックすると
下図のようにでるので、theme.jsでoverridesの中に書いていく
スクリーンショット 2019-07-10 18.13.37.png

theme.js
import { createMuiTheme } from "@material-ui/core/styles";

export const theme = createMuiTheme({
overrides: {
    MuiButton: {
      label: {
        color: "white",
      },
    },
  },
});

↓白に変わりました!

スクリーンショット 2019-07-10 11.58.44.png

このやり方はスタイルを無理やり変えてる感あるので、
もっといいやり方があれば教えてください。

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?