英文:
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 'style/file.module.css'
to style an element create a class in the CSS file:
.paragraph{
color:green
}
and then use it in the component:
<p className={styles.paragraph}>I am styled with CSS modules</p>
Ref!
there is a standard CSS framework called Tailwindcss you should try it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论