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>
    );
}
1
  • do you see the size class inside your HTML? or menu-item is all you see? Commented Sep 6, 2020 at 12:20

3 Answers 3

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>
);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

className={`${size} menu-item`}

1 Comment

single quotation marks instead of double quotation marks ??
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>
    );

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.