Quick Introduction to Styling React Components with Emotion
In this article, we will see how to style React components with Emotion. We will learn how to add, configure, and use Emotion in React applications.We can define styling for our React components directly in the JavaScript code without using CSS files. This is a great way to apply dynamic styling and to compose multiple styles together. In React, we can use inline styling using the style property that is available for all components. There are drawbacks to this approach though. It is hard to compose inline styles, nested selectors and media queries are not supported, there is no auto-prefixing, and no theming support. Emotion is a library that helps to write CSS styles in JavaScript. It combines the advantages of inline styling with the power of CSS files. Emotion allows us to define dynamic styling, supports styles composition and themes. It also integrates nicely with React which makes Emotion a good choice for styling in React applications, especially when building reusable components. For this article, we assume that you're familiar with React components basics, such as working with component's properties. Let's start by adding Emotion to a project. There are several ways you can use Emotion in React projects. We'll be using the styled function to apply styling to React elements. This approach works well with React and does not require additional configuration. There is also the css property . It integrates with React components but requires additional Babel configuration. This makes it difficult to use in projects based on Create React App as CRA does not support custom Babel configurations. We can use the css property in small projects and projects where you can modify Babel configuration. It is also possible to use emotion in projects without React. You may check out the documentation for the emotion package for additional guidance. We need to add two packages to use Emotion with React: Now, we can use Emotion to apply styling to React components: We use styled to create a React component with custom styling applied to it. We can call styled by specifying the name of an element or a React component. For standard elements, the function also provides handy utility functions. For example, the following calls are the same. Both will return a new React component that will render a button with custom background color: We also provide the string with CSS properties to apply, just as in CSS files. The function returns the StyledButton React component. This is the component based on the button element with additional styles applied. Finally, we use the StyledButton component to render a button with our custom styling. There are two ways to declare styles with the styled function. In our example, we use a tagged template literal . We could also use an object with the inline styling naming rules . The following declarations are identical and you can select the one you prefer: With Emotion, we can use media queries, nested and pseudo selectors. CSS rules will be automatically auto-prefixed as well. We can have the dynamic configuration of the inline styling and power of CSS selectors at the same time. For example, let's declare a custom button component with nested selectors and dynamic configuration: In this example, we have got a more complex button component. We're using the :hover pseudo-selector to add styles for hovering. There's also the media query defined that makes the font-size larger on big screens. The StyledComponent also contains the <span> element with text. We're using the nested CSS rule for span to define the font-family and color properties. The value of the color property comes from props that can be passed to the StyledComponent . We use this property while rendering to create two buttons with different text colors. Emotion is a library for styling React components with JavaScript. We can declare powerful React components with dynamic styling and complex CSS rules. In this article, we gave a quick overview of the Emotion library. There are more features that we haven't covered in this article. We encourage you to learn more about the Emotion and start using it in your React projects.