简体   繁体   English

使用 CSS 模块针对 next.js 中的纯元素

[英]Targeting Pure elements in next.js with CSS modules

I'm porting an app from React to Next and I keep getting an error with my css: "ul" is not pure (pure selectors must contain at least one local class or id).我正在将一个应用程序从 React 移植到 Next,并且我的 css 不断出现错误:“ul”不是纯的(纯选择器必须包含至少一个本地 class 或 id)。

I'm using css modules and am not sure what the workaround is to keep everything in the css module.我正在使用 css 模块,但不确定解决方法是将所有内容保留在 css 模块中。

for example:例如:

index.js index.js

<div className={styles.container}>
  <ul>
    <li>Person One</li>
  </ul>
</div>

index.module.css index.module.css

.container{
  background-color: pink;
}
ul{
  list-style-type: none; //this throws an error
}

If you don't want to use classes as mentioned @Zeeshan's answer, you cannot use the module-based CSS.如果您不想使用@Zeeshan 的回答中提到的类,则不能使用基于模块的 CSS。 You will need to use a global stylesheet to target pure elements as selectors.您将需要使用全局样式表将纯元素定位为选择器。 They cannot be component-scoped.它们不能是组件范围的。

Further, you can only import a global stylesheet in one place ( _app.tsx/_app.jsx/_app.js ), or you'll receive an error ( explained here ).此外,您只能在一个地方 ( _app.tsx/_app.jsx/_app.js ) 导入全局样式表,否则您将收到错误消息( 在此处解释)。

You may break your styles out across many stylesheets, but they all have to be global, so there's probably little benefit from doing so.您可能会在许多样式表中破坏您的 styles,但它们都必须是全局的,因此这样做可能没有什么好处。

To quote directly from this Next.js GitHub discussion forum :直接这个 Next.js GitHub 讨论论坛引用:

You are receiving this error because you are using html tags directly instead of classnames or ids in a file extension that is probably [filename].module.(css | scss | sass)您收到此错误是因为您直接使用 html 标记而不是文件扩展名中的类名或 ids,可能是[filename].module.(css | scss | sass)

File extensions with * .module.(css | scss | sass) are css modules and they can only target elements using classnames or ids and not using tag names.带有 * .module.(css | scss | sass)的文件扩展名是 css 模块,它们只能使用类名或 id 来定位元素,而不能使用标签名。 Although this is possible in other frameworks like create-react-app, it is not possible in next-js.虽然这在 create-react-app 等其他框架中是可能的,但在 next-js 中是不可能的。

My suggestion is using these html selector css in a separate file that doesn't contain the '.module' in it.我的建议是在不包含“.module”的单独文件中使用这些 html 选择器 css。 Example: [filename].(css | scss | sass) --> styles.scss示例: [文件名].(css | scss | sass) --> styles.scss

And instead of importing like this而不是像这样导入

import styles from './styles.module.scss';

import like this像这样导入

import './styles.scss';

Try giving a className or id to ul tag and then write your styles accordingly.尝试为 ul 标签提供 className 或 id,然后相应地编写您的 styles。

for example:例如:

index.js index.js

<div className={styles.container}>
 <ul className={styles.container__list}>
  <li>Person One</li>
 </ul>
</div>

index.module.css index.module.css

.container {
  background-color: pink;
}
.container__list{
  list-style-type: none;
}

This is actually extremely simple.这实际上非常简单。

You can add a container class to the top element (just like you did), and then use combinators (您可以将容器 class 添加到顶部元素(就像您所做的那样),然后使用组合器( , + , > , etc.) to target pure elements as you wish. , + , >等)根据需要定位纯元素。

For example:例如:

index.js: index.js:

<div className={styles.container}>
    <ul>
        <li>Person One</li>
    </ul>
</div>

index.module.css: index.module.css:

.container {
  background-color: pink;
}
.container ul {
  list-style-type: none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM