How to use Next Image Component instead of CSS Background URL to put an image to a background
Answered
Western Meadowlark posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Western MeadowlarkOP
I'm using Tailwind with Next
I have a situation where I use the css background url (
I have created a minimum reproducible example
https://github.com/cmgchess/next-bg-image
https://stackblitz.com/~/github.com/cmgchess/next-bg-image
Help much appreciated 🙂
I have a situation where I use the css background url (
bg-[url('/path.webp')]
in tailwind to be precise. But now I run into slow image loading issues. Was wondering if I can use the Next Image component instead hoping it would optimize and improve loading speedsI have created a minimum reproducible example
https://github.com/cmgchess/next-bg-image
https://stackblitz.com/~/github.com/cmgchess/next-bg-image
Help much appreciated 🙂
Answered by ReverseGem7
import Image from "next/image";
import styles from "./home.module.css";
export default function Home() {
return (
<>
<div className={`${styles.gradient}`}>
<div style={{ height: "calc(100vh - 13vh)" }} className="lg:mr-14">
<div className="h-4/5 w-full relative">
<Image
src="/hero.webp"
fill={true}
alt="Your alt"
className="object-contain object-right-top hidden lg:block"
/>
</div>
<div className="h-1/5 flex items-center justify-center w-screen bg-[#2BE94B] text-white text-2xl font-medium text-center leading-normal xl:text-[36px]">
How can I use Next Image instead of Bg
</div>
</div>
</div>
</>
);
}
8 Replies
![Avatar](https://cdn.discordapp.com/avatars/453614925110050816/5f43a6de00cff8d660e12787c098f6fc.webp?size=256)
@ReverseGem7 https://nextjs.org/docs/app/api-reference/components/image#props
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Western MeadowlarkOP
not sure how exactly to use it tho
created a stackblitz as well
https://stackblitz.com/~/github.com/cmgchess/next-bg-image
created a stackblitz as well
https://stackblitz.com/~/github.com/cmgchess/next-bg-image
![Avatar](https://cdn.discordapp.com/avatars/453614925110050816/5f43a6de00cff8d660e12787c098f6fc.webp?size=256)
import Image from "next/image";
import styles from "./home.module.css";
export default function Home() {
return (
<>
<div className={`${styles.gradient}`}>
<div style={{ height: "calc(100vh - 13vh)" }} className="lg:mr-14">
<div className="h-4/5 w-full relative hidden lg:block">
<Image
src="/hero.webp"
fill={true}
alt="Your alt"
className="object-contain object-right-top"
/>
</div>
<div className="h-1/5 flex items-center justify-center w-screen bg-[#2BE94B] text-white text-2xl font-medium text-center leading-normal xl:text-[36px]">
How can I use Next Image instead of Bg
</div>
</div>
</div>
</>
);
}
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Western MeadowlarkOP
willl have a look
thanks!
thanks!
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Western MeadowlarkOP
@ReverseGem7 tried your solution and looks super close
however i would like to retain the div with the image hidden when making screen small
any idea how?
i deployed so you can see for yourself
https://next-bg-image.vercel.app/
however i would like to retain the div with the image hidden when making screen small
any idea how?
i deployed so you can see for yourself
https://next-bg-image.vercel.app/
![Image](https://cdn.discordapp.com/attachments/1205586183254450226/1205782663575633930/image.png?ex=65d99f9d&is=65c72a9d&hm=2bb2b740175bb73bddfd821524a4c9151a51c6ea06f25faeffdee22712ec1625&)
![Image](https://cdn.discordapp.com/attachments/1205586183254450226/1205782663986806794/image.png?ex=65d99f9d&is=65c72a9d&hm=af2c64a41738bc6aea3538a2323e61d8ae0f85bb16e4bc1c8ca2ac542fdfddc4&)
right now looks like
![Image](https://cdn.discordapp.com/attachments/1205586183254450226/1205782923064901652/image.png?ex=65d99fdb&is=65c72adb&hm=ee7e45562291bddde19435a84c8e61bb575b7255752661708af83072533de952&)
![Avatar](https://cdn.discordapp.com/avatars/453614925110050816/5f43a6de00cff8d660e12787c098f6fc.webp?size=256)
import Image from "next/image";
import styles from "./home.module.css";
export default function Home() {
return (
<>
<div className={`${styles.gradient}`}>
<div style={{ height: "calc(100vh - 13vh)" }} className="lg:mr-14">
<div className="h-4/5 w-full relative">
<Image
src="/hero.webp"
fill={true}
alt="Your alt"
className="object-contain object-right-top hidden lg:block"
/>
</div>
<div className="h-1/5 flex items-center justify-center w-screen bg-[#2BE94B] text-white text-2xl font-medium text-center leading-normal xl:text-[36px]">
How can I use Next Image instead of Bg
</div>
</div>
</div>
</>
);
}
Answer
![Avatar](https://cdn.discordapp.com/avatars/453614925110050816/5f43a6de00cff8d660e12787c098f6fc.webp?size=256)
@ReverseGem7 tsx
import Image from "next/image";
import styles from "./home.module.css";
export default function Home() {
return (
<>
<div className={`${styles.gradient}`}>
<div style={{ height: "calc(100vh - 13vh)" }} className="lg:mr-14">
<div className="h-4/5 w-full relative">
<Image
src="/hero.webp"
fill={true}
alt="Your alt"
className="object-contain object-right-top hidden lg:block"
/>
</div>
<div className="h-1/5 flex items-center justify-center w-screen bg-[#2BE94B] text-white text-2xl font-medium text-center leading-normal xl:text-[36px]">
How can I use Next Image instead of Bg
</div>
</div>
</div>
</>
);
}
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Western MeadowlarkOP
thank you! this looks much better
https://next-bg-image-git-patch-2-cmgchess.vercel.app/
css is hard 😦
https://next-bg-image-git-patch-2-cmgchess.vercel.app/
css is hard 😦