2

How can I attach both style1 & style2 to the div below using the Emotion library?


const style1 = css`
  display: flex;
`

const style2 = css`
   width: 0;
`

const el= () => (
    <div css={style1}>
)

2 Answers 2

2

In Emotion you can do what's called composition:

const style1 = css`
  display: flex;
`

const style2 = css`
   width: 0;
`

const el= () => (
    <div 
        css={css`
            ${style1};
            ${style2};
        `}
    >
)
Sign up to request clarification or add additional context in comments.

Comments

0

The above solution can be simplified as follows:

const el= () => (
    <div css={[style1, style2]}>
)

This type of composition for CSS objects is also in the documentation (but not easy to find): https://emotion.sh/docs/best-practices#method-1-export-css-objects

const style1 = css({
  display: flex;
})

const style2 = css({
  width: 0;
})

const combinedStyles = css([style1, style2])

const el= () => (
    <div css={combinedStyles}>
)

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.