如何放大material-ui iconButtons中的SVG图标?

Jun*_*Yin 18 css svg reactjs material-design material-ui

有没有人使用react.jsMaterial UI库构建网页?我应该如何调整图标大小?这是一个svg图标.我刚刚构建了一个"创建新"组件,它是一张材料纸,顶部有一个内容添加按钮.这是代码.

import React from 'react';
import Paper from 'material-ui/lib/paper';
import ContentAdd from 'material-ui/lib/svg-icons/content/add';
import IconButton from 'material-ui/lib/icon-button';


const styleForPaper = {
  width: '96vw',
  height: '20vh',
  margin: 20,
  textAlign: 'center',
  display: 'inline-block',
};

const styleForButton = {
  'marginTop': '7vh',
};


const PaperToAddNewWidgets = () => (
  <div>

    <Paper style={styleForPaper} zDepth={2}>

    <IconButton
        style={styleForButton}
        touch={true}
        tooltip="Add New Widget">

    <ContentAdd/>

    </IconButton>

    </Paper>
  </div>
);

export default PaperToAddNewWidgets;
Run Code Online (Sandbox Code Playgroud)

看起来不错(确保以全尺寸查看),但图标太小.然后我打开了chrome dev工具,看到了以下的html代码.

<div style="background-color:#ffffff;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;box-sizing:border-box;font-family:Roboto, sans-serif;-webkit-tap-highlight-color:rgba(0,0,0,0);box-shadow:0 3px 10px rgba(0,0,0,0.16),
         0 3px 10px rgba(0,0,0,0.23);border-radius:2px;width:96vw;height:20vh;margin:20px;text-align:center;display:inline-block;mui-prepared:;" data-reactid=".0.2.0.1.0"><button style="border:10px;background:none;box-sizing:border-box;display:inline-block;font:inherit;font-family:Roboto, sans-serif;tap-highlight-color:rgba(0, 0, 0, 0);cursor:pointer;text-decoration:none;outline:none;transform:translate3d(0, 0, 0);position:relative;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;padding:12px;width:48px;height:48px;font-size:0;margin-top:7vh;mui-prepared:;-webkit-appearance:button;" tabindex="0" type="button" data-reactid=".0.2.0.1.0.0"><div data-reactid=".0.2.0.1.0.0.0"><span style="height:100%;width:100%;position:absolute;top:0;left:0;overflow:hidden;mui-prepared:;" data-reactid=".0.2.0.1.0.0.0.0"></span><div style="position: absolute; font-family: Roboto, sans-serif; font-size: 14px; line-height: 32px; padding: 0px 16px; z-index: 3000; color: rgb(255, 255, 255); overflow: hidden; top: -10000px; border-radius: 2px; opacity: 0; left: -44px; transition: top 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, transform 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms, opacity 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; box-sizing: border-box; -webkit-user-select: none;" data-reactid=".0.2.0.1.0.0.0.1:0"><div style="position: absolute; left: 50%; top: 0px; transform: translate(-50%, -50%); border-radius: 50%; transition: width 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, height 0ms cubic-bezier(0.23, 1, 0.32, 1) 450ms, backgroundColor 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms; width: 0px; height: 0px; background-color: transparent;" data-reactid=".0.2.0.1.0.0.0.1:0.0"></div><span style="position:relative;white-space:nowrap;mui-prepared:;" data-reactid=".0.2.0.1.0.0.0.1:0.1">Add New Widget</span></div><svg style="display:inline-block;height:24px;width:24px;transition:all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;fill:rgba(0, 0, 0, 0.87);mui-prepared:;-webkit-user-select:none;" viewBox="0 0 24 24" data-reactid=".0.2.0.1.0.0.0.1:2:$/=10"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" data-reactid=".0.2.0.1.0.0.0.1:2:$/=10.0"></path></svg></div></button></div>
Run Code Online (Sandbox Code Playgroud)

使用chrome dev工具,我修改了svg的svg图标大小和viewbox属性,并在浏览器中使图标变大.但我不知道如何在代码中调整图标大小.如果我编写一个CSS文件来修改svg,如果有多个svg元素,那将会有问题.

Гео*_*нов 24

做这个

<SomeIcon className="svg_icons"/>

.svg_icons{
  transform: scale(1.8);
}
Run Code Online (Sandbox Code Playgroud)

只是使用规模:D它的工作原理

  • 问题是宽度不随其增加而导致重叠 (3认同)

Bra*_*don 17

你必须在iconStyle道具中设置图标的大小<IconButton>.以下示例来自material-ui docs.

根据我的经验,如果你只设置高度或宽度,没有任何反应 - 只有当你将高度和宽度设置为相同的值时才会起作用.

import React from 'react';
import IconButton from 'material-ui/IconButton';
import ActionHome from 'material-ui/svg-icons/action/home';

const styles = {

  largeIcon: {
    width: 60,
    height: 60,
  },

};

