How to Use Vite with Bootstrap?
Last Updated :
03 Sep, 2024
This article provides a thorough how-to for combining Vite, an innovative frontend build tool known for its quick development environment and effective build process, with Bootstrap, a popular CSS framework. To accomplish this integration, we'll look into two methods: installing Bootstrap using npm for greater control, or using the Bootstrap CDN for faster setups. Developers of all experience levels will find it simple to follow along and select the approach that best suits their needs, as both approaches will be thoroughly explained.
These are the following approaches:
Steps to Set up Vite Application
Step 1: Install Vite and set up your project
npm create vite@latest my-vite-project
Step 2: Select these options
√ Select a framework: » React
√ Select a variant: » JavaScript + SWC
Step 3: Navigate to the project
cd my-vite-project
Step 4: Install all the dependencies
npm install
Project structure:
Project structureUpdated dependencies:
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Using Bootstrap CDN
Using the Bootstrap CDN is the quickest way to integrate Bootstrap with Vite. Smaller projects or rapid prototyping are best served by this approach since it eliminates the need for extensive Bootstrap style customization. Without installing extra dependencies, you can quickly get started with Vite and Bootstrap by including it via a Content Delivery Network (CDN).
Example: This example shows the use of bootstrap in Vite by using Bootstrap CDN link.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Vite + Bootstrap</title>
<!-- Bootstrap CSS via CDN -->
<link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity=
"sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>
<div id="root"></div>
<!-- Bootstrap JavaScript Bundle with Popper -->
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
JavaScript
// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
JavaScript
// src/App.jsx
import React from 'react';
function App() {
return (
<div className="container mt-5">
<h1 className="text-primary">Hello, Bootstrap with Vite!</h1>
<button className="btn btn-success">Click Me</button>
</div>
);
}
export default App;
Run the Development Server:
npm run dev
Output: Visit the local development URL provided (e.g., http://localhost:5173). You should see a basic Bootstrap-styled page with a button.
OutputInstalling Bootstrap via npm
It is recommended to install Bootstrap via npm for larger applications or when you require more control over Bootstrap's styles and dependencies. By using features like tree-shaking to remove unnecessary CSS, you can better manage dependencies and customize Bootstrap.
Install Bootstrap and Its Dependencies
Navigate to your Vite project directory and install Bootstrap via npm:
npm install bootstrap
Updated dependencies:
"dependencies": {
"bootstrap": "^5.3.3",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Example: This example shows the using bootstrap with Vite by installing it through npm.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width,
initial-scale=1.0" />
<title>Vite + Bootstrap (npm)</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
JavaScript
// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
// Import Bootstrap styles
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Run the Development Server:
npm run dev
Output: Visit the local development URL provided (e.g., http://localhost:5173). The output should be a Bootstrap-styled page, similar to the CDN approach but with more flexibility.
Output
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read