如何在Next.js中使用样式CSS文件?

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

How to use style css files in Next.js?

问题

I'm new to Next.js.

Although I declared css files in _app.tsx, some of styles defined in the css files are not working.
Some of styles use images imported from 'public/images' and this are not imported neither.
Please help me find out what is wrong with this. Do I have to change the folder structure?
The version of Next.js is "13.1.1".

Thanks in advance!!

I'm working on a project with below folder structures.

  • public
    • fonts
    • images
  • src
    • pages
    • styles
      • global.css
      • layout.css

My _app.tsx file looks like

import '@/styles/layout.css';
import '@/styles/common.css';

export default function App({ Component, pageProps }: AppProps) {
 ...
}
英文:

I'm new to Next.js.

Although I declared css files in _app.tsx, some of styles defined in the css files are not working.
Some of styles use images imported from 'public/images' and this are not imported neither.
Please help me find out what is wrong with this. Do I have to change the folder structure?
The version of Next.js is "13.1.1".

Thanks in advance!!

I'm working on a project with below folder structures.

  • public
    • fonts
    • images
  • src
    • pages
    • styles
      • global.css
      • layout.css

My _app.tsx file looks like

import '@/styles/layout.css';
import '@/styles/common.css';

export default function App({ Component, pageProps }: AppProps) {
 ...
}

答案1

得分: 1

将你的CSS文件重命名为layout.module.css。在你的CSS文件中使用.module.css扩展名。查阅nextjs文档以获取更多参考信息。

英文:

Rename your CSS file as layout.module.css. Use .module.css extension in your CSS files. Refer nextjs documentation for further references.

答案2

得分: 0

我也是新手,发现你不能使用普通的CSS文件,应该使用CSS模块。

你需要将每个CSS文件的名称(除全局样式外)从.css改为.module.css,然后需要在要样式化的组件中导入该文件:import styles from 'style/file.module.css'

要为元素添加样式,请在CSS文件中创建一个类:

.paragraph {
  color: green;
}

然后在组件中使用它:

<p className={styles.paragraph}>我是使用CSS模块样式的</p>

参考链接

还有一个名为Tailwindcss的标准CSS框架,你应该尝试一下。

英文:

am also new to Nextjs , turned out you can't use regular CSS files, you should use CSS modules.

you need to rename each CSS file 'except global' to end with .module.css instead of .css
then you need to import the file in the component you want to style : import styles from &#39;style/file.module.css&#39;

to style an element create a class in the CSS file:
.paragraph{
color:green
}

and then use it in the component:

        &lt;p className={styles.paragraph}&gt;I am styled with CSS modules&lt;/p&gt;

Ref!

there is a standard CSS framework called Tailwindcss you should try it

huangapple
  • 本文由 发表于 2023年1月9日 16:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054649.html
匿名

发表评论

匿名网友

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

确定