0

In this code i am passing both imageurl and size which are javascript variables but i am using in css. css is not considering both. How to resolve this issue ??

const MenuItem = ({title, imageUrl, size}) => {
    return(
        <div className="${size} menu-item">
            <div className="background-image" style={{backgroundImage: "url(${imageUrl})"}} />
            <div className="content">
                <h1 className="title">{title.toUpperCase()}</h1>
                <span className="subtitle">SHOP NOW</span>
            </div>
        </div>
    );
}
Akhilesh Mishra
  • 5,876
  • 3
  • 16
  • 32

3 Answers3

1

This should work:

const MenuItem = ({title, imageUrl, size}) => {
return(
    <div className={`${size} menu-item`}>
        <div className="background-image" style={{backgroundImage: "url(${imageUrl})"}} />
        <div className="content">
            <h1 className="title">{title.toUpperCase()}</h1>
            <span className="subtitle">SHOP NOW</span>
        </div>
    </div>
);
}
0

To use variables inside a string like this, you need to use the following syntax:

className={`${size} menu-item`}
dantheman
  • 3,189
  • 2
  • 10
  • 18
0

You should wrap them in back-ticks (``):

const MenuItem = ({title, imageUrl, size}) => {
    return(
        <div className=`${size} menu-item`>
            <div className="background-image" style={{backgroundImage: `${url(${imageUrl})}`}} />
            <div className="content">
                <h1 className="title">{title.toUpperCase()}</h1>
                <span className="subtitle">SHOP NOW</span>
            </div>
        </div>
    );

}
underscore
  • 6,495
  • 6
  • 39
  • 78