Next.js background

编程入门 行业动态 更新时间:2024-10-16 04:27:49
本文介绍了Next.js background-image css 属性无法加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题只是我试图访问静态图像以在 React 的内联 backgroundImage 属性中使用.

The issue simply is I'm trying to access a static image to use within an inline backgroundImage property within React.

我正在使用 reactjs 和 next.js,然后我遇到了使用 next.js 添加图像的问题,但通过使用名为 Next.js + Images 的图像加载器解决了这个问题,

i am working with reactjs and next.js then i faced an issue with adding images with next.js but fixed this by using image loader called : Next.js + Images,

现在我可以使用普通的 html img 标签正常添加图片了

now i could add images normally with normal html img tag

示例:<img src={ img }/> 这行得通.

但是当我尝试添加css背景图片如下:

but when i tried to add css background images as the following:

const team = (props) => { const img = require("../../assets/images/security-team.jpg"); const styling = { backgroundImage: `url('${img}')`, width:"100%", height:"100%" } console.log(img); return ( <dev className="team" style={styling}> </dev> );

}

这是 console.log 结果:

here was the console.log results :

/_next/static/images/security-team-449919af8999ae47eaf307dca4dda8e1.jpg

/_next/static/images/security-team-449919af8999ae47eaf307dca4dda8e1.jpg

图像没有出现,然后没有发生错误,我尝试在浏览器中输入 website-url + console.log 结果图像出现了!

the image doesn't appear and no errors happened then, i tried to type in the browser website-url + the console.log results the image appeared !

推荐答案

当我使用 JSX 样式然后添加绝对位置属性时,它工作得很好.

when i used JSX styles then added position absolute property it worked just fine.

像这样:

<style JSX>{` .team { width:100%; height:100%; position:absolute; background: url('`+img+`') no-repeat; } `}</style>

更多推荐

Next.js background

本文发布于:2023-11-02 00:13:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   background

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!