How to style Material-UI's tooltip?

前端 未结 5 1911
栀梦
栀梦 2020-12-14 16:01

How can I style Material-UI\'s tooltip text? The default tooltip on hover comes out black with no text-wrap. Is it possible to change the background, color etc? Is this opti

5条回答
  •  情深已故
    2020-12-14 16:50

    The other popular answer (by André Junges) on this question is for the 0.x versions of Material-UI. Below I've copied in my answer from Material UI's Tooltip - Customization Style which addresses this for v3 and v4.

    Below are examples of how to override all tooltips via the theme, or to just customize a single tooltip using withStyles (two different examples). The second approach could also be used to create a custom tooltip component that you could reuse without forcing it to be used globally.

    import React from "react";
    import ReactDOM from "react-dom";
    
    import {
      createMuiTheme,
      MuiThemeProvider,
      withStyles
    } from "@material-ui/core/styles";
    import Tooltip from "@material-ui/core/Tooltip";
    
    const defaultTheme = createMuiTheme();
    const theme = createMuiTheme({
      overrides: {
        MuiTooltip: {
          tooltip: {
            fontSize: "2em",
            color: "yellow",
            backgroundColor: "red"
          }
        }
      }
    });
    const BlueOnGreenTooltip = withStyles({
      tooltip: {
        color: "lightblue",
        backgroundColor: "green"
      }
    })(Tooltip);
    
    const TextOnlyTooltip = withStyles({
      tooltip: {
        color: "black",
        backgroundColor: "transparent"
      }
    })(Tooltip);
    
    function App(props) {
      return (
        
          
    Hover to see tooltip customized via theme
    Hover to see blue-on-green tooltip customized via withStyles
    Hover to see text-only tooltip customized via withStyles
    ); } const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement);

    Here is documentation on tooltip CSS classes available to control different aspects of tooltip behavior: https://material-ui.com/api/tooltip/#css

    Here is documentation on overriding these classes in the theme: https://material-ui.com/customization/components/#global-theme-override

提交回复
热议问题