Lesson 20: Customizing Bootstrap Variables with SCSS

📌 Objective: Learn how to customize Bootstrap styles using SCSS variables and build a custom Bootstrap theme.


🔹 1. Introduction to Bootstrap SCSS Customization

Bootstrap is built using SCSS (Sassy CSS), which allows customization of colors, typography, spacing, and other styles.

💡 Why Use SCSS for Bootstrap Customization?
✔️ Modify Bootstrap colors, fonts, and layout without overriding CSS.
✔️ Improve performance by compiling only the necessary Bootstrap components.
✔️ Make your theme unique while using Bootstrap’s framework.


🔹 2. Setting Up Bootstrap SCSS

To customize Bootstrap, follow these steps:

1️⃣ Install Bootstrap with npm

Run the following command:

sh
npm install bootstrap

🔹 What happens?

  • This installs Bootstrap and its SCSS files in node_modules/bootstrap/.

2️⃣ Create a SCSS File for Customization

Inside your project, create a custom SCSS file (e.g., custom.scss).

Example: Import Bootstrap in SCSS

scss
// Import Bootstrap functions and variables
@import "node_modules/bootstrap/scss/functions";
@import "node_modules/bootstrap/scss/variables";
// Override Bootstrap variables
$primary: #ff6600; // Custom primary color
$secondary: #333333; // Custom secondary color
$body-font-family: ‘Poppins’, sans-serif; // Custom font

// Import Bootstrap SCSS (after overriding variables)
@import “node_modules/bootstrap/scss/bootstrap”;

🔹 What happens?

  • Bootstrap variables are modified before importing Bootstrap styles.

🔹 3. Customizing Bootstrap Colors

Bootstrap uses SCSS variables to define colors.

Example: Changing Bootstrap Colors

scss
$primary: #ff6600;
$success: #28a745;
$danger: #dc3545;
$warning: #ffc107;
$info: #17a2b8;
// Import Bootstrap with new colors
@import “node_modules/bootstrap/scss/bootstrap”;

🔹 What happens?

  • The primary color changes from blue to orange (#ff6600).

🔹 4. Customizing Bootstrap Typography

Modify Bootstrap’s default font family, sizes, and weights.

Example: Custom Font & Typography

scss
$font-family-sans-serif: 'Poppins', sans-serif;
$font-size-base: 1rem; // Default font size
$h1-font-size: 2.5rem; // Custom H1 size
// Import Bootstrap with updated typography
@import “node_modules/bootstrap/scss/bootstrap”;

🔹 What happens?

  • The default font changes to Poppins.
  • H1 headers become larger.

🔹 5. Customizing Bootstrap Buttons

Bootstrap buttons are styled using SCSS variables.

Example: Custom Button Styles

scss
$btn-padding-y: 0.75rem;
$btn-padding-x: 1.5rem;
$btn-border-radius: 10px;
// Import Bootstrap with new button styles
@import “node_modules/bootstrap/scss/bootstrap”;

🔹 What happens?

  • Buttons become larger and rounded.

🔹 6. Creating a Custom Theme

You can define a completely custom theme using SCSS.

Example: Custom Theme

scss
$primary: #6f42c1; // Purple theme
$secondary: #f8f9fa;
$body-bg: #f4f4f4;
$body-color: #333;
// Custom button styles
$btn-border-radius: 30px;
$btn-font-weight: bold;

// Import Bootstrap with custom theme
@import “node_modules/bootstrap/scss/bootstrap”;

🔹 What happens?

  • The entire site follows a custom theme with purple primary color.

🔹 7. Compiling SCSS to CSS

To apply your SCSS changes, compile SCSS into CSS.

Example: Compile SCSS to CSS

sh
sass custom.scss custom.css

🔹 What happens?

  • The SCSS file compiles into a CSS file (custom.css).

🔹 8. Using Compiled CSS in HTML

Once compiled, link the custom CSS in your HTML file.

Example: Using Custom Bootstrap Styles

html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="custom.css">
</head>
<body>
<button class="btn btn-primary">Custom Button</button>
</body>
</html>

🔹 What happens?

  • Bootstrap now uses your customized styles.

📝 Lesson Recap Quiz

🎯 Test your knowledge of Bootstrap SCSS Customization.

📌 Multiple-Choice Questions (MCQs)

1️⃣ What command installs Bootstrap with SCSS?
a) npm install bootstrap
b) bootstrap install
c) sass install bootstrap
d) bootstrap-cli install

Correct Answer: a) npm install bootstrap


2️⃣ Where should you override Bootstrap variables?
a) After importing Bootstrap
b) Before importing Bootstrap
c) Inside HTML
d) In JavaScript

Correct Answer: b) Before importing Bootstrap


📌 True or False

3️⃣ Bootstrap’s SCSS variables can be customized without JavaScript.
Answer: True

4️⃣ You must recompile SCSS whenever you change Bootstrap variables.
Answer: True


🎯 Practical Challenge

✅ Modify Bootstrap’s primary color to #ff5722.
✅ Customize Bootstrap buttons to have rounded corners and a larger size.
✅ Set Bootstrap’s default font to “Roboto”.
✅ Post your solution in the discussion forum for feedback!


🎓 Lesson Summary

Bootstrap SCSS customization allows deep style modifications.
Variables ($primary, $font-family-sans-serif) control Bootstrap styles.
SCSS must be compiled into CSS using sass or a build tool.
Customizing Bootstrap SCSS makes your theme unique.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *