next.js学习笔记-css的引入

学习目标:

css引入


学习内容:

next中
1、页面中如何引入css
2、组件中如何引入


学习产出:

css的引入(四种方法)

方法1:全局引入

在pages文件夹下新建_app.tsx文件(next中有一个app根组件,新建_app.tsx是在重写该文件)

//_app.tsx文件
import "../styles.scss"
export default ({Component, pageProps}) => (
  // Component代表动态组件,跟随访问地址不同而指向不同的组件
  <Component {...pageProps} />
)

方法2:模块引入

该方法的样式文件命名必须为xxx.module.scss

// pages中的页面文件
import styles from "./index.modules.scss"
const Index = () => {
   return (
       <>
         <h2 className={styles.xxxx}>pageIndex</h2>
       </>
   )
}
export default Index;

方法3:内联样式

// pages中的页面文件
import styles from "./index.modules.scss"
const Index = () => {
   return (
       <>
         <h2 className={styles.xxxx}>模块引入</h2>
         <h2 style={{color: 'green'}}>内联样式</h2>
       </>
   )
}
export default Index;

方法4:jsx方式

// pages中的页面文件
import styles from "./index.modules.scss"
const Index = () => {
   return (
       <>
         <h2 className={styles.xxxx}>模块引入</h2>
         <h2 style={{color: 'green'}}>内联样式</h2>
         <p>jsx方式</p>
         <style jsx>{`
            {
                p {
                    color: blue;
                }
            }
         `}</style>
          还有一种形式,可以设置全局样式
          <style global jsx>{`
            {
                body {
                    background: #000;
                }
            }
         `}</style>
       </>
   )
}
export default Index;

sass

sass的引入和css方式一样。next.js允许您同时使用.scss和.sass扩展名导入sass。您可以通过css模块和.module.scss或module.sass扩展名使用组件级sass。前提是安装了sass

less

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值