# Lesson 3 - CSS Review

## Styling UI with CSS Framework

<figure><img src="/files/lmK3jTYElUFhKj0xhlSF" alt="" width="375"><figcaption></figcaption></figure>

## **What is Bootstrap?**

* **Front-end Framework:** Bootstrap is a popular and open-source CSS framework that provides pre-built HTML, CSS, and JavaScript components for building responsive and mobile-first web applications.
* **Key Features:**
  * **Grid System:** A flexible and responsive grid system for creating layouts that adapt to different screen sizes.
  * **Pre-built Components:** A wide range of pre-styled components like buttons, forms, navigation bars, alerts, carousels, and more.
  * **Utilities:** A collection of utility classes for quickly applying styles like margins, padding, colors, and text styles.
  * **JavaScript Plugins:** Includes JavaScript plugins for interactive components like modals, tooltips, popovers, and more.
  * **Responsive Design:** Designed with mobile-first principles in mind, Bootstrap components adapt seamlessly to various screen sizes.

## Installing and Using Bootstrap in Vite

**Step 1: Install Bootstrap**

1. Install Bootstrap using npm: <https://getbootstrap.com/docs/5.3/getting-started/introduction/>&#x20;

   ```bash
   npm install bootstrap
   //or
   yarn add bootstrap
   ```

**Step 2: Include Bootstrap CSS and JavaScript in Vite**

1. In the `main.jsx` file, import Bootstrap's CSS:

   <pre class="language-jsx" data-line-numbers><code class="lang-jsx">//main.jsx
   import 'bootstrap/dist/css/bootstrap.min.css';
   import 'bootstrap/dist/js/bootstrap.bundle.min.js';
   import React from 'react';
   import ReactDOM from 'react-dom';
   import App from './App';

   ReactDOM.createRoot(document.getElementById('root')).render(
     &#x3C;React.StrictMode>
       &#x3C;App />
     &#x3C;/React.StrictMode>
   );
   </code></pre>
2. Bootstrap's JavaScript is bundled with Vite automatically.

***

## Use Cases of Bootstrap Components

**Div Example:**

```jsx
<div className="container">
  <h1>Hello Bootstrap</h1>
</div>
```

**Paragraph Example:**

```jsx
<p className="text-muted">This is a muted paragraph.</p>
```

**Anchor Example:**

```jsx
<a href="#" className="btn btn-primary">Click Me</a>
```

**Image Example:**

```jsx
<img src="https://via.placeholder.com/150" className="img-thumbnail" alt="Example" />
```

**Input Example:**

```jsx
<input type="text" className="form-control" placeholder="Enter text" />
```

**Button Example:**

```jsx
<button className="btn btn-success">Submit</button>
```

**Form Example:**

```jsx
<form>
  <div className="mb-3">
    <label htmlFor="exampleInputEmail1" className="form-label">Email address</label>
    <input type="email" className="form-control" id="exampleInputEmail1" />
  </div>
  <button type="submit" className="btn btn-primary">Submit</button>
</form>
```

**Navbar Example:**

```jsx
<nav className="navbar navbar-expand-lg navbar-light bg-light">
  <div className="container-fluid">
    <a className="navbar-brand" href="#">Navbar</a>
    <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
      <span className="navbar-toggler-icon"></span>
    </button>
    <div className="collapse navbar-collapse" id="navbarNav">
      <ul className="navbar-nav">
        <li className="nav-item">
          <a className="nav-link active" href="#">Home</a>
        </li>
        <li className="nav-item">
          <a className="nav-link" href="#">About</a>
        </li>
      </ul>
    </div>
  </div>
</nav>
```

### **Benefits of Using Bootstrap**

* **Faster Development:** Bootstrap provides pre-built components and a robust grid system, accelerating the development process.
* **Responsive Design:** Ensures your website looks good on all devices.
* **Cross-browser Compatibility:** Tested and works across major browsers.
* **Large Community:** A large and active community provides support, resources, and extensions.

**In Summary**

Bootstrap is a powerful tool for front-end developers. By leveraging its pre-built components, grid system, and customization options, you can quickly and efficiently create visually appealing and responsive web UIs.

