DEV Community

Cover image for Adding Dynamic Css Background Image Using Styled Components In React
Rahul Kumar Choudhary
Rahul Kumar Choudhary

Posted on

7 2

Adding Dynamic Css Background Image Using Styled Components In React

import React from "react";
import styled from "styled-components";


const Home=()=>{
return ( 
<Wrap backgroundImage={backgroundImage}> 
</Wrap>
)
}


const Wrap = styled.div`
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-image: ${(props) =>
    `url(${require(`../images/${props.backgroundImage}`).default})`};
  display: flex;
  flex-direction: column;
  justify-content: space-between; //vertical alignment because of flex direction column
  align-items: center; //horizontal alignment because of flex direction column
`;

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
drealtechie profile image
Femi Olagundoye

Pls I nee a video of this post

Collapse
 
chisonm profile image
Chisonm
Comment hidden by post author

Some comments have been hidden by the post's author - find out more

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay