如何覆盖 ant-design 5 中的样式?

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

How do I override the styles in ant-design 5?

问题

I'm using React (create-react-app), ant design 5.3.0 and modular scss.

When using embedded components, their styles override mine, because the component creates a wrapper and my styles are hung from the upstream wrapper and the downstream element creates the ant design itself.

To override them in scss, I figured out that I need to declare an inline class inside my class which generates ant design. For example, for Typography it will form .ant-typography. There's one BUT: if you don't use modular scss (don't write to files like head.module.css), you're fine. And if you write in modular scss files, it doesn't override.

Head.module.scss
index.js importing module scss
what render index.js with module scss
index.js importing plain scss
what render index.js with plain scss
(sorry: i dont have enough reputation to post images yet because of novice contributor)

I think it has something to do with kebab-case notation. I tried changing the notation to camelCase in the model files, but that didn't help either. Can you tell me how to solve the issue?

英文:

I'm using React (create-react-app), ant design 5.3.0 and modular scss.

When using embedded components, their styles override mine, because the component creates a wrapper and my styles are hung from the upstream wrapper and the downstream element creates the ant design itself.

To override them in scss, I figured out that I need to declare an inline class inside my class which generates ant design. For example, for Typography it will form .ant-typography.
There's one BUT: if you don't use modular scss (don't write to files like head.module.css), you're fine. And if you write in modular scss files, it doesn't override.

Head.module.scss
index.js importing module scss what render index.js with module scss
index.js importing plain scss what render index.js with plain scss
(sorry: i dont have enough reputation to post images yet because of novice contributor)

I think it has something to do with kebab-case notation. I tried changing the notation to camelCase in the model files, but that didn't help either. Can you tell me how to solve the issue?

答案1

得分: 1

尝试使用以下方式:

:global(.className) {
    // 设置 CSS 属性
}

在模块化的 SCSS 文件中,例如:

:global(.swiper-pagination) {
    width: 100%;
}

检查页面以找到需要覆盖的类名。这里有一个关于 :global 的链接:
https://stackoverflow.com/questions/43613619/what-does-global-colon-global-do

英文:

Try using

:global(.className) {
    // Set CSS Properties
}

in the modular scss file

For example:

:global(.swiper-pagination) {
    width: 100%;
}

Inspect the page to find the classname you need to override.
Here's a link about ':global':
https://stackoverflow.com/questions/43613619/what-does-global-colon-global-do

huangapple
  • 本文由 发表于 2023年3月9日 15:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681680.html
匿名

发表评论

匿名网友

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

确定