3 回答
TA贡献1817条经验 获得超14个赞
在内联CSS中只需添加url(...)如下内容
import React from "react";
import ReactDOM from "react-dom";
const customStyle = {
color : "red"
}
const dStyle = {
backgroundImage : "url(https://wallpapercave.com/wp/wp2771916.jpg)",
}
ReactDOM.render(
<div style = {dStyle}>
<h1 style = {customStyle}>Hello World!
</h1>
</div>, document.getElementById("root"));
TA贡献1831条经验 获得超4个赞
解决方案一:
<div style={{ backgroundImage: `url(require("https://wallpapercave.com/wp/wp2771916.jpg"))` }}>
解决方案2:
import React from "react";
import Background from 'https://wallpapercave.com/wp/wp2771916.jpg';
import ReactDOM from "react-dom";
const customStyle = {
color : "red"
}
const dStyle = {
backgroundImage: "url(" + { Background } + ")" // unable to view this image as background-image
}
ReactDOM.render(
<div style = {dStyle}>
<h1 style = {customStyle}>Hello World!
</h1>
</div>, document.getElementById("root"));
TA贡献1859条经验 获得超6个赞
这将有助于:
首先将 imageUrl 存储在变量中,然后使用style属性,如下所示:
const TestingImage = () => {
const imageUrl = "https://wallpapercave.com/wp/wp2771916.jpg";
return (
<>
<div
style={{
backgroundImage: `url(${imageUrl})`,
height: "400px",
}}
></div>
</>
);
};
添加回答
举报