loading...
Cover image for Add Colored Text and Background Classes in Bootstrap

Add Colored Text and Background Classes in Bootstrap

shhdharmen profile image Dharmen Shah ・2 min read

Customizing Bootstrap (3 Part Series)

1) Create custom checkboxes and radios in Bootstrap 2) Responsive Size Classes in Bootstrap 3) Add Colored Text and Background Classes in Bootstrap

Bootstrap provides contextual classes, let's see how we can add more classes in the same manner.

This is the series about customizing Bootstrap with it's scss variables, mixins and functions. In previous article, we learned how to create responsive size classes.


Background

Bootstrap already provides contextual color classes, like bg-primary, text-danger, etc.

Let's see how we can add classes directly related with color name, like bg-blue, text-yellow, etc.

Let's get started

This is going to be very simple in short.

Start quickly with my github repo: bootstrap-theme-kit. Just follow the initial guide and continue below.

GitHub logo shhdharmen / bootstrap-theme-kit

Quickly generate and showcase your bootstrap theme.

Bootstrap Theme Kit

bootstrap-theme-kit

Quickly ⚑ Generate and Showcase 🎯 your bootstrap theme 🎨. Get Started or See sample theme.

GitHub license Commitizen friendly

πŸš€ Getting Started

β˜‘οΈ Minimum Requirements

node -v
// v10.17.0
git --version
// git version 2.x

⬇️ Steps to Follow

  1. First, fork this repo.
  2. Open terminal and:
git clone <forked-repo-url>
cd bootstrap-theme-kit
npm i
npm run init
npm start
  1. Browser will open at 3000 port.
  2. Start editing your scss/html files and browser will reload.

πŸ† Features

We already have file called _colors.scss in utilities folder. Let's use that.

Add below code in it:

// src\styles\utilities\_colors.scss

$colors: () !default;
@each $color in map-keys($colors) {
  .text-#{$color} {
    color: $color !important;
  }
  .bg-#{$color} {
    background-color: $color !important;
  }
}

Now, we need to make some change in src\styles\main.scss, so that we can use Bootstrap's $colors list in utilities_colors.scss.

As we are using @use rule, it's easier to achieve such thing. Let's make some changes in src\styles\main.scss:

// src\styles\main.scss

...
// 5. Utilities
@use "utilities" with (
    $colors: vendors.$colors
);
...

A quick description of what's happening over here:

  1. We created _colors.scss a configurable module
  2. Then, we loaded member, in this case $colors, of Bootstrap using vendors.$colors
  3. And we configured _colors.scss using with ( <variable>: <value>, <variable>: <value> ) pattern

That's it!!! Now you can use all the color classes for items defined in $colors list. Let me know your thoughts and feedback in the comment section.

Well done

Credits

Let's get started image: Photo by David Iskander on Unsplash
Man sitting on mountain: Photo by Ian Stauffer on Unsplash
Cover image: Photo by Andrej LiΕ‘akov on Unsplash

Customizing Bootstrap (3 Part Series)

1) Create custom checkboxes and radios in Bootstrap 2) Responsive Size Classes in Bootstrap 3) Add Colored Text and Background Classes in Bootstrap

Posted on by:

shhdharmen profile

Dharmen Shah

@shhdharmen

I work on Angular, React, Electron, JavaScript, CSS, SCSS, Bootstrap, NodeJS. Love to contribute to Open-Source Projects.

Discussion

markdown guide