# Customize Bootstrap with CSS Variables

In this chapter, we will explore how to customize Bootstrap using CSS variables. We'll learn how to create a custom theme by adding variables to the :root selector, enabling us to easily apply our styles across our CSS files. We will also modify some of Bootstrap's global settings, incorporate Bootstrap components styled with our custom theme, and even build a custom component from scratch.

# Prerequisites

Before starting, ensure you have the following:

  1. Clone the start project: Obtain the project files from the GitHub repository (opens new window).
  2. Install live-server: Use npm to install the live-server globally.
    npm install -g live-server
    
    1
  3. Run live-server: Start the development server with the command below.
    npm run watch
    
    1

# File Structure

The project is organized as follows:

πŸ“‚css_bootstrap_demo_project/
β”œβ”€β”€ πŸ“‚src/
β”‚   β”œβ”€β”€ πŸ“‚css/
β”‚   β”‚   β”œβ”€β”€ πŸ“‚bootstrap/
β”‚   β”‚   β”‚   └── πŸ“„bootstrap5.3.3.min.css
β”‚   β”‚   └── πŸ“‚components/
β”‚   β”‚       β”œβ”€β”€ πŸ“„alert.css
β”‚   β”‚       β”œβ”€β”€ πŸ“„button.css
β”‚   β”‚       β”œβ”€β”€ πŸ“„callout.css
β”‚   β”‚   β”œβ”€β”€ πŸ“„content.css
β”‚   β”‚   β”œβ”€β”€ πŸ“„main.css
β”‚   β”‚   └── πŸ“„root.css
β”‚   β”œβ”€β”€ πŸ“‚js/
β”‚   β”‚   └── πŸ“„bootstrap5.3.3.min.js
β”‚   β”œβ”€β”€ πŸ“„index.html
└── πŸ“„package.json
└── πŸ“„README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

All the necessary files for this demonstration are located within the src directory. The basic project setup is already completed, and our focus will be on customizing the Bootstrap framework.

# Create a Custom Theme

Let's begin by establishing a custom theme for our project. We will utilize the following color palette for this example:

tm-primary
#f04b20
tm-secondary
#009cab
tm-tertiary
#f0cf1f
tm-slate
#546363

NAMING CONVENTION

  • We will follow the naming convention tm-<color-name> for our custom theme colors. You are free to use any naming scheme, provided it does not conflict with existing Bootstrap class names.
  • Using suffixes like primary, secondary, and tertiary facilitates easy switching between different themes in the future.
  • The suffix text will be used for text and background colors within our theme.

# Add Theme Colors to the :root Selector

Now, let's create CSS variables for our chosen colors and add them to the :root selector within the root.css file. Because root.css is imported after Bootstrap's CSS, our custom theme colors will override Bootstrap's defaults. This allows us to introduce custom properties that we can then utilize throughout our CSS.

TIP

  • We have created a simple web tool to help you generate various shades of a color.
  • Visit https://css-shades.netlify.app/ (opens new window):
  • Click on the color picker to select your desired color. (Use the double arrow in the bottom right to switch between RGB, HSL, and HEX formats).
  • Modify the color name as needed.
  • Copy the generated CSS custom properties and paste them into the :root selector in your root.css file.
  • Shade 400 typically represents the base color.
  • Utilize the web tool to generate custom properties for our color palette:
  • Base color: #f04b20, Color name: tm-primary
  • Base color: #009cab, Color name: tm-secondary
  • Base color: #f0cf1f, Color name: tm-tertiary
  • Base color: #546363, Color name: tm-slate
  • Open the file src/css/root.css and insert the generated custom properties within a :root selector.

# Global Settings

# Modify the :root Selector

  • To inspect the contents of Bootstrap's :root selector, you can open the bootstrap5.3.3.min.css file and search for :root.
  • Since this file is minified, it is not easily readable. Alternatively, you can view the original bootstrap5.3.3.css file in your browser by navigating to: https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.css (opens new window). Bootstrap root variables
  • The :root selector in Bootstrap contains global settings such as:
    • --bs-font-sans-serif: Defines the default sans-serif font family.
    • --bs-body-color: Sets the default text color for the <body>.
    • --bs-body-bg: Sets the default background color for the <body>.
    • --bs-border-radius: Defines the standard border-radius applied to various elements.
    • ...
  • Let's override these four settings in our root.css file.

# Customize Other Elements

We can further customize the styling by modifying properties of standard HTML tags within our project. This can be accomplished in the content.css file.

  • Open the file src/css/content.css and apply the following changes to heading tags:
    • Set the color of all heading tags (h1 to h6) to #007d89 and the font-size to 2rem.
    • For the h2 tag, add a top and bottom padding of 1rem and set the font-size to 1.5rem.
    • Add a ::before pseudo-element to the h2 tag with the content "β˜… ".

# Components

To identify the CSS variables used within Bootstrap components, the most effective method is to inspect the source code using your browser's developer tools. We'll start by examining a default Bootstrap component to understand which CSS variables are in use, which will guide our customization efforts.

# Alert Components

  • Inspect the primary alert component using your browser's developer tools to see the associated CSS variables. Alert CSS Variables
  • Open the file src/css/components/alert.css.
  • Copy the entire .alert-primary class definition from the browser's developer tools and paste it into alert.css.
  • Rename the class to .alert-tm-primary and update the values of the --bs-alert-color, --bs-alert-bg, --bs-alert-border-color, and --bs-alert-link-color properties using shades from our tm-primary color palette.
  • Repeat this process for the .alert-tm-secondary and .alert-tm-tertiary classes.

# Button Components

  • Inspect the primary button component using your browser's developer tools to identify the relevant CSS variables. Button CSS Variables
  • Open the file src/css/components/button.css.
  • Copy the complete .btn-success class definition from the browser's developer tools and paste it into button.css.
  • Rename the class to .btn-tm-primary and update all the CSS variable values using shades from the tm-primary color palette.
  • Repeat this for the .btn-tm-secondary and .btn-tm-tertiary classes.

The base .btn class can be further enhanced by adding custom properties. Let's introduce an animation and a box-shadow to the button's :hover state.

# Create a Custom callout Component

Now, let's create a custom component, the callout, which is not included in the Bootstrap framework but will be styled with our custom theme. We'll follow a similar structure to Bootstrap's components:

  • A base class .callout to define general properties.
  • Modifier classes .callout-tm-primary, .callout-tm-secondary, and .callout-tm-tertiary for applying different theme colors.

For this custom component, we'll use a slightly different approach:

  • The base class .callout will have custom properties with default fallback values.
  • The modifier classes will override these default values to apply our custom theme colors.

# Create the Base Class .callout

  • Open the file src/css/components/callout.css.

# Create the Modifier Classes

The next step is to add the custom properties to the modifier classes: .callout-tm-primary, .callout-tm-secondary, and .callout-tm-tertiary.

# Conclusion

By leveraging CSS variables, we've successfully customized Bootstrap to match our desired theme. This approach allows for a more maintainable and scalable styling system. We've seen how to override Bootstrap's default variables, style existing components, and even create new components that seamlessly integrate with our custom theme. This methodology is crucial for building consistent and branded web applications efficiently.

Last Updated: 3/4/2025, 9:36:21 PM