Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
React JS styled-components importing images from public folder

I have been trying to set the image to background-image from the public folder in styled-components. I tried so

import styled from "styled-components";
import Background from 'process.env.PUBLIC_URL + /images/background.svg'

export const HomeContainer = styled.section`
  height: 100vh;
  max-width: 100vw;
  background-image: url(${Background});
  background-repeat: no-repeat;
  background-size: cover;
`;

and so

import styled from "styled-components";

export const HomeContainer = styled.section`
  height: 100vh;
  max-width: 100vw;
  background-image: url('process.env.PUBLIC_URL + /images/background.svg');
  background-repeat: no-repeat;
  background-size: cover;
`;

How can I import it? Thanks in advance

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I've googled it but still have nothing just like you.

So I decided to use props:

import styled from "styled-components";

const HomeContainer = styled.section`
  height: 100vh;
  max-width: 100vw;
  background-image: url(${ ({Background}) => Background });
  background-repeat: no-repeat;
  background-size: cover;
`;

const App = () => {
  return (
    <HomeContainer Background={ process.env.PUBLIC_URL + 'where your image at' } />
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

I highly recommend you to have jsconfig file configured with base path

{
  "compilerOptions": {
      "jsx": "react",
      "baseUrl": "./",
  }
}

And then access your image by simply import img from "public/images/whatever.png

about 4 years ago · Juan Pablo Isaza Report

0

Try this, it's working for me.

import bgImage from "../images/background.png";

const Wrapper = styled.div`
  background-image: url(${bgImage});
`
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!