Background image won’t render in my React styled-component
I’m working on converting a Figma design into a React component and running into an issue where the background image isn’t showing up. I’ve tried using both background and background-image CSS properties but neither seems to work. The image should appear as a blue background but it’s just not rendering at all.
import React from "react";
import styled from "styled-components";
import heroImage from "../assets/hero-bg.png";
import cardImage1 from "../assets/card-photo-1.png";
import cardImage2 from "../assets/card-photo-2.png";
import HeadingText from "../components/HeadingText.styled";
import DescriptionText from "../components/DescriptionText.styled";
import ActionButton from "../components/ActionButton.styled";
const MainContainer = styled.section`
margin-top: 8rem;
width: 100vw;
height: 90vh;
background: url(${heroImage});
background-size: cover;
background-position: center;
`;
const ContentWrapper = styled.div`
display: flex;
padding-left: 2rem;
`;
const ImageContainer = styled.div`
flex: 1;
`;
const TextContent = styled.div`
margin-left: 5rem;
margin-top: 12rem;
flex: 1;
`;
const TitleSection = styled.div`
margin-bottom: 2rem;
`;
const DescriptionSection = styled.div`
margin-top: 1.5rem;
max-width: 45rem;
`;
const ButtonSection = styled.div`
margin-top: 3rem;
`;
const SecondRow = styled.div`
display: flex;
margin-top: 4rem;
align-items: center;
`;
const RightContent = styled.div`
margin-left: auto;
margin-right: 8rem;
text-align: right;
`;
const HeroSection = () => {
return (
<MainContainer>
<ContentWrapper>
<ImageContainer>
<img src={cardImage2} alt="Feature showcase" />
</ImageContainer>
<TextContent>
<TitleSection>
<HeadingText>
Build Amazing
<br /> Digital Experiences
</HeadingText>
</TitleSection>
<DescriptionSection>
<DescriptionText>
Create powerful applications with our flexible platform. Whether you're building from scratch or extending existing functionality, our tools provide everything you need to succeed in the digital marketplace.
</DescriptionText>
</DescriptionSection>
<ButtonSection>
<ActionButton variant="primary" size="large">
Get Started Today
</ActionButton>
</ButtonSection>
</TextContent>
</ContentWrapper>
<SecondRow>
<RightContent>
<TitleSection>
<HeadingText>
Join Our
<br /> Developer Network
</HeadingText>
</TitleSection>
<DescriptionSection>
<DescriptionText>
Connect with thousands of developers worldwide. Our community offers support, resources, and networking opportunities to help you grow your skills and career.
</DescriptionText>
</DescriptionSection>
<ButtonSection>
<ActionButton variant="primary" size="large">
Explore Resources
</ActionButton>
</ButtonSection>
</RightContent>
<ImageContainer>
<img src={cardImage1} alt="Community network" />
</ImageContainer>
</SecondRow>
</MainContainer>
);
};
export default HeroSection;
Has anyone encountered this before? What could be preventing the background image from displaying properly?