Home > Web Front-end > JS Tutorial > How to add background image in react

How to add background image in react

王林
Release: 2021-01-08 17:28:22
Original
8249 people have browsed it

How to add a background image in react: first introduce the image path we need, such as [import Background from '../../../images/xxx.jpg']; then define the background style, such as [width: "100%"]; just render the page finally.

How to add background image in react

Environment of this article:

  • windows10, react16 version

  • Dell G3 computer

(Learning video sharing: react video tutorial)

Set div background image in react js project

In traditional projects, we only need to define the style in the css file, and then add the corresponding class to the div. However, in react.js, especially in an environment built with webpack, it seems that it is not necessary to directly define the class style. It works so well, because in such a react project, you need to use camel case className={Obj (an object)} to define the class attribute, and style={(an object)} to define the style on the tag.

So how do we add a background image to a div?

is as follows:

//首先引入需要的图片路径
import Background from '../../../images/login.jpg';

//定义背景样式

var sectionStyle = {
  width: "100%",
  height: "400px",
// makesure here is String确保这里是一个字符串,以下是es6写法
  backgroundImage: `url(${Background})` 
};

export default class Login extends Component{
//渲染页面
render(){
        return (
    <div style={sectionStyle}></div>
        )

    }

}
Copy after login

Code screenshot:

How to add background image in react

How to add background image in react

##Related recommendations:

js tutorial

The above is the detailed content of How to add background image in react. 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