Create-react-app: How to get CSS background images with styled-components?

Created on 4 Oct 2017  路  4Comments  路  Source: facebook/create-react-app

This basic code doesn't seem to be working, there are no compilation errors or anything.

const Content = styled.div`
    border: 1px solid #000;
    background-image: url(./img/bg.gif);
    width: 2000px;
    height: 2000px;
`

Thanks in advance!

question

Most helpful comment

The following should work:

import styled from 'styled-components';
import img from './img/bg.gif';

const Content = styled.div`
    border: 1px solid #000;
    background-image: url(${img});
    width: 2000px;
    height: 2000px;
`;

All 4 comments

The following should work:

import styled from 'styled-components';
import img from './img/bg.gif';

const Content = styled.div`
    border: 1px solid #000;
    background-image: url(${img});
    width: 2000px;
    height: 2000px;
`;

how i can make background image in each div

How i made this with CSS style in react ? :s

You can do with dinamic props, like that

const Content = styled.div`
    background-image: url(${props => props.img});
`;
<Content img={ImagePath} />
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dualcnhq picture dualcnhq  路  3Comments

adrice727 picture adrice727  路  3Comments

fson picture fson  路  3Comments

wereHamster picture wereHamster  路  3Comments

jnachtigall picture jnachtigall  路  3Comments