const IconButtonExampleSize = () => (
  <div>
    <IconButton
      iconStyle={styles.largeIcon}

    >
      <ActionHome />
    </IconButton>
  </div>
);
Run Code Online (Sandbox Code Playgroud)

  • “material-ui”似乎不再支持“iconStyle”属性。这使得这个接受的答案不正确。 (4认同)
  • 哇,试图设置 Icon 的样式本身,不知道它是这样工作的。谁设计的?叹。 (3认同)

小智 11

使用最新版本的 material-ui (3.1.0),您可以更改 IconButton 的 padding 和 SvgIcon 的 fontSize 以更新外观。

const styles = theme => ({
  smallButton: {
    padding: 6
  },
  largeButton: {
    padding: 24
  },
  largeIcon: {
    fontSize: "3em"
  },
  input: {
    display: "none"
  }
});    

function IconButtons(props) {
  const { classes } = props;
  return (
    <div>
      <IconButton className={classes.smallButton} aria-label="Delete">
        <DeleteIcon fontSize="small" />
      </IconButton>
      <IconButton aria-label="Delete">
        <DeleteIcon fontSize="large" />
      </IconButton>
      <IconButton className={classes.largeButton} aria-label="Delete">
        <DeleteIcon className={classes.largeIcon} />
      </IconButton>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

现场演示


Ala*_*ALI 9

我在使用上一个 React 版本(今天是 v16.13.1)和 Material-ui(今天是 v4.9.14)时遇到了同样的问题。

我是怎么解决的?

只需将此样式添加到图标按钮

MyIconButton: {
    '& svg': {
      fontSize: theSizeIWant
    }
}
Run Code Online (Sandbox Code Playgroud)

完整示例

import React from "react";
import { makeStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import DeleteIcon from '@material-ui/icons/Delete';

const useStyles = makeStyles((theme) => ({
  deleteIcon1: {
    '& svg': {
      fontSize: 25
    }
  },
  deleteIcon2: {
    '& svg': {
      fontSize: 50
    }
  },
  deleteIcon3: {
    '& svg': {
      fontSize: 75
    }
  },
  deleteIcon4: {
    '& svg': {
      fontSize: 100
    }
  }
}));


export default function App() {

  const classes = useStyles();

  return (
    <div className="App">

      <h1>fontSize: 25</h1>
      <IconButton className={classes.deleteIcon1}>
        <DeleteIcon />
      </IconButton>

      <h1>fontSize: 50</h1>
      <IconButton className={classes.deleteIcon2}>
        <DeleteIcon />
      </IconButton>

      <h1>fontSize: 75</h1>
      <IconButton className={classes.deleteIcon3}>
        <DeleteIcon />
      </IconButton>

      <h1>fontSize: 100</h1>
      <IconButton className={classes.deleteIcon4}>
        <DeleteIcon />
      </IconButton>

    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

结果将是:

在此处输入图片说明

实时运行代码

我创建了这个 codeandbox

希望这可以帮助!


qiu*_*qiu 8

不。大多数其他答案都不令人满意并且实践不佳,至少在 2021 年是这样。这个答案在此处文档中得到了字面上的回答,不需要任何 CSS 技巧即可开始工作。

在 MUI 中,ButtonandIconButton组件本质上只是将子Icon元素嵌套在<a>or <Link>(react-router) 元素中的包装器。他们对大小没有任何内在控制。因此,要增加按钮大小,只需增加使用该属性的Icon子项的大小即可。这将自动使按钮变大。IconButtonfontSize

例如,要使IconButton以下更大:

<IconButton>
  <Icon />
</IconButton>
Run Code Online (Sandbox Code Playgroud)

我们只是直接增加Icon孩子的大小。

<IconButton>
  <Icon fontSize="large" />
</IconButton>
Run Code Online (Sandbox Code Playgroud)

但是,这是您可以使用该fontSize属性指定的最大尺寸。为了更大,我们必须font-size手动配置 CSS属性。

<IconButton>
  <Icon style={{ fontSize: 60 }} />
</IconButton>
Run Code Online (Sandbox Code Playgroud)

简单如。

编辑:显然,无论出于何种原因,Button组件都有一个size允许控制其大小的属性,但IconButton组件没有。我不知道为什么 MUI 如此不一致......


小智 5

对于当前版本的 MUI (v5.8.5),图标上有一个fontSize支持“小”、“中”、“大”不同大小的设置,因此您可以执行以下操作:

<HomeIcon fontSize="small" />
Run Code Online (Sandbox Code Playgroud)

如果这些设置大小之一不符合您的需求,如果您希望它与其所在容器的字体大小相匹配并且没有自己的单独大小,则还可以选择使用“继承”。

最后,您可以使用sxMUI 放置在其所有组件上的属性,并在其中接受允许fontSize您直接设置图标大小的属性。例如:

<WhatshotIcon sx={{ fontSize: 140 }} />
Run Code Online (Sandbox Code Playgroud)

https://mui.com/material-ui/icons/#size