CSS Styles
by Tomas Trescakยท React

Root Folder
Not Attempted
NameProgress
Introduction
Not Read
๐Ÿฐ Player
Not Read
Setup
Not Attempted
NameProgress
Software Installation
Not Read
Project Setup
Not Read
Running and Testing
Not Read
React and ReactDOM
Not Read
๐Ÿ’ก Assignment 1: Welcome Message
Not Attempted
Submissions
Not Read
JSX and Components
Props
๐Ÿ‘พ Exercise: Props
Not Attempted
CSS Styles
useState and Hooks
๐Ÿ‘พ Exercise: useState
Not Attempted
Conditional Rendering
Lists
๐Ÿ‘พ Exercise: Lists
Not Attempted
Forms and Events
๐Ÿ‘พ Exercise: Forms
Not Attempted
๐Ÿ’ก Assignment 2: Front End
Not Attempted
Advanced React
Not Attempted
NameProgress
Pure Components - memo
Not Read
Lifecycle - useEffect
Not Read
Expensive? - useMemo
Not Read
DOM Interactions - useRef
Not Read
forwardRef
Not Read
useImperativeHandle
Not Read
๐Ÿ‘พ useImperativeHandle
Not Attempted
Context
Not Read
useCallback
Not Read
useId
Not Read
useReducer
Not Read
Infrastructure
Not Attempted
NameProgress
Database
Not Read
NextAuth and Github Authentication
Not Read
Prisma and ORM
Not Read
Project Setup
Not Read
Project Authentication
Not Read
APIs
Not Attempted
NameProgress
APIs
Not Read
APIs - Slides
Not Attempted
Rest APIs
Not Read
Rest APIs - Express.js
Not Read
ReastAPIs - Next.js
Not Read
Securing APIs
Not Read
Securing APIs - NextAuth
Not Read
tRPC
Not Attempted
NameProgress
tRPC
Not Read
tRPC - Routers
Not Read
tRPC - Server Rendering
Not Read
tRPC - Client Rendering
Not Read
Persisting Data
Not Read
Assignment 3: APIs
Not Read
0 / 1070 XP

Lecture Transcript
If you prefer text to video, check out the transcript of the presentation above

Let's explore how can we add styles to React components.

In React, you have multiple options for how to style your components using CSS. In this section, we explore CSS files, Inline styles and CSS modules.

We will not talk about CSS in JS, as your approach will depend on selection of the CSS library, such as Emotion or Styled Components.

Let's first define a simple CSS file and store it in the "styles (dot) css" file.

You have two options how to include this file in your React project.

If you store your file in the web accessible location, you can include this file in your HTML file.

But, in javascript frameworks such as NextJS, you will most probably import it in your main application file.

In simple applications you can use the HTML import, but try to use the javascript import for further optimisations.

Using CSS files in your application creates global classes that are shared in your application. But how do we use them?

In html we specify CSS classes using a class prop. Would you know what the problem is with that in JSX? Well, class is a reserved word in JavaScript, so we cannot use it.

Instead, we use prop named className to specify the CSS class.

When we execute this code, we see the formatted header.

The issue with global class names is that it can create naming conflicts with multiple classes sharing the same name. This is particularly dangerous if you use 3rd party ui libraries that introduce their classes in a global namespace. Alternatively, you can use inline styles to specify the look of your elements.

In HTML, you would specify styles in this way. Unfortunately, React does not know how to parse the style string.

Instead, in React, for increased type safety and control, we use the object notation with CSS style names in the Pascal case.

You can also easily create reusable styles and share it with multiple components

Running this code styles both the header and the paragraph. However, the issue with inline styles is that they are limited in their use, as they cannot specify advanced behaviours such as mouse over events or media queries for responsive styling.

CSS Modules combine the convenience and expressivity of CSS classes with creation of isolated class names. To enable CSS modules, you have to use compatible bundler or framework, such as NextJS.

To create a CSS module you rename the styles.css to styles.module.css and import it in your component.

You can then access individual class names using the dot notation.

Under the hood, the framework creates unique class names per each CSS module. For example, in the browser, you would see something similar to this. Consequently, you can use the same CSS class name across multiple CSS modules without worrying about creating conflicts.

Description
All the extra information about this section

In React, you have multiple options for how to style your components using CSS:

  1. CSS Classes and CSS files
  2. Inline Styles
  3. CSS Modules
  4. CSS in JS

In this section, we explore the first three as the latter depends on your selection of the CSS library you want to use, such as Emotion or Styled Components.

CSS Classes

Similar to vanilla javascript, you first need to define and include your CSS file in the project. Consider the following CSS file:

.header {
    font-weight: bold;
    color: salmon;
} 

.header:hover {
    color: blue;
}

.code {
    font-family: monospace;
    color: #333;
}

Consider the file name to be styles.css, you can include this file in the main index.html file:

<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

But, more often with web frameworks such as Next.js you would import this file in your entry level file such as app.tsx:

// file: app.tsx
import "./styles.css"

Using CSS files in your application creates global classes shared in your application. But how do we use this? In html we specify CSS classes using class such as <h1 class="header">. Would you know what the problem is with that in JSX?

Well, class is a reserved word in javascript, so we cannot use it. Therefore, we use className instead. For example:

function Greeting() {
    return <h1 className="header">Hello</h1>
}

The issue with global class names is that it can create conflicts. Particularly if you use 3rd party libraries that introcuce their own classes and class names.

Inline Styles

Alternatively, you can use inline styles to specify the look of your elements. In HTML, you specify styles in the following way:

<h1 style="font-weight: bold;font-size: 1em">Hello</h1>

In React, for increased type safety and control, we use object notation of CSS classes specified inside the styles prop. The example above would look in React as following:

<h1 style={{ fontWeight: 'bold', fontSize: "1em" }}>Hello</h1>

So, style prop expects an object with values of CSS styles as react does not know how to parse style strings. You can easily create reusable inline styles similar to this:

const header = { fontWeight: 'bold', fontSize: "1em" }

<h1 style={header}>Header 1</h1>
<h1 style={header}>Header 2</h1>

The issue with inline styles is that they are limited in their use, as they cannot specify advanced behaviours such as mouse over events, or media queries for responsive styling.

CSS Modules

CSS Modules combine the convenience and expressivity of CSS classes with creation of isolated class names. To enable CSS modules, you have to use compaible bundler or framework, such as Next.js. 

To create a CSS module you rename the styles.css to styles.module.css and import it in your component. You can then access individual class names using the .dot notation such as the following:

import styles from "./styles.module.css"

function Greeting() {
    return <h1 className={styles.header}>Hello<h1>
}

Under the hood, the framework creates unique class names per each css module. For example, in browser, you would see something similar to this (note the name of the class):

<h1 className="App_header_JQ3e45">Hello</h1>

Consequently, you can use the same name of the CSS class across multiple CSS modules, without worry to create conflicts. In Skillpies editor we do not support CSS modules, only global CSS files and inline styles. But, some of our external exercises will use CSS modules, so be on the lookout! 

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline