博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在React JS组件和React JS App中添加CSS样式?
阅读量:2531 次
发布时间:2019-05-11

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

In this tutorial, we will only work with CSS styles. Please ensure you have basic knowledge of HTML, CSS, React JS and Node.Js.

在本教程中,我们将仅使用CSS样式 。 请确保您具有HTML,CSS,React JS和Node.Js的基础知识。

In our recent articles, we talked about ?

在最近的文章中,我们讨论了 ?

We said the first step to adding a component from an external component is to import the file into the main component.

我们说从外部组件添加组件的第一步是将文件导入到主要组件中。

Adding a CSS file in our React JS app is also as simple as importing a file in the React JS App.

在我们的React JS应用程序中添加CSS文件也与在React JS应用程序中导入文件一样简单。

Let's dive into our code.

让我们深入研究我们的代码。

Create a CSS file in the same folder with your component's file.

在与组件文件相同的文件夹中创建一个CSS文件。

index.css

index.css

body {
color: blue;}table {
border-collapse: collapse; width: 50%;}th,td {
text-align: left; padding: 8px;}tr:nth-child(even) {
background-color: #f2f2f2}th {
background-color: yellow; color: green;}.go {
background-color: lightblue}#form {
input[type=text]:focus { background-color: lightblue; }}

Let's add some code in our main component file,

让我们在主要组件文件中添加一些代码,

App.js:

App.js:

import React from "react" class App extends React.Component {	render (){		return (			

React is Cool!

Course Day Coeff
Algorithm monday 5
Database friday 4

) }}export default App

Finally, our index.js where we will import our CSS file using the import keyword followed by the name of our CSS file.

最后,在我们的index.js中 ,我们将使用import关键字和CSS文件名导入 CSS文件。

import React from "react"import ReactDOM from "react-dom"import App from "./App"import "./index.css"     ReactDOM.render ( 
, document.getElementById('root'))

Also, adding style to a class is simply by adding the class name in your CSS code. I know you may wonder why I gave used className to assign an attribute to my div.

此外,只需在CSS代码中添加类名称即可为类添加样式 。 我知道您可能想知道为什么我要使用classNamediv分配一个属性。

That's the right syntax actually because we are writing JavaScript.

实际上,这是正确的语法,因为我们正在编写JavaScript。

Run your app and open in browser...

运行您的应用并在浏览器中打开...

React JS | Adding CSS styles in React JS App | Example

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.

感谢您与我编码! 下次见。 随意发表评论或问题。

翻译自:

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

你可能感兴趣的文章
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>