> For the complete documentation index, see [llms.txt](https://tkssharma.gitbook.io/react-training/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tkssharma.gitbook.io/react-training/miscellaneous-items-day-11/styling-react-component.md).

# Styling React Component

### Using classes and CSS <a href="#using-classes-and-css" id="using-classes-and-css"></a>

The first and most simple is to use classes, and use a normal CSS file to target those classes:

```
const Button = () => {
  return <button className="button">A button</button>
}
```

```
.button {
  background-color: yellow;
}
```

You can import the stylesheet using an import statement, like this:

```
import './style.css'
```

and [Webpack](https://flaviocopes.com/webpack/) will take care of adding the CSS property to the bundle.

### Using the style attribute <a href="#using-the-style-attribute" id="using-the-style-attribute"></a>

A second method is to use the `style` attribute attached to a JSX element. Using this approach you don’t need a separate CSS file.

```
const Button = () => {
  return <button style={{ backgroundColor: 'yellow' }}>A button</button>
}
```

CSS is defined in a slightly different way now. First, notice the double curly brackets: it’s because `style` accepts an object. We pass in a JavaScript object, which is defined in curly braces. We could also do this:

```
const buttonStyle = { backgroundColor: 'yellow' }
const Button = () => {
  return <button style={buttonStyle}>A button</button>
}
```

When using `create-react-app`, those styles are autoprefixed by default thanks to its use of [Autoprefixer](https://github.com/postcss/autoprefixer).

Also, the style now is camelCased instead of using dashes. Every time a CSS property has a dash, remove it and start the next word capitalized.

Styles have the benefit of being local to the component, and they cannot leak to other components in other parts of the app, something that using classes and an external CSS file can’t provide.

### Using CSS Modules <a href="#using-css-modules" id="using-css-modules"></a>

**CSS Modules** seem to be a perfect spot in the middle: you use classes, but CSS is scoped to the component, which means that any styling you add cannot be applied to other components without your permission. And yet your styles are defined in a separate CSS file, which is easier to maintain than CSS in JavaScript (and you can use your good old CSS property names).

Start by creating a CSS file that ends with `.module.css`, for example `Button.module.css`. A great choice is to give it the same name as the component you are going to style

Add your CSS here, then import it inside the component file you want to style:

```
import style from './Button.module.css'
```

now you can use it in your JSX:

```
const Button = () => {
  return <button className={style.content}>A button</button>
}
```

That’s it! In the resulting markup, React will generate a specific, unique class for each rendered component, and assign the CSS to that class, so that the CSS is not affecting other markup.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tkssharma.gitbook.io/react-training/miscellaneous-items-day-11/styling-react-component.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
