1. Inline styles To add inline styles to the virtual DOM, you need to use an expression to pass in a style object. To implement inline styles, you need to write a style object, and this style object can be placed in many places, such as: in the
2. Use classReact recommends that we use inline styles because React considers each component to be an independent entity. In fact, in most cases, we still add a lot of class names to elements, but it should be noted that import React, { Component } from 'react' 1. Import styles defined externally import styles from './style.css' class ClassStyle extends Component { render() { // js logic let className = cx({ font: false }) return ( <> <div className={className}>hello</div> <p className='setstyle'>Style</p> <DivContainer> world </DivContainer> <> ) } } export default ClassStyle 3. Add different styles for different classNames conditions Sometimes you need to add different styles based on different conditions, such as completion status, completed is green, unfinished is red. In this case, we recommend using the classnames package: import style from './style.css' <div className={style.class1 style.class2}</div> The final rendering effect is:
Download and install
use import classnames from 'classnames' <div className=classnames({ 'class1': true, 'class2': true )> </div> 4. CSS-in-JS
styled-components is probably the most popular library for CSS-in-JS. With styled-components, you can use ES6's tag template string syntax to define a series of CSS properties for the component that needs to be styled. When the JS code of the component is parsed and executed, styled-components will dynamically generate a CSS selector and insert the corresponding CSS style into the head tag in the form of a style tag. Dynamically generated CSS selectors will have a small hash value to ensure global uniqueness to avoid style conflicts. 1. Installation
Defining styles import styled from 'styled-components' const Title = styled.div` color:red; font-size:16px; h3{ color:blue; font-size:20px; } ` export { Title } Display it just like you would a regular React component using Title import React, { Component } from 'react' import { Title } from './Styles' class App extends Component { render() { return ( <div> <Title> I'm just a title <h3>Hello World</h3> </Title> </div> ); } } export default App 3. Style inheritance style import styled from 'styled-components' const Button = styled.button` font-size: 20px; border: 1px solid red; border-radius: 3px; `; // A new component that inherits Button and overrides some styles const Button2 = styled(Button)` color: blue; border-color: yellow; `; export { Button, Button2 } show import React, { Component } from 'react' import { Button, Button2 } from './Styles' class App extends Component { render() { return ( <div> <Button>I am a button 1</Button> <Button2>I am a button 2</Button2> </div> ); } } export default App 4. Attribute transfer style import styled from 'styled-components' const Input = styled.input` color: ${props => props.inputColor || "blue"}; border-radius: 3px; `; export { Input } show import React, { Component } from 'react' import { Input } from './Styles' class App extends Component { render() { return ( <div> <Input defaultValue="Hello" inputColor="red"></Input> </div> ); } } export default App This is the end of this article about how to set DOM styles in four react components. For more relevant react component DOM style content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solve the problem that the repository and tag names are both none after Docker loads a new image
>>: Research on the Input Button Function of Type File
In Linux system, both chmod and chown commands ca...
Table of contents Written in front Two-way encryp...
There are currently three ways to display the cen...
Table of contents introduce Usage scenarios Sourc...
After learning the basic operations of Docker, we...
Table of contents Preface 1. Routing lazy loading...
The worst option is to sort the results by time a...
Front-end test page code: <template> <di...
Table of contents Preface environment Install Cre...
Recently, when I was modifying the intranet porta...
swarm three virtual machines 132,133,134 1. Initi...
Enctype : Specifies the type of encoding the brows...
Source: http://www.ruoyi.vip/ import Vue from ...
This article uses an example to describe the MySQ...
1. Search mysql in the browser to download and in...