Home > Web Front-end > JS Tutorial > Can react introduce css?

Can react introduce css?

青灯夜游
Release: 2020-12-04 09:51:41
Original
2459 people have browsed it

react can introduce css. Method: 1. Use "style={{css style}}" directly inside the component to introduce css; 2. First declare a variable in the render function to save the style sheet binding to style. attribute, and then quote; 3. Use "import 'css path'" to introduce the css file.

Can react introduce css?

The operating environment of this tutorial: windows7 system, react16 version. This method is suitable for all brands of computers.

Recommended learning: "Programming Video"

CSS can be introduced in React. However, if you write the inline style directly in the following way, an error will be reported directly because JSX syntax is not supported.

<div style="color:red">测试数据</div> //报错
Copy after login

React supports the following three methods of writing css: [Related tutorial recommendations: React video tutorial]

1. Inline style: directly inside the component Definition

...
<div style={{
    width:&#39;200px&#39;,
    height:&#39;80px&#39;,
    backgroundColor:&#39;yellow&#39;,        
    fontSize:&#39;24px&#39;,        
    textAlign:&#39;center&#39;       
}}>测试数据</div>
...
Copy after login

2. Declaration style: declare first in the render function, and then quote

The declaration style is similar to the inline style, the only difference is that a variable is declared to save Style sheets are bound to the style attribute.

render() {
    let mystyle = {
         width:&#39;200px&#39;,
		 height:&#39;80px&#39;,
		 backgroundColor:&#39;yellow&#39;,        
		 fontSize:&#39;24px&#39;,        
		 textAlign:&#39;center&#39;    
    }
    return(
        <div style={mystyle}>
            测试数据
        </div>
    );
}
Copy after login

3. Use import to introduce external css style files

A React component is generally a folder that contains the corresponding js and css. As long as it is in Just introduce the same level of css into js.

import &#39;./mystyle.css&#39;;
Copy after login

If you want to read more related articles, please visit PHP Chinese website! !

The above is the detailed content of Can react introduce css?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template