How to add styling in ReactJS
Introduction to ReactJS
ReactJS is a popular JavaScript library used for building user interfaces, especially single-page applications. Think of it as a big set of LEGO blocks that you can put together in numerous ways to create interactive web pages. But just like LEGO blocks, ReactJS components can look dull without the right colors or, in our case, styling.
What is Styling in ReactJS?
Styling in ReactJS is akin to giving colors to our LEGO blocks. It's how we make our website look good and feel good. In traditional HTML, we use CSS (Cascading Style Sheets) to style our components. CSS is like a set of rules that dictate how elements on our webpage should look.
In ReactJS, we style our components a bit differently but don't worry, it's not complicated. It's just like learning a new way to paint our LEGO blocks. Let's dive into it.
Inline Styling in ReactJS
In ReactJS, the first way we can add styles to our components is through inline styling.
import React from 'react';
const MyComponent = () => {
const style = {
color: 'blue',
fontSize: '20px',
};
return <p style={style}>Hello, world!</p>;
};
export default MyComponent;
In the code above, we've created an object called style
that contains CSS properties. We then added this style
object to our paragraph tag <p>
using the style
attribute. Notice how we used camel case (fontSize
) instead of hyphen-separated syntax (font-size
) as we would in CSS. It's one of the quirks of ReactJS, but you'll get used to it.
Using External CSS Files
While inline styling is straightforward, it's not always practical. Imagine having a giant LEGO project with hundreds of blocks. Coloring each block individually can be a hassle. This is where external CSS files come in handy.
Let's create a new CSS file MyComponent.css
:
.myParagraph {
color: blue;
font-size: 20px;
}
Now, we can import this CSS file into our ReactJS component:
import React from 'react';
import './MyComponent.css';
const MyComponent = () => {
return <p className="myParagraph">Hello, world!</p>;
};
export default MyComponent;
In this code, we've linked our CSS file and used the className
attribute (equivalent to the class
attribute in HTML) to apply the .myParagraph
style to our paragraph.
CSS Modules
What if you've a huge LEGO project with thousands of blocks and you want to avoid style conflicts between them? Here, CSS modules can help.
A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. It's like having separate paint boxes for different sections of your LEGO project.
Let's rename our MyComponent.css
to MyComponent.module.css
and import it in our component:
.myParagraph {
color: blue;
font-size: 20px;
}
import React from 'react';
import styles from './MyComponent.module.css';
const MyComponent = () => {
return <p className={styles.myParagraph}>Hello, world!</p>;
};
export default MyComponent;
In this code, we've imported our CSS module as styles
and used it to style our paragraph. Each class in styles
is unique and scoped locally to MyComponent
, avoiding conflicts with other components.
Styled-components
Sometimes, we want our LEGO blocks to have unique, dynamic colors. This is where styled-components
comes in. It's a library that allows us to write actual CSS in our JavaScript.
First, we need to install the library:
npm install styled-components
Then, we can use it in our component:
import React from 'react';
import styled from 'styled-components';
const StyledParagraph = styled.p`
color: blue;
font-size: 20px;
`;
const MyComponent = () => {
return <StyledParagraph>Hello, world!</StyledParagraph>;
};
export default MyComponent;
In this code, we've created a StyledParagraph
with styled-components
and used it instead of the standard p
tag. We can add any CSS we want between the backticks `
, giving us great flexibility in styling our components.
Conclusion
Styling in ReactJS is like painting our LEGO blocks. We can do it inline, use external CSS files, CSS modules, or even styled-components
, depending on our project's needs. Each method has its pros and cons, but all of them give us the power to create beautiful, interactive web pages.
With practice, you'll find your preferred way of styling and become adept at making your ReactJS components look the way you want. Happy coding!