mui无法覆盖Rating组件的大小样式

huangapple go评论60阅读模式
英文:

mui cannot override size style to Rating component

问题

I cannot override default style of MUI Rating component. No matter what it defaults to medium 24 fontSize, and does not increase it. What had I done wrong?

import { Rating } from '@mui/material';

export const Stars = () => {
  return (
    <>
      <Rating name="size-large" defaultValue={2} size="large" />
      <Rating
        name="size-large"
        value={4.5}
        precision={0.5}
        sx={{ fontSize: '48px' }}
        size="large"
        readOnly
      />
    </>
  );
};

package.json

"dependencies": {
    "@emotion/react": "^11.10.6",
    "@emotion/styled": "^11.10.6",
    "@mui/icons-material": "^5.11.0",
    "@mui/lab": "^5.0.0-alpha.115",
    "@mui/material": "^5.11.9",
    "@tanstack/react-query": "^4.22.0",
    "axios": "^1.2.2",
    "formik": "^2.2.9",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.6.2",
    "styled-components": "^5.3.6",
    "yup": "^0.32.11"
  }

mui无法覆盖Rating组件的大小样式

英文:

I cannot override default style of MUI Rating component. No matter what it defaults to medium 24 fontSize, and does not increase it. What had I done wrong?

import { Rating } from &#39;@mui/material&#39;;

export const Stars = () =&gt; {
  return (
    &lt;&gt;
      &lt;Rating name=&quot;size-large&quot; defaultValue={2} size=&quot;large&quot; /&gt;
      &lt;Rating
        name=&quot;size-large&quot;
        value={4.5}
        precision={0.5}
        sx={{ fontSize: &#39;48px&#39; }}
        size=&quot;large&quot;
        readOnly
      /&gt;
    &lt;/&gt;
  );
};

package.json

&quot;dependencies&quot;: {
    &quot;@emotion/react&quot;: &quot;^11.10.6&quot;,
    &quot;@emotion/styled&quot;: &quot;^11.10.6&quot;,
    &quot;@mui/icons-material&quot;: &quot;^5.11.0&quot;,
    &quot;@mui/lab&quot;: &quot;^5.0.0-alpha.115&quot;,
    &quot;@mui/material&quot;: &quot;^5.11.9&quot;,
    &quot;@tanstack/react-query&quot;: &quot;^4.22.0&quot;,
    &quot;axios&quot;: &quot;^1.2.2&quot;,
    &quot;formik&quot;: &quot;^2.2.9&quot;,
    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-router-dom&quot;: &quot;^6.6.2&quot;,
    &quot;styled-components&quot;: &quot;^5.3.6&quot;,
    &quot;yup&quot;: &quot;^0.32.11&quot;
  },

mui无法覆盖Rating组件的大小样式

答案1

得分: 1

您可能没有调用此组件,而是看到了其他组件。您的代码将第一个星星的默认值设为2,但您的屏幕截图显示值为440/550。

或者您可能已经使用了MUI Ratings的样式覆盖全局样式表,阻止了内联的sx样式生效。

您可以右键单击检查您期望具有font-size: 48px的星星,然后查看它实际的字体大小来自哪个CSS样式表。

使用您的代码和依赖项在简单的沙箱中,我看到了预期的较大星星。

英文:

You're probably not calling this component and seeing some other component. Your code has the first stars with a default value of 2, but your screenshot shows the value at 440 / 550.

Or you may have an MUI Ratings style override or global stylesheet preventing the inline sx styling from taking hold.

You can right click Inspect the stars you are expecting to have a font-size: 48px then see what CSS stylesheet it's getting it's actual font-size from.

Using your code and dependencies in a simple sandbox I see the larger stars as expected.

huangapple
  • 本文由 发表于 2023年2月19日 01:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定