
:global 修改 antd 样式为何无效
本文旨在帮助您解决在组件内使用:global修改 antd 全局样式未生效的问题。
问题描述
您在组件内使用:global修改 antd 按钮样式,但没有生效。完整代码可参考 https://codesandbox.io/s/fk7jnl 。
解决方案
:global样式并非像您导入的那样导入。您需要显式指定以下内容:
import mystyles from './index.module.css'
...
<button classname={mystyles.mybutton} type="primary" shape="circle">按钮</button>此外,选择器也编写不正确。正确的选择器应该是:
.mybutton:global(.ant-btn-primary) {
/* 注意没有空格,两者不是父子关系 */
background-color: red !important;
}替代方案
您还可以不用 css modules,而是在.css文件中编写以下 css:
.myButton.ant-btn-primary {
background-color: red !important;
}并在 typescript 组件中导入.css文件,使用相同的classname。
以上就是为什么使用 :global 修改 Antd 样式无效?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号