## Tailwind CSS with Vite

### **What is Tailwind CSS?**

Tailwind CSS is a utility-first CSS framework that allows developers to create modern and responsive designs efficiently. Instead of pre-designed components, Tailwind provides low-level utility classes that enable custom styling while maintaining consistency and scalability.

### **Why Use Tailwind CSS?**

* **Utility-First Approach**: Allows for rapid UI development.
* **Customizability**: Tailwind is highly configurable and can be customized to fit project needs.
* **Performance**: Generates only the necessary CSS, making applications faster.
* **Responsive Design**: Includes responsive utilities to design mobile-friendly interfaces.

***

## **Step-by-Step Installation Guide for Tailwind CSS in a Vite React App**

### **Step 1: Create a Vite + React Project**

**ref site :**  [**https://tailwindcss.com/docs/guides/vite**](https://tailwindcss.com/docs/guides/vite)&#x20;

Before installing Tailwind CSS, you need to have a Vite React project. If you don’t have one, create it using the following command:

```bash
npm create vite@latest tailwind-css-app -- --template react
```

Navigate into your project directory:

```bash
cd tailwind-css-app
```

Install dependencies:

```bash
npm install
```

***

#### **Step 2: Install Tailwind CSS and Dependencies**

T

Initialize the Tailwind configuration files:

Install Tailwind CSS Vite plugin along with its necessary dependencies, run the following command:

```bash
npm install --save-dev @tailwindcss/vite
```

* vite.config.js : Modify your `vite.config.js` file to include the Tailwind CSS Vite plugin:

```javascript
// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig({
  plugins: [react(), tailwindcss()],
});
```

**Verify Tailwind CSS installation:** Make sure you've also installed Tailwind CSS itself and generated the configuration files:

```bash
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
```

This command will install the following:

* The Tailwind CSS framework
* Post CSS, which provides plugins to perform different functionalities like prefixes in Vanilla CSS
* Autoprefixer, which is a PostCSS plugin to parse CSS and add vendor prefixes to CSS rules

### Generate the Configuration Files <a href="#heading-step-4-generate-the-configuration-files" id="heading-step-4-generate-the-configuration-files"></a>

```
// Some code
npx tailwindcss init -p
```

This command generates `tailwind.config.cjs` and`postcss.config.cjs` configuration files, also known as config files. They help you interact with your project and customize everything.

### **Step 3: Configure Tailwind to Purge Unused Styles**

Modify `tailwind.config.js` to specify the paths to all template files:

```jsx
/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};
```

This configuration ensures Tailwind CSS removes unused styles in production, making the CSS file smaller and more optimized.

***

### **Step 4: Add Tailwind CSS to Your Stylesheet**

In your `src` folder, locate `index.css` (or create one if missing) and include the Tailwind directives at top:

```css

/* src/styles.css */
@import "tailwindcss";

```

These directives import Tailwind's base styles, components, and utility classes.

These three lines act as directives that tell Tailwind to inject its different style layers into your CSS, enabling you to use its pre-defined styles and utility classes throughout your project. They must be present in your main CSS file (usually `index.css`) for Tailwind to function.

***

### **Step 5: Use Tailwind CSS in Your React Components**

Now, you can start using Tailwind classes directly in your React components. For example, modify `App.jsx`:

```jsx
function App() {
  return (
    <div className="min-h-screen flex items-center justify-center bg-gray-100">
      <h1 className="text-4xl font-bold text-blue-500">Hello, Tailwind CSS with Vite!</h1>
      <p className="mt-4 text-lg text-gray-700">This is styled using Tailwind in a Vite React app.</p>
      <button className="mt-6 px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-700">Click Me</button>
    </div>
  );
}

export default App;
```

***

Challenge: &#x20;

```
CHALLENGE: Style the menu to look like the mockup
        - Position and place the menu in the center of the viewport using Flexbox
        - Using Flexbox, center-align and space the menu contents
        - Display and space the 3 price-points side by side
        - Display and space the Extras side by side, wrapping if needed. 
        - Size the titles
```

<figure><img src="/files/POYDorjypUaVNgBzhL0t" alt="" width="255"><figcaption></figcaption></figure>

```jsx
// App.jsx - updated
import './App.css'

function App() {
  

  return (
    <>
      <div className=" items-center justify-center bg-gray-100">
        <h3 className="text-4xl font-bold text-blue-700">
          Hello, Tailwind CSS with Vite!
        </h3>
        <p className="mt-4 text-lg text-orange-700">
          This is styled using Tailwind in a Vite React app.
        </p>
        <button className="mt-6 px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-700">
          Click Me
        </button>
      </div>
           <main className="w-1/2 bg-green-900 text-white font-mono p-12 flex flex-col items-center justify-center gap-6 mx-auto">
            
            <h1 className="text-4xl font-bold">Coffee</h1>
            <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#ffffff"><path d="M160-120v-80h640v80H160Zm160-160q-66 0-113-47t-47-113v-400h640q33 0 56.5 23.5T880-760v120q0 33-23.5 56.5T800-560h-80v120q0 66-47 113t-113 47H320Zm0-80h240q33 0 56.5-23.5T640-440v-320H240v320q0 33 23.5 56.5T320-360Zm400-280h80v-120h-80v120ZM320-360h-80 400-320Z"/>
            </svg>
            <section>
                <h2 className="text-xl mb-1">Cappuccino</h2>
                <section className="flex gap-8">
                    <p>$3.60 (S)</p>
                    <p>$4.20 (M)</p>
                    <p>$5.00 (L)</p>
                </section>
            </section>
            <section>
                <h2 className="text-xl mb-1">Flat White</h2>
                <section className="flex gap-8">
                    <p>$3.60 (S)</p>
                    <p>$4.20 (M)</p>
                    <p>$5.00 (L)</p>
                </section>
            </section>
            <section>
                <h2 className="text-xl mb-1">Latte</h2>
                <section className="flex gap-8">
                    <p>$3.60 (S)</p>
                    <p>$4.20 (M)</p>
                    <p>$5.00 (L)</p>
                </section>
            </section>
            <section>
                <h2>Extras ($3ea)</h2>
                <section className="flex flex-wrap gap-x-6 justify-center">
                    <p>Cream</p>
                    <p>Wafer</p>
                    <p>Shortbread</p>
                    <p>Marshmellow</p>
                    <p>Chocolate square</p>
                    <p>Cinnamon Stick</p>
                </section>
            </section>
        </main>
    </>
  )
}

export default App
```

```jsx
// snippet of App.jsx
<main className="w-1/2 bg-green-900 text-white font-mono p-12 flex flex-col items-center justify-center gap-6 mx-auto">
```

#### Explanation:

* <mark style="color:orange;">`w-1/2`</mark>: Sets the width of the `<main>` element to half of its parent container.
* <mark style="color:orange;">`mx-auto`</mark>: Centers the element horizontally by setting automatic margins on the left and right.
* `f`<mark style="color:orange;">`lex`</mark><mark style="color:orange;">,</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`flex-col`</mark><mark style="color:orange;">,</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`items-center`</mark><mark style="color:orange;">,</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`justify-center`</mark>: These classes ensure that the content inside the `<main>` element is centered both vertically and horizontally.

This setup will ensure that the `<main>` element is centered and takes up half the width of its parent container.

<details>

<summary>References</summary>

</details>

<br>

App.jsxAppGPT-4o⇧⏎ New Line⏎ Send

### **Step 6: Start the Development Server**

To see your Tailwind-styled app in action, run the Vite development server:

```bash
npm run dev
```

This will start your application at `http://localhost:5173/` (or another available port).

***

### **Comparison and Best Practices:**

* **Tailwind CSS:**
  * Utility-first framework, customizable, better for fully custom designs.
  * Requires adding classes directly to HTML elements.
* **Bootstrap:**
  * Component-based, comes with pre-styled components, great for rapid prototyping.
  * Ideal when you need a quick layout with minimal custom styling.

### **Conclusion**

You have successfully set up Tailwind CSS in your Vite-powered React application! Now, you can utilize Tailwind’s utility classes to build modern, responsive, and maintainable UIs with ease.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reactjs.koida.tech/react-in-the-beginning/lesson-3-css-review.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
