5 Ways To Handle CSS in React

 Introduction

There are three main component in web development HTML , CSS , Javascript where to include external , internal , inline  CSS is quite easy but when Facebook release the React there jsx is born which is a hybrid of javascript and html that makes to style component bit differently and many developers find it difficult to include CSS in React .
That's why in this blog we will discuss about some methods for how to include CSS in  React

Let's get started !

1. Internal CSS

Internal CSS is used when you want to design the particular element and which is quite straight forward . You just specify the attribute style and include CSS properties in the camelCase and values in the string to be placed in the object which enclosed in later jsx object which is used to style the element . You can also specify the pre-defined object of CSS in the in the jsx object. Look at the below example of using the internal CSS in react


  CSS styles may be event created in object and used in style attribute as shown below :-



2. External CSS ( CSS file )

Just like we create the CSS file traditionally and link in the html likely we do in react . We just create the external css file and import in the component we need to used also specify the className="" to the element that has specified in the css file.

Create a Css file and write some content


Import into the component to style the elements and specify the className which is defined in the CSS file


3. Styled Components

Styled components is the package which needs to be installed and then we can inherit css in the components with the pre-defined css . We will render components just like earlier but styled components. As shown in the example below,

Since it is a npm package , first you have to install in the react


Then style the components without even defined style in className and style attribute.
We use the styled object with any html element where we can start styling the component with the template literal introduced in ES6 . 


Then , we render the component but with some pre-defined style.

4. Using CSS Modules

We can create the CSS file with the name.module.css and import the classes into the component . Then , we just use the variable where we import the file and use the dot notation to extract the classes from the css module file. These modular files are used when using the big or complex projects and can create separate css module file for each module.

Create a Page.module.css with the css content


And import it into the component where you want to use


5. Tailwind CSS

Tailwind css is used for fast css implications where you just need to import the package and use the shortcuts for margin , padding , width , height etc. to enhance productivity in the website development



  Since the tailwind provides the utility first css class , it will provide the pre-defined css classes to interact.

Conclusion

So, in the conclusion we see the different methods of handling css into react where the inline and style components css method used to style the components quickly and which are not broad or lengthy for the element whereas the global css file , CSS module and tailwind css is used for the complex projects where you deal every component on the modular basis .

Thank you for reading :)

Comments

Popular posts from this blog

Learn React Router v6 ( Part - 2)

Why use Typescript ?

What is Single Page Application Routing ?