React CSS Styling

React CSS Styling


React is a popular JavaScript library for building user interfaces. One of the key features of React is its ability to easily style components using CSS. In this tutorial, we will cover the basics of React CSS styling and provide some examples to help you get started.

To begin, let's create a simple React component called "MyComponent". This component will have a single div element that displays some text. In order to style this component, we can create a CSS file called "MyComponent.css" and import it into our React component.

Here is an example of how to import the CSS file into your React component:

In the above code, we are importing the "MyComponent.css" file and adding the "my-component" class to our div element. Now we can add some CSS styles to our "MyComponent.css" file

In this example, we are setting the text color of our div element to red and the font-size to 24px.

Another way of styling the components is by using inline styling. Inline styling is done by passing a style object to the JSX element

It is important to note that when using inline styles, the CSS properties must be written in camelCase and numeric values should not include "px"

In addition to basic styling, React also provides a library called "styled-components" which allows you to create reusable, stylable components. This is done by creating a "styled" version of a component, and then applying CSS styles to that component.

Here is an example of how to use "styled-components" to style a button

In this example, we are creating a "styled" version of the button component and applying some basic CSS styles to it.

These are the basic concepts of styling React components with CSS. You can also use pre-processors like SASS, LESS etc to write CSS. Styling in React can be done in various ways as shown above and choose the best that suits your needs.