Introduction

How To Apply Styling On React Components
React supports four types of options to style components.
CSS Stylesheet
You can simply define CSS into a separate file and then you just need to import in the component file. You can even create a separate component file rather than merging all CSS into one.
When a React application is made of complex CSS Modules or regular, CSS stylesheets is recommended.
  1. .container {
  2. margin: 40px;
  3. border: 5px dotted pink;
  4. }
  5. .center {
  6. font-size: 15px;
  7. text-align: center;
  8. }
  1. import React from 'react';
  2. import './style.css';
  3. const App = () => (
  4. <div className="container">
  5. <p className="center">Get started with CSS styling</p>
  6. </div>
  7. );
  8. export default App;
CSS Modules
A CSS Module is a CSS file with all class names and animation names are scoped locally.

We import the CSS file import styles './style.css'
  1. :local(.container) {
  2. margin: 40px;
  3. border: 5px dashed pink;
  4. }
  5. :local(.center) {
  6. font-size: 15px;
  7. text-align: center;
  8. }
  1. import React from 'react';
  2. import styles from './style.css';
  3. const App = () => (
  4. <div className={styles.container}>
  5. <p className={styles.center}>Getting started with CSS styling</p>
  6. </div>
  7. );
  8. export default App;
In case you're configured with webconfig.js file, then you can add below code into webconfig file so that it handles the styling of your local component.
  1. {
  2. test: /\.css$/,
  3. loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
  4. }
Inline styling
As the name suggests, in a React application, it's not similar to HTML inline styling string. In the React app, the styling property is not similar to a simple CSS property, we need to define it in camelcase.
  1. import React from 'react';
  2. const divStyle = {
  3. margin: '10px',
  4. border: '2px solid pink'
  5. };
  6. const pStyle = {
  7. fontSize: '15px',
  8. textAlign: 'center'
  9. };
  10. const Dialog = () => (
  11. <div style={divStyle}>
  12. <p style={pStyle}>Configured inline style</p>
  13. </div>
  14. );
  15. export default Dialog;
Styled-components
Styled-components is a component-level style that is a combination of JavaScript and CSS.

To use this library, we need to install the package first.
  1. npm install styled-components --save or yarn add styled-components
To use Style-component, please see the below snippet.
  1. import React from 'react';
  2. import styled from 'styled-components';
  3. const Div = styled.div`
  4. margin: 20px;
  5. border: 2px outset pink;
  6. &:hover {
  7. background-color: green;
  8. }
  9. `;
  10. const Paragraph = styled.p`
  11. font-size: 10px;
  12. text-align: center;
  13. `;
  14. const DialogBox = () => (
  15. <Div>
  16. <Paragraph>Styling with styled-components</Paragraph>
  17. </Div>
  18. );
  19. export default DialogBox;

Summary

All these were ways of styling React components. You can choose any of the options based on your requirements and preferences.