{"id":1667,"date":"2018-03-03T16:43:25","date_gmt":"2018-03-03T21:43:25","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1667"},"modified":"2018-03-14T22:11:40","modified_gmt":"2018-03-15T03:11:40","slug":"react-js-use-css-react","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/03\/react-js-use-css-react\/","title":{"rendered":"React: How to use CSS in React"},"content":{"rendered":"<p>In this article I will describe two ways to use CSS in React.<\/p>\n<h3>Option 1: Inline<\/h3>\n<p>Define CSS styles &#8216;inline&#8217; inside React. This uses the props key &#8216;style&#8217;, which uses a dictionary to define all of the CSS attributes. However, these CSS attributes have differences from normal CSS. For example,<br \/>\n1. The attribute names must be in camelCase, so the CSS attribute\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">font-family<\/code>would be changed to <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">fontFamily<\/code>.<br \/>\n2. The value portion of each attribute must be a string (surrounded by single quotes).<br \/>\n3. Each key-value pair is followed by a comma (since it is just another key-value pair inside the styles dictionary)<\/p>\n<p>Here is what the React code with inline styles looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function cell(props) {\r\n  return React.createElement(\r\n    'div', \r\n    {\r\n      style: {\r\n        justifyContent: 'center',\r\n        alignItems: 'center',\r\n        display: 'flex',\r\n        fontFamily: 'Arial',\r\n        fontSize: '3rem',\r\n        fontWeight: 'bold',\r\n        background: 'white'\r\n      },\r\n      id : props.number,\r\n      onClick: props.onClick\r\n    },                 \r\n  )\r\n}<\/pre>\n<p><strong>Note:<\/strong> Here is the syntax that you would use when using JSX. Notice the double curly braces:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;h1 style={{fontFamily:\"Arial\"}}&gt;React Tic-Tac-Toe&lt;\/h1&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h3>Option 2: CSS<\/h3>\n<p>Instead of defining the styles &#8216;inline&#8217; as shown above, you can use standard CSS. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.cell {\r\n  justify-content: center;\r\n  align-items: center;\r\n  display: flex;\r\n  font-family: Arial;\r\n  font-size: 3rem;\r\n  font-weight: bold;\r\n  background: white;\r\n}<\/pre>\n<p>In the React code, all we need to use this CSS class is a prop called className.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">className: \"cell\"<\/pre>\n<p>(We don&#8217;t need the style prop.)<br \/>\nHere is what the React code looks like without the inline styles:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function cell(props) {\r\n  return React.createElement(\r\n    'div', \r\n    {\r\n      className: \"cell\",\r\n      id : props.number,\r\n      onClick: props.onClick\r\n    },                 \r\n  )\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will describe two ways to use CSS in React. Option 1: Inline Define CSS styles &#8216;inline&#8217; inside React. This uses the props key &#8216;style&#8217;, which uses a dictionary to define all of the CSS attributes. However, these CSS attributes have differences from normal CSS. For example, 1. The attribute names must &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/03\/react-js-use-css-react\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">React: How to use CSS in React<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,111],"tags":[10,45,114,110],"class_list":["post-1667","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-react-js","tag-css","tag-javascript","tag-jsx","tag-react-js"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1667"}],"version-history":[{"count":7,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1667\/revisions"}],"predecessor-version":[{"id":1722,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1667\/revisions\/1722"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}