Lesson 3 - CSS Review
Last updated
Last updated
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.
Step 1: Install Bootstrap
Step 2: Include Bootstrap CSS and JavaScript in Vite
In the main.jsx
file, import Bootstrap's CSS:
Bootstrap's JavaScript is bundled with Vite automatically.
Div Example:
Paragraph Example:
Anchor Example:
Image Example:
Input Example:
Button Example:
Form Example:
Navbar Example:
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 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.
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.
Before installing Tailwind CSS, you need to have a Vite React project. If you don’t have one, create it using the following command:
Navigate into your project directory:
Install dependencies:
T
Initialize the Tailwind configuration files:
Install Tailwind CSS Vite plugin along with its necessary dependencies, run the following command:
vite.config.js : Modify your vite.config.js
file to include the Tailwind CSS Vite plugin:
Verify Tailwind CSS installation: Make sure you've also installed Tailwind CSS itself and generated the configuration files:
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
This command generates tailwind.config.cjs
andpostcss.config.cjs
configuration files, also known as config files. They help you interact with your project and customize everything.
Modify tailwind.config.js
to specify the paths to all template files:
This configuration ensures Tailwind CSS removes unused styles in production, making the CSS file smaller and more optimized.
In your src
folder, locate index.css
(or create one if missing) and include the Tailwind directives at top:
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.
Now, you can start using Tailwind classes directly in your React components. For example, modify App.jsx
:
Challenge:
w-1/2
: Sets the width of the <main>
element to half of its parent container.
mx-auto
: Centers the element horizontally by setting automatic margins on the left and right.
f
lex
, flex-col
, items-center
, justify-center
: 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.
App.jsxAppGPT-4o⇧⏎ New Line⏎ Send
To see your Tailwind-styled app in action, run the Vite development server:
This will start your application at http://localhost:5173/
(or another available port).
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.
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.
Install Bootstrap using npm:
ref site :