{"id":366452,"date":"2022-06-22T07:24:37","date_gmt":"2022-06-22T14:24:37","guid":{"rendered":"https:\/\/css-tricks.com\/?p=366452"},"modified":"2022-06-22T07:24:39","modified_gmt":"2022-06-22T14:24:39","slug":"different-ways-to-write-css-in-react","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/different-ways-to-write-css-in-react\/","title":{"rendered":"Different Ways to Write CSS in React"},"content":{"rendered":"\n
We\u2019re all familiar with the standard way of linking up a stylesheet<\/a> to the Turns out there are several ways to go about styling a React application. Some overlap with traditional styling, others not so much. But let\u2019s count all the ways we can do it.<\/p>\n\n\n\n\n\n\n As the name suggests, React can import CSS files. The process is similar to how we link up CSS file in the HTML Like this:<\/p>\n\n\n\n That usually goes at the top of the file where other imports happen:<\/p>\n\n\n\n In this example, a CSS file is imported into an You may be used to hearing that inline styling isn\u2019t all that great for maintainability and whatnot, but there are definitely situations (here\u2019s one!<\/a>) where it makes sense. And maintainability is less of an issue in React, as the CSS often already sits inside the same file anyway.<\/p>\n\n\n\n This is a super simple example of inline styling in React:<\/p>\n\n\n\n A better approach, though, is to use objects:<\/p>\n\n\n\n Let\u2019s see that in context:<\/p>\n\n\n\n This example contains a Note that curly brackets are used when referencing styles rather than the quotation marks we\u2019d normally use in plain HTML.<\/p>\n\n\n CSS Modules<\/a>\u2026 what the heck happened to those, right? They have the benefit of locally scoped variables and can be used right alongside React. But what are they, again, exactly?<\/p>\n\n\n\n<head><\/code> of an HTML doc, right? That\u2019s just one of several ways we\u2019re able to write CSS. But what does it look like to style things in a single-page application (SPA), say in a React project?<\/p>\n\n\n\n
Importing external stylesheets<\/h3>\n\n\n
<head><\/code>:<\/p>\n\n\n\n
import \".\/style.css\";<\/code><\/pre>\n\n\n\n
import { React } from \"react\";\nimport \".\/Components\/css\/App.css\";\nfunction App() {\n return (\n <div className=\"main\">\n <\/div>\n );\n}\nexport default App;<\/code><\/pre>\n\n\n\n
App.js<\/code> from the
\/Components\/css<\/code> folder.<\/p>\n\n\n
Write inline styles<\/h3>\n\n\n
<div className=\"main\" style={{color:\"red\"}}><\/code><\/pre>\n\n\n\n
style<\/code> attribute and then select the property to style.<\/li><\/ol>\n\n\n\n
import { React } from \"react\";\nfunction App() {\n const styles = {\n main: {\n backgroundColor: \"#f1f1f1\",\n width: \"100%\",\n },\n inputText: {\n padding: \"10px\",\n color: \"red\",\n },\n };\n return (\n <div className=\"main\" style={styles.main}>\n <input type=\"text\" style={styles.inputText}><\/input>\n <\/div>\n );\n}\nexport default App;<\/code><\/pre>\n\n\n\n
styles<\/code> object containing two more objects, one for the
.main<\/code> class and the other for a text input, which contain style rules similar to what we\u2019d expect to see in an external stylesheet. Those objects are then applied to the
style<\/code> attribute of elements that are in the returned markup.<\/p>\n\n\n\n
Use CSS Modules<\/h3>\n\n\n