Introduction to seven methods of using css in react (with code)
This article brings you an introduction to seven methods of using CSS in react (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The first one: Use the style directly in the component
No need for the component to import css files from the outside, just write it directly in the component.
import React, { Component } from "react"; const div1 = { width: "300px", margin: "30px auto", backgroundColor: "#44014C", //驼峰法 minHeight: "200px", boxSizing: "border-box" }; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <div>123</div> <div> </div> ); } } export default Test;<p> Notes: <br>1. In normal css, such as background-color, box-sizing and other attributes, the attributes in the style object p1 must be converted into camel case, backgroundColor, boxSizing . Properties without hyphens, such as margin, width, etc., remain unchanged in the style object. <br>2. In normal css, the value of css does not need to be quoted in double quotes (""), such as </p> <pre class="brush:php;toolbar:false">.App-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); color: white; }
3. You cannot directly use strings to write style, and an error will be reported
<div> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/image/171/466/819/1553917250154517.png?x-oss-process=image/resize,p_40" class="lazy" title="1553917250154517.png" alt="Introduction to seven methods of using css in react (with code)"></p> <p>When using the style object in react. Values must be enclosed in double quotes. </p> <p>This method of react style only applies to the current component. </p> <p style="white-space: normal;">Second: Introduce the [name].css file into the component</p> <p>You need to use import at the beginning of the current component to introduce the css file. </p> <pre class="brush:php;toolbar:false">import React, { Component } from "react"; import TestChidren from "./TestChidren"; import "@/assets/css/index.css"; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <div>123</div> <testchidren>测试子组件的样式</testchidren> </div> ); } } export default Test;
The css styles introduced in this way will apply to the current component and all its descendant components.
Third method: 3. Introduce the [name].scss file into the component
React already supports files with the suffix scss, so you only need to install node -sass is enough, because with node-sass, scss files can be compiled into css files in the node environment.
>yarn add node-sass
Then write the scss file
//index.scss .App{ background-color: #282c34; .header{ min-height: 100vh; color: white; } }
For details on how to use sass, please check the sass official website
The css style introduced in this way will also affect The current component and all its descendant components.
Fourth method: Introduce the [name].module.css file into the component
Introduce the css file as a module. All css in this module only Acts on the current component. Does not affect descendant components of the current component.
import React, { Component } from "react"; import TestChild from "./TestChild"; import moduleCss from "./test.module.css"; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <div>321321</div> <testchild></testchild> </div> ); } } export default Test;
This method can be seen as an upgraded version of the first method of using style in components. Completely separate css and components without affecting other components.
The fifth way: introduce the [name].module.scss file into the component
Similar to the fourth way, the difference is that the fourth way introduces the css module, and This is just introducing the scss module.
import React, { Component } from "react"; import TestChild from "./TestChild"; import moduleCss from "./test.module.scss"; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <div>321321</div> <testchild></testchild> </div> ); } } export default Test;
The same method can be regarded as an upgraded version of the first method of using style in components.
Sixth: Using styled-components
You need to install
>yarn add styled-components
first and then create a js file (note that it is a js file, not a css file )
//style.js import styled, { createGlobalStyle } from "styled-components"; export const SelfLink = styled.p` height: 50px; border: 1px solid red; color: yellow; `; export const SelfButton = styled.p` height: 150px; width: 150px; color: ${props => props.color}; background-image: url(${props => props.src}); background-size: 150px 150px; `;
Use styled-components style in components
import React, { Component } from "react"; import { SelfLink, SelfButton } from "./style"; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <selflink>app.js</selflink> <selfbutton> SelfButton </selfbutton> </div> ); } } export default Test;
This method is to merge the entire css style and html node into one component. Introducing this component has both html and css.
The advantage is that you can dynamically change the style at any time by uploading attributes to the component. It is more convenient for processing variables, media queries, pseudo-classes, etc.
This method of css is only valid for the current component.
For specific usage, please check the styled-components official website
Seventh method: Use radius
You need to install it first
>yarn add radium
and then Directly introducing and using
import React, { Component } from "react"; import Radium from 'radium'; let styles = { base: { color: '#fff', ':hover': { background: '#0074d9' } }, primary: { background: '#0074D9' }, warning: { background: '#FF4136' } }; class Test extends Component { constructor(props, context) { super(props); } render() { return ( <div> <button> this is a primary button </button> </div> ); } } export default Radium(Test);
in react components is inconvenient for processing variables, media queries, pseudo-classes, etc.
Using Radium, you can directly handle variables, media queries, pseudo-classes, etc., and you can directly use mathematics, connections, regular expressions, conditions, functions, etc. in js.
Please check the official website of radius for specific usage
Note:Before exporting, it must be wrapped with Radium.
This article has ended here. For more other exciting content, you can pay attention to the CSS Video Tutorial column of the PHP Chinese website!
The above is the detailed content of Introduction to seven methods of using css in react (with code). For more information, please follow other related articles on the PHP Chinese website!

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),