Full Stack Development

CSS Styles

by Tomas Trescak t.trescak@westernsydney.edu.au

React Fundamentals

CSS Styles

  • CSS files
  • Inline Styles
  • CSS Modules
  • Tailwind.js
  • CSS in JS

Styling React

CSS Classes

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

.header:hover {
    color: blue;
}

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

body {
  padding: 16px;
}

    

Styling React

CSS Classes

      function App() {
  return (
    <>
      <h1>
        Header
      </h1>
      <p>
        My Text
      </p>
    </>
  )
}
import { render } from 'react';
render(<App />)
    

Styling React

Inline Styles

      

function App() {
  return (
    <>
      <h1>
        Header
      </h1>
      <p>
        My Text
      </p>
    </>
  )
}
import { render } from 'react';
render(<App />)
    

Styling React

CSS Modules

      import "./styles.css";

function App() {
  return (
    <>
      <h1 className="header">
        Header
      </h1>
    </>
  )
}

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

Styling React

Tailwind.js

  • What is Tailwind CSS?
  • Why use Tailwind?
  • Example
    <button style={{
    backgroundColor: "var(--color-blue-700)",
    color: "white",
    padding: "4px 8px"
}}>
    Click Me
</button>
  
    <button className="
    bg-blue-500 
    text-white
    px-1 py-2
">
    Click Me
</button>
  
Inline Styles
Tailwind

Tailwind.js

Installation

  • Create Project and Install Tailwind
  • Configure postcss.config.mjs
  • Import tailwind into global.css
    pnpx create-next-app@latest tailwind --typescript --eslint --app 
pnpm install tailwindcss @tailwindcss/postcss postcss 
  
    const config = {
  plugins: {
    "@tailwindcss/postcss": {},
  },
};
export default config;
  
    @import "tailwindcss";
  

Tailwind / Utility Classes

Introduction

  • prefixes?:identifier-param1?-param2?...
    • text-red-200
    • hover:text-red-500
    • p-8
    • rounded
    • dark:hover:bg-blue-200

Tailwind / Utility Classes

Text and Background Colors

  • text-red-500 → Sets text colour
    • text-[colour]-[shade]
  • bg-gray-200 → Sets background colour
    • bg-[colour]-[shade]
  • p-4 → Adds padding
    • p-number (multiplications of 4)
    • p-[value] (e.g. p-[20px])
    • pt, pb, px ...

    <p className="text-red-500 bg-gray-200 p-4">Hello Tailwind!</p>
  

Playground

Tailwind / Utility Classes

Typography and Font Styling

  • text-3xl → Large font size
  • font-bold → Bold text
  • underline → Underlined text
    <h1 className="text-3xl font-bold underline">Welcome</h1>
  

Playground

Tailwind / Utility Classes

Flexbox & Layout

  • flex → Enables Flexbox
  • justify-center → Centers horizontally
  • items-center → Centers vertically
  • h-screen → Full height of screen

Playground

Tailwind / Utility Classes

Responsive Layout

  • Breakpoints: sm, md, lg, xl
  • Example: <div className="md:w-1/2 lg:w-1/3">
  • Tailwind makes mobile-first design easy!

Playground

Exercise