How To Customize Bootstrap With Sass?
Last Updated :
24 Sep, 2024
Bootstrap is one of the most popular CSS frameworks used to develop responsive websites quickly. However, using the default Bootstrap design can result in many websites looking the same. This is where Sass (Syntactically Awesome Style Sheets) comes in handy. By customizing Bootstrap with Sass, you can create a unique design while still using Bootstrap’s powerful grid system, components, and utilities.
Why Customize Bootstrap with Sass?
Customizing Bootstrap with Sass allows you to:
- Modify variables: Change Bootstrap’s default variables like colors, fonts, and sizes to match your brand.
- Use only what you need: Remove unused components to reduce the size of your final CSS file.
- Easily create themes: Sass makes it easy to switch themes or create multiple themes for different sections of your site.
Prerequisites
Below are the approaches to customize Bootstrap using Sass:
Steps to Set Up Bootstrap and Sass
Step 1: Install Node.js and npm
Ensure you have Node.js and npm installed on your system. You can download them from the official Node.js website.
Step 2: Create a New Project Folder
Create a new folder for your project:
mkdir bootstrap-sass-customization
cd bootstrap-sass-customization
Step 3: Initialize npm
Inside the folder, initialize a new npm project:
npm init -y
Step 4: Install Bootstrap and Sass
Now install Bootstrap and Sass via npm:
npm install bootstrap@5 sass
Step 5: Update your package.json with scripts to compile Sass:
{
"scripts": {
"build-css": "sass scss/custom.scss dist/custom.css --watch"
}
}
Folder Structure
Folder StructureUpdated Dependencies
"dependencies": {
"bootstrap": "^5.3.3",
"sass": "^1.79.3"
}
Run the command to watch for Sass changes:
npm run build-css
1. Changing Bootstrap Variables
You can change predefined variables like colors, fonts, spacing, etc., in Bootstrap by overriding its default variables in your Sass file. One of the simplest ways to customize Bootstrap with Sass is to change its default variables.
Example: The SCSS code customizes the Bootstrap primary button's background and border colors, with a hover effect, and applies these styles to a button in the HTML file.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/custom.css">
</head>
<body>
<button class="btn btn-primary m-5">Primary Button</button>
</body>
</html>
CSS
// scss/custom.scss
@import "../node_modules/bootstrap/scss/bootstrap";
// Correct way
.btn-primary {
& {
background-color: #e74c3c;
border-color: #e74c3c;
}
&:hover {
background-color: #c0392b;
}
}
Output
How to Customize Bootstrap with Sass2. Overriding Bootstrap Components
Sometimes, you may want to customize specific components like buttons or cards. You can override Bootstrap styles by targeting these components directly in your custom Sass file. You can apply specific changes to Bootstrap components by targeting their classes directly and adding your custom styles.
Example: The SCSS overrides Bootstrap's primary button styles, changing the background, border, and hover colors with Sass-compliant syntax, and applies these styles to a button in the HTML file.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/custom.css">
</head>
<body>
<button class="btn btn-primary">Primary Button</button>
</body>
</html>
CSS
// scss/custom.scss
@import "../node_modules/bootstrap/scss/bootstrap";
// Override button styles with Sass compliance
.btn-primary {
// Wrapping declarations in & {} block to avoid deprecation warning
& {
background-color: #e74c3c;
border-color: #3c7be7;
}
&:hover {
background-color: #35c02b;
}
}
Output
3. Adding Custom Styles
In addition to modifying existing Bootstrap components, you can add entirely new styles to your project using Sass. You can write custom styles using Sass and include them alongside Bootstrap's existing styles for added functionality.
Example: The SCSS defines custom styles for a header with a green background, white text, and hover effect, and applies these styles to a header in the HTML file.
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/custom.css">
</head>
<body>
<header class="custom-header">
Welcome to My Custom Bootstrap Page
</header>
</body>
</html>
CSS
// scss/custom.scss
@import "../node_modules/bootstrap/scss/bootstrap";
// Custom styles for a header
.custom-header {
& {
background-color: #2ecc71;
// Wrapping in & {}
color: white;
padding: 20px;
text-align: center;
font-size: 24px;
}
&:hover {
background-color: #27ae60;
// Darker green on hover
}
}
Output
How To Customize Bootstrap With Sass?
Similar Reads
How to Customize Bootstrap 5 Radios ?
Bootstrap 5 radios provide a flexible and stylish way to create radio buttons in your forms. Customize their appearance and behavior easily with built-in classes. Enhance usability with various layout options, including inline and stacked radios, ensuring a responsive and accessible user experience.
3 min read
How to Customize Bootstrap 5?
To customize Bootstrap 5, adjust its appearance by using custom CSS to modify default styles or by utilizing Sass variables to customize design elements like colors, typography, and spacing. These methods enable you to control your project's visual presentation. Table of Content Custom CSSModifying
2 min read
How to Customize Button Styles in Bootstrap ?
Customizing button styles in Bootstrap involves modifying predefined classes or creating custom CSS rules. This allows alignment with brand identity, and differentiation, and ensures design consistency across projects. We can customize button styles in Bootstrap using different approaches as listed
3 min read
How to Customize Bootstrap jQuery Plugins ?
Bootstrap is a popular HTML, CSS, and JavaScript framework for building responsive, mobile-first web applications. It includes a library of jQuery plugins that provide various UI components and behaviors, such as modals, tabs, and carousels. While these plugins are useful out of the box, you may nee
4 min read
How to Customized Bootstrap 5 Checkbox?
Bootstrap, a widely used customizing front-end framework, streamlines web development with its pre-built CSS and JavaScript components. Bootstrap 5 Checkbox enhances interactive forms and interfaces, offering customization options for styles, sizes, and validation, facilitating user selections.There
3 min read
How to Use Bootstrap with React?
Bootstrap is one of the most popular front-end frameworks, widely used to create visually appealing, responsive, and mobile-first websites quickly. It provides pre-designed UI components, grid systems, and various CSS classes that help you build mobile-first, responsive web applications quickly and
9 min read
How to generate thumbnails and customize using bootstrap ?
Bootstrap helps web developers to create thumbnails that are used to show linked images in grids with the pre-defined classes which help to reduce codes length. Thumbnails are created to provide a quick preview of images with small images.Thumbnail Image: A thumbnail is a small image that represents
4 min read
BootStrap 5 Typography Customizing Headings
Bootstrap 5 Typography Customizing headings is used to customize the heading without using HTML tags and CSS classes. Use the .text-muted class with <small> tag. Typography Customizing headings used tag: small: It is used to create secondary subheadings. Typography Customizing headings used cl
2 min read
How to Create a Four-Column Layout with Bootstrap ?
In Bootstrap, creating a column layout is important for organizing content into a structured grid system. A four-column layout allows for efficient arrangement of content, such as displaying courses, products, or services, with flexibility in responsiveness for various screen sizes. Below are the ap
2 min read
Bootstrap 5 Customizing the Grid
Bootstrap Grid system is one of the most versatile layouts which we can use and it has different responsive variants which can be used to add custom responsiveness. Bootstrap Grid system provides a responsive layout with columns and gutters. Customizing the grid is essentially changing the number of
6 min read