Css 动态添加背景图像在React中不起作用
我正在尝试在clientsSection组件中显示图像。我选择使用div元素并动态更改它们的背景图像,但是图像没有显示出来。如果我应用其他样式,它们会工作,但不会应用背景图像 我的组成部分:Css 动态添加背景图像在React中不起作用,css,reactjs,Css,Reactjs,我正在尝试在clientsSection组件中显示图像。我选择使用div元素并动态更改它们的背景图像,但是图像没有显示出来。如果我应用其他样式,它们会工作,但不会应用背景图像 我的组成部分: import React from 'react'; import { clientsData } from './data'; import './clients.styles.css'; const ClientsSection = () => ( <div className=&
import React from 'react';
import { clientsData } from './data';
import './clients.styles.css';
const ClientsSection = () => (
<div className="clients-section-container">
<h2>we've helped over 600 clients achieve</h2>
<div className="clients-images">
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
)
export default ClientsSection;
顺便说一句,我把我的图片保存在公用文件夹中,它可以正常工作,但我读到这是一种不好的做法(可能会导致构建问题),所以我在/src/images中更改了它们
我的组件在/src/sections/clients节下您试过这个吗
backgroundImage: `url("path to image")`
你试过这个吗
backgroundImage: `url("path to image")`
尝试按如下方式导入要使用的图像:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
然后在数据文件中使用它们,如下所示:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
最后,您可以在UI中显示它们,如下所示:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
我们已经帮助600多名客户实现
{
clientsData.images.map(image=>{
返回
})
}
您的输出将如下所示(我已删除样式,请稍后包括它们):
尝试按如下方式导入要使用的图像:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
然后在数据文件中使用它们,如下所示:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
最后,您可以在UI中显示它们,如下所示:
import first_image from './Assets/icons/airPlaneBlack.png';
import second_image from './Assets/icons/metro-printer.png';
export const clientsData = {
images: [
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '40%',
'top': '20%',
'backgroundImage': `url(${first_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat',
},
{
'position': 'absolute',
'width': '13%',
'height': '30%',
'left': '67%',
'top': '22%',
'backgroundImage': `url(${second_image})`,
'backgroundSize': 'contain',
'backgroundRepeat': 'no-repeat'
}
]
}
<div >
<h2>we've helped over 600 clients achieve</h2>
<div >
{
clientsData.images.map(image => {
return <div style={{ ...image }}></div>
})
}
</div>
</div>
我们已经帮助600多名客户实现
{
clientsData.images.map(image=>{
返回
})
}
您的输出将如下所示(我已删除样式,请稍后包括它们):