简体   繁体   English

在 REACTJS 中显示带有内嵌样式的背景图像

[英]Displaying Background image with in-line styling in REACTJS

 import React from 'react'; import Slick from 'react-slick'; import style from './Slider.module.css'; import {Link} from 'react-router-dom'; const SliderTemplates = (props) => { let template = null; const settings= { dots:true, infinite: true, arrows: false, speed: 500, slidesToShow:1, slidesToScroll:1 } switch(props.type){ case('featured'): template= props.data.map((item, i)=>{ return( <div key={i}> <div className={style.featured_item}> <div className={style.featured_image} style={{ background: `url(../../Assets/images/articles/${item.image})` }}> <Link to={`/articles/${item.id}`}> <div className={style.featured_caption}> {item.title}s </div> </Link> </div> </div> </div> ) }) break; default: template=null; } return ( <Slick {...settings}> {template} </Slick> ); }; export default SliderTemplates;
 .featured_item{ position: relative; } .feature_item a{ position: absolute; width: 100%; bottom: 0px; text-decoration: none; right: 0px; } .featured_image { height: 330px; background-size: cover !important; background-repeat: no-repeat !important; } .featured_caption{ color: #fff; top: 0px; width: 100%; padding: 20px; font-weight: 300; font-size: 28px; box-sizing: border-box; }

I am trying to map images from a folder (Address given in the picture attached) with in-line styling in ReactJs.我正在尝试使用 ReactJs 中的内嵌样式映射来自文件夹(附图中给出的地址)中的图像。 All the data is imported from db.json and I want to display the images(position: relative) with text (position: absolute) but the picture is not available for display and I can't find where I went wrong.所有数据都是从db.json导入的,我想用文本(位置:绝对)显示图像(位置:相对),但图片无法显示,我找不到我出错的地方。 The picture is not displayed.图片不显示。 Screenshot截屏

如果您尝试设置背景图像,则 css 属性为BackGroundImage 。其次,您需要从文件夹中导入图像,这就是您要执行的操作

style={{ BackGroundImage: `url(${require('../../Assets/images/articles/'+item.image+''})` }}

Just add the image into a folder in react and set it as a background image you would in regular css只需将图像添加到反应文件夹中,并将其设置为常规 css 中的背景图像

.featured_image {
    height: 330px;
    background: url('./(IMAGE PATH HERE)') no-repeat center center / cover
}

Just one to note also, i would always give your styles a capital letter if you are using .module.css and do not use - or _ , i would recommend renaming to .FeaturedImage, and then change it in your jsx - className={style.FeaturedImage}还有一点要注意,如果您使用 .module.css 并且不使用 - 或 _ ,我总是会给您的样式一个大写字母,我建议重命名为 .FeaturedImage,然后在您的 jsx 中更改它 - className={ style.FeaturedImage}

Try尝试

style={{ backgroundImage: url(../../Assets/images/articles/${item.image}) }}>` style={{ backgroundImage: url(../../Assets/images/articles/${item.image}) }}>`

Also consider placing your images in the public folder instead - static assets work best there.还可以考虑将您的图像放在公共文件夹中 - 静态资产在那里效果最好。 By doing this you could use this approach:通过这样做,您可以使用这种方法:

style={{ backgroundImage: url(${process.env.PUBLIC_URL}/images/articles/${item.image})` style={{ backgroundImage: url(${process.env.PUBLIC_URL}/images/articles/${item.image})`

Read more about static assets in React here: https://create-react-app.dev/docs/using-the-public-folder/在此处阅读有关 React 中静态资产的更多信息: https : //create-react-app.dev/docs/using-the-public-folder/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM