Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load background images from css in React and NextJS

The images not loading from CSS file which is used for the background but in jsx it's loading

In the CSS file, I have used like below

.main-banner {
  position: relative;
  height: 910px;
  z-index: 1;
  background: transparent url(../../static/images/banner-bg1.jpg) right top no-repeat;
}

the image URL is absolutely fine

And configuration file like this

//next.config.js
const withImages = require('next-images');
const withCSS = require('@zeit/next-css')

module.exports = withImages(withCSS({
    cssLoaderOptions: {
        url: false
    }
}))

what is the issue actually?

Thanks

like image 209
jesica Avatar asked Apr 03 '19 05:04

jesica


People also ask

How do I add a background image in next JS?

Firstly import the image component in next. js and define the image path in src props in the image component. Now your image shows in the browser. js, by default, provides all functionality.

How do I import a background image into react?

To do this, we set the backgroundRepeat property: import image from "./img/UpmostlyLogo. png"; function Component() { return ( <div style={{ backgroundImage:`url(${image})`,backgroundRepeat:"no-repeat" }}> Hello World </div> ); } export { Component }; What is this?

Can I use image in Nextjs?

Nextjs also support image format.


1 Answers

Your static files are being deployed to the web's root. Same as your compiled js and css files.

So you can access them with the following url: url(/static/images/banner-bg1.jpg)

More information about that on this github issue.

like image 199
Luca Avatar answered Oct 26 '22 23:10

Luca