Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

ReactJS How to pass in style to div

Understanding the Basics

In this blog post, we are going to explore the subject of passing styles to div elements in ReactJS. If you're new to programming, it might seem a little complicated. But don't worry, we'll take it step by step. Think of it this way: If programming was cooking, then ReactJS is just one of the many recipes you can choose to use. And just like in cooking, the ingredients (in our case, the codes) need to be put together in a certain way to get the result you want.

What is a div?

Before we get started, let's first understand what a div is. In HTML, div is a tag that we use to create a division or a section in a web page. It’s like a container for other elements. Imagine you’re packing for a trip, a div would be like your suitcase, and the clothes and other items you want to bring would be the other elements.

The Inline Styling Method

Now, how do we pass style to this suitcase (div) in ReactJS? One way is through inline styling. Here's an example:

<div style=>
  I'm a div with blue text on a yellow background!
</div>

In this case, the style attribute is used to add styles directly within the div tag. The styles are written as JavaScript objects, with the style properties (like color and backgroundColor) as the keys and the desired styles (like "blue" and "yellow") as the values.

Using External CSS

Another way to pass style to a div is through an external CSS file. This is like having a fashion designer (the CSS file) design your outfit (the div) for you. Here's how you can do it:

First, create a CSS file. Let's call it App.css:

.myDiv {
  color: white;
  background-color: black;
}

Then, import the CSS file in your JavaScript file and use the class name in your div:

import './App.css';

// ...

<div className="myDiv">
  I'm a div with white text on a black background!
</div>

In this example, .myDiv is a CSS class that we defined in App.css, and we're using it in our div by setting className="myDiv".

Using Styled Components

Apart from the above two methods, there's another popular method called styled-components. To understand this, let’s imagine the div as a house. Styled-components would be like giving this house a complete makeover, making it unique and distinct from other houses.

To use styled-components, you first need to install it:

npm install styled-components

Then, you can create a styled div like this:

import styled from 'styled-components';

const StyledDiv = styled.div`
  color: red;
  background-color: green;
`;

// ...

<StyledDiv>
  I'm a div with red text on a green background!
</StyledDiv>

In this example, StyledDiv is a new component that we created with styled-components. It's a div with some styles, and we can use it like any other React component.

Conclusion: The Art of Styling

Styling in ReactJS, or any other programming language, is much like painting a picture. Each div is a canvas, and the styles are the colors you paint with. The methods we've discussed in this blog post - inline styling, external CSS, styled-components - are like different types of brushes you can use. They each have their pros and cons, and the one you choose depends on what you're comfortable with and what the situation requires.

In the end, passing style to a div is just one part of the art of programming. There's much more to learn and explore. So, keep experimenting, keep learning, and most importantly, keep coding! Remember, every renowned artist was once a novice. So, embrace your inner artist and paint your masterpiece with code!