Slide 1
-----------
Part 1: Let's explore how can we add styles to React components.
Slide 2
-----------
Part 1: In React, you have multiple options for how to style your components using CSS. In this section, we explore CSS files, Inline styles, CSS modules and Tailwind.js.
Part 2: 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.
Slide 3
-----------
Part 1: Let's first define a simple CSS file and store it in the "styles (dot) css" file.
Part 2: You have two options how to include this file in your React project.
Part 3: If you store your file in the web accessible location, you can include this file in your HTML file.
Part 4: 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.
Slide 4
-----------
Part 1: Using CSS files in your application creates global classes that are shared in your application. But how do we use them?
Part 2: 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.
Part 3: Instead, we use prop named className to specify the CSS class.
Part 4: When we execute this code, we see the formatted header.
Slide 5
-----------
Part 1: 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.
Part 2: In HTML, you would specify styles in this way. Unfortunately, React does not know how to parse the style string.
Part 3: Instead, in React, for increased type safety and control, we use the object notation with CSS style names in the Pascal case.
Part 4: You can also easily create reusable styles and share it with multiple components
Part 5: 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.
Slide 6
-----------
Part 1: 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.
Part 2: To create a CSS module you rename the styles.css to styles.module.css and import it in your component.
Part 3: You can then access individual class names using the dot notation.
Part 4: 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.
Slide 7
-----------
Part 1: Let's now talk about the new kid on the block TailwindJS, which quickly gained traction among developers.
Part 2: Tailwind is a utility-first CSS framework that lets you style components directly in JSX using predefined classes.
Part 3: It simplifies styling, speeds up development, and helps maintain consistency. It also produces static HTML when building your application, with no need for Javascript.
Part 4: Compare these two examples and think which one you prefer. As with any other library it comes down to preference!
Slide 8
-----------
Part 1: Installation with tailwind 4.0 is seamless
Part 2: First, create your project and install the necessary packages.
Part 3: Then, configure the postcss package to use the tailwind library. PostCSS extracts and compiles all your CSS files into static CSS, resulting in superior performance.
Part 4: Lastly, import the tailwind from your global.css file. And just like that, you are ready to write some concise and intuitive CSS!
Slide 9
-----------
Part 1: Let's check out how Tailwind utility classes work
Part 2: Each tailwind utility class starts with zero or more prefixes, followed by the class identifier and parameters separated by hyphens.
Part 3: Consider this utility class with identifier text and two parameters, red and 200. This class sets the colour of the text to red with a shade of 200.
Part 4: This class uses the prefix named hover, identifier text and two parameters, red and 500. In this case, the color of the text when mouse if over the text will be set to red, with a shade of 500.
Part 5: Utility classes can have a variable number of parameters and prefixes, such as this one with one parameter ...
Part 6: ... this one with no parameters at all.
Part 7: ... or this one with multiple prefixes
Part 8: We strongly recommend you check the official documentation of Tailwind to discover its full potential. But, for now, let's explore a few utility classes that we will use in this class.
Slide 10
-----------
Part 1: The first utility class is text, which sets the color of the text. It takes two parameters: the name of the colour and its shade. All colours can be customised on the CSS to adapt to the company palette. You can even define your custom colours.
Part 2: Then, there is the utility class bg, setting the background of the element ...
Part 3: ... and the p class, setting the padding of the element. The p class has multiple variations, such as px for setting horizontal padding, or py for setting vertical padding. Similarly, there is the m class setting the margin of the element.
Slide 11
-----------
Part 1: Please pause the presentation and practice your tailwind skills!
Slide 12
-----------
Part 1: For typography, there are multiple utility classes. The variation of utility class text with one parameter sets the size. The sizes, such as sm, md, lg or xl can be customised in the tailwind configuration. Other utility classes, such as font modify further properties of text.
Slide 13
-----------
Part 1: Please pause the presentation and practice your tailwind skills!
Slide 14
-----------
Part 1: Tailwind also has full support for flexbox and grid layouts, making layout creation a breeze. In the following slides we'll show you how to create responsive layouts easily.
Slide 15
-----------
Part 1: Please pause the presentation and practice your tailwind skills!
Slide 16
-----------
Part 1: Tailwind excels when making responsive designs. For such purposes, Tailwind uses breakpoints such as md, or lg that specify styles based on the current window size.
Slide 17
-----------
Part 1: Please pause the presentation and practice your tailwind skills!
Slide 18
-----------
Part 1: As an exercise, try this coding challenge. Your task is to create a responsive card component using Tailwind. The button should change color when hovering and the card width should be adjusted based on screen size. Please pause the presentation and do it! Once you are done, click the Share button next to the Tailwind Play and paste your link in the comments below!