Tips for creating classes with SASS?

In my application, I am utilizing Bootstrap 4 with sass. While Bootstrap offers a custom input checkbox for the primary theme color, it does not provide one for other theme colors.

I have come across methods to generate classes using sass like @each and @mixin, but I found them to be unclear. Therefore, as shown in the code below, I manually created each class and color.

.custom-checkbox-primary{
  .custom-control-input:checked ~ .custom-control-label::before {
    color: $primary;
    border-color: $primary;
    background-color: $primary;
  }

}
.custom-checkbox-secondary{
  .custom-control-input:checked ~ .custom-control-label::before {
    color: $secondary;
    border-color: $secondary;
    background-color: $secondary;
  }
}

.custom-checkbox-danger{
  .custom-control-input:checked ~ .custom-control-label::before {
    color: $danger;
    border-color: $danger;
    background-color: $danger;
  }
}

My goal is to have sass dynamically generate these 'custom-checkbox-{{theme-color}}' classes for me.

Answer №1

If you want to loop through the theme-colors using @each, here's a simple example:

@each $color, $value in $theme-colors {
  .custom-checkbox-#{$color}{
      .custom-control-input:checked ~ .custom-control-label::before {
        color: $value;
        border-color: $value;
        background-color: $value;
      }
  }
}

Check out the demo here:


For more information on extending Bootstrap 4 and SASS, visit: Extending Bootstrap 4 and SASS

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Container for Magazines using HTML and CSS

Looking to develop a digital magazine application with smooth swipe transitions and fade in effects when swiping left and right. I attempted using jQuery.mobile, but struggled with adapting the background image height. My goal is to create a universal web ...

Align several images vertically inside a container

I'm currently attempting to vertical align/center some images within a div, but it's not coming out perfectly. The images seem to be slightly too low to be considered vertically centered. What could be causing this issue? .Container { wid ...

What is the best way to maintain the layout design when resizing the font?

Here is the HTML and CSS code snippet: .navi ul li { float:left; list-style-type: none; border-right: 1px solid #c0c0c0; margin:0px 0 0 0; } .navi ul li:last-child { border:none; } .navi ul li a { display:block; margin:0 20px; text-dec ...

Bootstrap fixed top navbar with a double color scheme

I would like to replicate the following image: using a navbar-fixed-top with Bootstrap. Currently, I have added the following custom CSS: navbar-fixed-top.css .navbar { background-color: #8CB5A0; background-image: none; } Here is the Bootstrap ...

Tips for inserting database table parameters into CSS style

Is there a way to define CSS style parameters in a database table (MySQL) and then use them to style elements such as a menu? Currently, I am aware that we can use variables within styles like this: <div id="content-inner" style="<?php echo $this-&g ...

Exploring Webpack: Unveiling the Contrasts Between Production and Development CSS Bundles

Can you explain the differences in CSS between production and development when compiling with webpack? I've noticed that the production stylesheet seems to consider the entire website, whereas the development mode appears to only focus on the specifi ...

Tips for designing a pre-selection process

I've been considering the idea of implementing a temporary screen that allows users to choose between different options before proceeding to a specific page. For instance, on the contact page, users would be prompted with a question like "Are you loo ...

"Maximize user experience: Eliminate the "#" in your URL by utilizing anchor tags for one-page navigation in

I've created a website with menu tabs including Home, About, Work, and Contact. To make navigation easier, I've used anchor tags for this one-page layout. However, I'm concerned about the URLs updating when clicking on the tabs. I prefer to ...

A guide to configuring the default date format as ('dd/mm/yyyy') using ng-bootstrap

html <label for="date">{{ "date" | translate }}</label> <input type="date" class="form-control checking-field" id="date"> Is there a way to display the date in the format ('dd/mm/yyyy') in this HTML code? ...

Pause the event listener temporarily and reattach it after specific actions have been completed

I am currently working on a React app that includes a scrollable div. Here is an example: <main id="main" onScroll={this.handleScroll}> My question is, how can I temporarily remove the onScroll event listener from the main element and then reattach ...

Is there a way to display x-overflow on a table instead of the window?

Check out the table at the following link: In my CSS, I set a min-width and overflow-x property for the table. However, on the mobile template, the overflow-x does not work properly. I want the template to display correctly with the header and footer in ...

Ensure the placeholder remains front and center, growing in size as it moves vertically

.inpsearch{ border:2px solid #bbb; border-radius:14px; height:29px; padding:0 9px; } .inpsearch::-webkit-input-placeholder { color: #ccc; font-family:arial; font-size:20px; } <input type='search' class='inpsea ...

Use the nobr HTML tag on Django form fields

I have been customizing Django form fields using this helpful guide. Here is the form structure: class contact(forms.Form): first_name = forms.CharField() middle_name = forms.CharField() last_name = forms.CharField() This is how the template ...

having difficulties in separating the mat-table header

It may seem like a basic question, but I'm having trouble figuring it out. I'm not sure if this requires a CSS change or something else. I'm looking to add a divider between my table header similar to the one highlighted in the screenshot be ...

Tips for creating an editable div that can also incorporate a textarea and the option to upload or delete photos

On my website, users can upload images, names, and descriptions which are saved in a MySQL database. The information is then fetched to display on the website. The code below allows users to enter this information and see it displayed when they click the s ...

Show a collection of items surrounding an image in order to form a circular menu

For my website, I'm looking to implement a circular navbar that will surround the user avatar. Using ul and li elements, I have created various options that are necessary. Is there a way to position all the navItems around the bottom half of the avata ...

Height constraint disregarded by anchor

I am facing an issue with a large textual menu where the active link space is overlapping onto the other menu items. This is causing difficulty in accurately clicking on a link. For reference, you can check out this example: http://jsfiddle.net/dhyz48j3/1 ...

Adjustable Video Size and Parent Container Vertical Space

Here is the code snippet I am working with: .jumbotron { position: relative; z-index: -101; overflow: hidden; height: 500px; background: red; } .jumbotron video { position: absolute; top: 0; left: 0; right: 0; bottom: 0 ...

The height of my row decreases when I implement the z-index for a hover effect

Hey there! I'm currently working on creating a hover effect for my cards using Bootstrap and z-index. However, I've run into an issue where the z-index works fine when I hover over the cards, but the row loses its height. I tried adding a height ...

The styled components can create identical CSS classes with varying conditions

Below are the CSS styles I am currently using: import styled, { css } from "styled-components"; const H4 = css` font-family: "Futura"; font-style: normal; font-weight: 500; font-size: 20px; line-height: 140%; color: #3a786a ...