【问题标题】:How to enlarge the SVG icon in material-ui iconButtons?如何放大material-ui iconButtons中的SVG图标?
【发布时间】:2016-07-28 16:27:30
【问题描述】:

有人使用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;

看起来不错(确保您以全尺寸查看),但图标太小。然后我打开了chrome开发工具,看到了下面的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>

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

【问题讨论】:

  • 如果你使用&lt;i class="material-icons"&gt;&amp;#abc;&lt;/i&gt;,那么它只是更新font-size: NNpx的NN 例如:在jQuery中$(".material-icons").css('font-size', "48px");
  • 阿尔文,感谢您的见解。 “font-size”适用于“abc;”,但不能解决我的问题。我使用了 'material-ui/lib/svg-icons/content/add' 中的 ,它在网页上生成 svg 图标。
  • 我的错,要增加SVG图标,你需要调整&lt;svg style="display:inline-block;height:24px;width:24px;...style={},例如:尝试改变height:48px; width:48px。有一些图标是基于 FONT 的,因为它是 font-sizetweak
  • 澄清一下,material-ui(在反应中)不是基于material design lite。如果您正在寻找 MDL,请查看 react-mdl
  • 谢谢,@Brandon。我将在获得编辑问题的权限后更新问题。

标签: css svg reactjs material-design material-ui


【解决方案1】:

这样做

<SomeIcon className="svg_icons"/>

.svg_icons {
  transform: scale(1.8);
}

只需使用比例:D 就可以了

【讨论】:

  • 问题是宽度不随宽度增长而导致重叠
【解决方案2】:

注意:IconButton Material UI 不再支持 iconStyle 属性,因此该答案已过时

您必须在&lt;IconButton&gt; 中的iconStyle 属性中设置图标的大小。下面的例子from the 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>
);

【讨论】:

  • 哇,尝试设置图标的样式本身,不知道它是这样工作的。那是谁设计的?叹息。
  • material-ui 似乎不再支持 iconStyle 属性。这使得这个公认的答案不正确。
【解决方案3】:

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

我是怎么解决的?

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

MyIconButton: {
    '& svg': {
      fontSize: theSizeIWant
    }
}

完整示例

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>
  );
}

结果将是:

实时运行代码

我创建了this codesandbox

希望这会有所帮助!

【讨论】:

    【解决方案4】:

    没有。至少在 2021 年,大多数其他答案都不令人满意,而且实践不佳。这个答案在 documentation here 中得到了真正的回答,并且不需要任何 CSS hack 即可工作。

    在 MUI 中,ButtonIconButton 组件本质上只是将子 Icon 元素嵌套在 &lt;a&gt;&lt;Link&gt; (react-router) 元素中的包装器。他们对大小没有任何内在控制。因此,要增加按钮大小,只需使用fontSize 属性增加IconButtonIcon 子项的大小。这会自动使按钮变大。

    例如,使下面的IconButton 更大:

    <IconButton>
      <Icon />
    </IconButton>
    

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

    <IconButton>
      <Icon fontSize="large" />
    </IconButton>
    

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

    <IconButton>
      <Icon style={{ fontSize: 60 }} />
    </IconButton>
    

    简单。

    编辑:显然,无论出于何种原因,Button 组件都有一个size 属性,可以控制它们的大小,但IconButton 组件没有。我不确定为什么 MUI 如此不一致......

    【讨论】:

      【解决方案5】:

      使用最新版本的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>
        );
      }
      

      Live demo

      【讨论】:

        【解决方案6】:

        您可以只在 IconButton 元素的子节点上使用 fontSize 属性,即在您的情况下 - ContentAdd 节点。

        <IconButton
                style={styleForButton}
                touch={true}
                tooltip="Add New Widget">
        
            <ContentAdd fontSize="large" />
        
        </IconButton>
        

        有 4 个选项可用 - 继承、默认、小和大。

        来源 - https://material-ui.com/api/icon/#props

        【讨论】:

          【解决方案7】:

          这对我有用 fontSize="大"

          <Badge badgeContent={4} color="primary" >
                 <CameraAltIcon  fontSize="large"   />
          </Badge>
          

          look Props --> fontSize

          https://material-ui.com/api/icon/

          【讨论】:

            【解决方案8】:

            我正在使用 IconStyle 属性来改变 svg 图标的大小。

            <IconButton 
               tooltip="Add New Group" 
               tooltipPosition="top-center" 
               style={{padding: 0, height: 0}} 
               iconStyle={{height: 31, width: 48}} 
               onClick={e => this.openNewList()} 
               disabled={!this.state.newAvailable}>
            
              <ActionHome />
            
            </IconButton>
            

            【讨论】:

              【解决方案9】:

              放大svg有两种方式,一种是override the style with iconStyle,另一种是编辑https://github.com/callemall/material-ui/blob/master/src/SvgIcon/SvgIcon.js中的代码

              注意:ContentAddSvgIcon 导入

              const mergedStyles = Object.assign({
               ..
                height: 24, // <-- change default height
                width: 24,  // <-- change default width
               ..
              }, style);
              
              return (
                <svg
                  {...other}
                 ..
                  style={prepareStyles(mergedStyles)}
                 ..
                >
              

              【讨论】:

                【解决方案10】:

                总有简单的事情要做

                给你的材质ui图标一个类名 并转到您的 css 文件并给出

                height:40px !important and width 40px !impoetant
                

                【讨论】:

                  【解决方案11】:

                  假设您使用 Sass:

                  .divWithTargetSVG {
                    .MuiSvgIcon-root {
                      font-size: Xrem/px; 
                    }
                  }
                  

                  【讨论】:

                    【解决方案12】:

                    当您的全局项目需要它时,您可以为您的 MUI 主题添加一个新变体。

                    components: {
                       MuiSvgIcon: {
                          variants: [
                            {
                              props: { fontSize: 'huge' },
                              style: {
                                fontSize: '5rem',
                              },
                            },
                          ],
                        },
                    }
                    

                    为了让它适用于 TS,您可能需要扩展模块声明:

                    declare module '@mui/material/SvgIcon' {
                      interface SvgIconPropsSizeOverrides {
                        huge: true;
                      }
                    }
                    

                    用法:

                    <HomeIcon fontSize="huge" />
                    

                    我还在dev.to上写了一个小帖子

                    【讨论】:

                      【解决方案13】:

                      如果 className={ classes.classname } , fontSize : '40px' , size= 'large' 等不起作用然后尝试内联样式,您可以根据需要提供高度和宽度尺寸

                      <OpenInNewIcon style={{ height: '18px', width: '18px' }} />
                      

                      我希望它会起作用,

                      【讨论】:

                        猜你喜欢
                        • 2021-11-25
                        • 1970-01-01
                        • 1970-01-01
                        • 2023-03-07
                        • 1970-01-01
                        • 2021-01-27
                        • 1970-01-01
                        • 2021-08-03
                        • 2019-02-24
                        相关资源
                        最近更新 更多