英文:
Is there a way to style all divs in a css class?
问题
我对CSS/前端非常陌生,我想知道是否有一种方法可以为CSS类中的所有后续HTML元素设置样式。
例如,所有在styles.error内的div元素。
我在React中使用模块,对下面的代码为什么不会为整个div元素设置样式感到困惑。
我期望看到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.
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.css或error.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 bediv.errorinstead 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.cssorerror.css, whatever name it is), and then import those global CSS files in youindex.js.
Hope this can help!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论