有没有一种方法来为CSS类中的所有
元素设置样式?

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

Is there a way to style all divs in a css class?

问题

我对CSS/前端非常陌生,我想知道是否有一种方法可以为CSS类中的所有后续HTML元素设置样式。

例如,所有在styles.error内的div元素。

我在React中使用模块,对下面的代码为什么不会为整个div元素设置样式感到困惑。

Error.module.css

error.jsx

页面视图

我期望看到div元素被着色。

英文:

I am extremely new to css/frontend and I was wondering if there was any way to style all subsequent html elements within a css class.

For example all the div within styles.error.

I am using Modules with react and am confused about why this code below doesn't style the entire div element.

Error.module.css

error.jsx

Page view

I expected to see the div element to be coloured in

答案1

得分: 0

有两个地方你可以注意到:

  • 重新检查你的CSS选择器,小心处理所有类名和标签名的空格和顺序。根据你的JSX,.error 类内部没有任何div。应该是 div.error 而不是 .error div
  • 在React中,模块化CSS仅在导入它的单个文件内生效。所以,如果你需要一个更全局的解决方案来为所有页面中的所有错误应用样式,你可以考虑使用普通的CSS文件(例如 styles.csserror.css,不管叫什么名字),然后在你的 index.js 中导入这些全局CSS文件。

希望这对你有帮助!

英文:

There are 2 things here you can notice:

  • Recheck your CSS selectors, be careful all space and order of your class name & tag name. As according to your JSX, you don't have any div inside class .error. It should be div.error instead of .error div.
  • Modules CSS in React is only applied inside a single file where it is imported, so if you need a more global solution to style all errors in all pages, you can consider to use normal CSS files (e.g. styles.css or error.css, whatever name it is), and then import those global CSS files in you index.js.

Hope this can help!

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

发表评论

匿名网友

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

确定