博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js如何覆盖组件css样式_使用CSS样式化Next.js组件
阅读量:2505 次
发布时间:2019-05-11

本文共 2312 字,大约阅读时间需要 7 分钟。

js如何覆盖组件css样式

How do we style React components in Next.js?

我们如何在Next.js中设置React组件的样式?

We have a lot of freedom, because we can use whatever library we prefer.

我们有很多自由,因为我们可以使用我们喜欢的任何库。

But Next.js comes with built-in, because that’s a library built by the same people working on Next.js.

但是Next.js内置了 ,因为那是由从事Next.js的同一个人构建的库。

And it’s a pretty cool library that provides us scoped CSS, which is great for maintainability because the CSS is only affecting the component it’s applied to.

它是一个非常酷的库,为我们提供了范围内CSS,这对于可维护性非常有用,因为CSS仅会影响所应用的组件。

I think this is a great approach at writing CSS, without the need to apply additional libraries or preprocessors that add complexity.

我认为这是编写CSS的好方法,无需应用其他会增加复杂性的库或预处理器。

To add CSS to a React component in Next.js we insert it inside a snippet in the JSX, which start with

要将CSS添加到Next.js中的React组件中,我们将其插入JSX的一个片段中,该片段以

and ends with

并以

`}

Inside this weird blocks we write plain CSS, as we’d do in a .css file:

在这个怪异的代码块中,我们编写了纯CSS,就像在.css文件中所做的那样:

You write it inside the JSX, like this:

您可以在JSX内编写它,如下所示:

const Index = () => (  

Home page

)export default Index

Inside the block we can use interpolation to dynamically change the values. For example here we assume a size prop is being passed by the parent component, and we use it in the styled-jsx block:

在块内部,我们可以使用插值来动态更改值。 例如,在此我们假设父组件正在传递一个size道具,并在styled-jsx块中使用它:

const Index = props => (  

Home page

)

If you want to apply some CSS globally, not scoped to a component, you add the global keyword to the style tag:

如果要全局应用某些CSS,而不是将其应用于组件,则将global关键字添加到style标签:

If you want to import an external CSS file in a Next.js component, you have to first install @zeit/next-css:

如果要在Next.js组件中导入外部CSS文件,则必须首先安装@zeit/next-css

npm install @zeit/next-css

and then create a configuration file in the root of the project, called next.config.js, with this content:

然后在项目的根目录中创建一个配置文件,名为next.config.js ,其内容如下:

const withCSS = require('@zeit/next-css')module.exports = withCSS()

After restarting the Next app, you can now import CSS like you normally do with JavaScript libraries or components:

重新启动Next应用程序后,您现在可以像通常使用JavaScript库或组件一样导入CSS:

import '../style.css'

You can also import a SASS file directly, using the library instead.

您也可以直接使用库导入SASS文件。

翻译自:

js如何覆盖组件css样式

转载地址:http://ctqgb.baihongyu.com/

你可能感兴趣的文章
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>