Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to inject css in ReactJS

Starting off with Cascading Style Sheets (CSS)

Before we dig into the mechanics of injecting CSS into ReactJS, let's take a moment to understand what CSS is. CSS, or Cascading Style Sheets, is a language we use to style our web pages. Think of it as the fashion designer of the web, deciding the colors, fonts, and layout of your website. It's what takes a basic HTML skeleton and makes it visually appealing.

Why Injecting CSS into ReactJS is Different

In traditional web development, CSS is often kept in separate files from the HTML and is linked to from within the HTML. However, when we start working with JavaScript libraries like ReactJS, things change a bit. ReactJS is all about components, reusable bits of user interface that we can slot together like Lego bricks to build a website or app. Each of these components is a blend of JavaScript, HTML, and you guessed it, CSS.

Inline Styling: The Simplest Way

The simplest way to apply CSS in React is to use inline styles. This is just like traditional inline styling in HTML, but with a JavaScript twist. Instead of writing styles as a string inside the style attribute, we provide a JavaScript object, with properties that correspond to the CSS we want to apply.

Here’s an example of inline styling in React:

const divStyle = {
  color: 'blue',
  backgroundImage: 'url(' + imgUrl + ')',

function HelloWorldComponent() {
  return <div style={divStyle}>Hello World!</div>;

In the code snippet above, we've defined a style object divStyle and then applied it to a div in our HelloWorldComponent by setting it as the style attribute's value. Remember, in React we use camel case property naming convention instead of hyphens in CSS property names. For instance, background-image becomes backgroundImage in React.

CSS Classes: A More Scalable Approach

If you've ever worked with CSS before, you know that defining styles inline can quickly get out of hand. That's where CSS classes come in. They let us define a style once and then apply it to as many elements as we want.

In React, we apply CSS classes using the className attribute (not class as in HTML due to ‘class’ being a reserved word in JavaScript). Here is how you might do it:

import './App.css';

function HelloWorldComponent() {
  return <div className="helloWorld">Hello World!</div>;

In the above example, we first import the CSS file. Then, we apply the helloWorld class to our div.

CSS Modules: Local Scope by Default

As our project grows, we may want to ensure that CSS classes are scoped locally by default to avoid naming collisions. This is where CSS Modules come in.

Here’s how you would use CSS Modules:

import styles from './App.module.css';

function HelloWorldComponent() {
  return <div className={styles.helloWorld}>Hello World!</div>;

In this example, we import our CSS as a module, and then use it as an object. The CSS classes are locally scoped by default and transformed into unique identifiers, so you don’t have to worry about naming collisions.

Styled Components: CSS in JS Library

Styled-components is a library for React and React Native that allows you to use component-level styles in your application that are written with a mixture of JavaScript and CSS. This approach enables you to fully take advantage of JavaScript's power (including its variables) in your styles.

Here's how you might create a styled component:

import styled from 'styled-components';

const StyledDiv = styled.div`
  color: blue;
  background-image: url(${props => props.imgUrl});

function HelloWorldComponent() {
  return <StyledDiv>Hello World!</StyledDiv>;

In the example above, we first import the styled-components library. Then, we create a new styled div. Finally, we use the StyledDiv as a component in our HelloWorldComponent.

Conclusion: The Art of Styling in ReactJS

Styling in ReactJS is like painting on a canvas. You have different brushes (inline styles, CSS classes, CSS Modules, Styled Components) at your disposal, each with its unique characteristics. Some are simple and straightforward to use; others offer more control and prevent clashes between styles. Choose the right brush depending on your project's needs, and you can paint a masterpiece that is both visually appealing and functionally robust.

Remember, every artist needs to practice to perfect their craft. So, don't be afraid to experiment and make mistakes along the way. Every line of code you write brings you one step closer to becoming a ReactJS styling maestro. Happy coding!