Pass "StaticImageData" to "background" css property
Answered
Savannah posted this in #help-forum
SavannahOP
I'm importing image like so:
And passing it to ProjectCard component via props. I want to set background image using inline styles, but this code:
gives me this error:
import logo from "@/assets/img/logo.png"And passing it to ProjectCard component via props. I want to set background image using inline styles, but this code:
export default function ProjectCard(props: ProjectInfo) {
return (
<article style={{ background: props.previewImage }} className={containerStyles.project_card}>
<div>
<h2>{props.title}</h2>
<p>{props.short_description}</p>
</div>
<p>{props.tags}</p>
</article>
);
}gives me this error:
10 Replies
SavannahOP
How can I convert it to the url and actually use it?
@joulev import logo from …
logo.url <- /_next/static/…
SavannahOP
undefinedI guess you are talking about
.src, it has this url but it's not workingIn css I need to wrap url like so:
I guess I also need to do that here? If so, can you please explain how exactly can I wrap it in
background: url("...")I guess I also need to do that here? If so, can you please explain how exactly can I wrap it in
url function. Do I need to import it from somewhere?American Crow
import Image from 'next/image'
import mountains from '../public/mountains.jpg'
export default function Background() {
return (
<Image
alt="Mountains"
src={mountains}
placeholder="blur"
quality={100}
fill
sizes="100vw"
style={{
objectFit: 'cover',
}}
/>
)
}@American Crow tsx
import Image from 'next/image'
import mountains from '../public/mountains.jpg'
export default function Background() {
return (
<Image
alt="Mountains"
src={mountains}
placeholder="blur"
quality={100}
fill
sizes="100vw"
style={{
objectFit: 'cover',
}}
/>
)
}
SavannahOP
Yes, with next/image it's quite simple. I need it to be done with background-image in css though.
@Savannah I guess you are talking about `.src`, it has this url but it's not working
Oh yeah sorry it is .src
Answer
SavannahOP
Thanks!