How can I customize the color of the check mark symbol in a bootstrap checkbox?

Here is my customized HTML code using Bootstrap:

<div class="col-lg-4 col-12 text-center">
  <div style="width: 80%; margin: auto">
    <ul class="list-group">
      {% for sl in my_list %}
      <li
        class="list-group-item my-lg-2 my-1 list-group-item-secondary bg-dark text-light"
      >
        <input
          class="form-check-input me-1"
          type="checkbox"
          value=""
          id="checkbox_{{ loop.index0 }}"
          style="
            border-top-left-radius: inherit;
            border-top-right-radius: inherit;
          "
        />
        <label
          class="form-check-label"
          for="checkbox_{{ loop.index0 }}"
          >{{ sl.description }}</label
        >
      </li>

      {% endfor %}
    </ul>
  </div>
</div>

I have also added some custom styles for my checkboxes:

@keyframes shadowAnimation {
  0% {
    box-shadow: 0 0 0 0.25rem rgba(255, 152, 0, 0.9);
  }
  100% {
    box-shadow: 0 0 0 0.25rem rgba(255, 152, 0, 0.1);
  }
}

.animate-checkbox {
  animation: shadowAnimation 1s ease-in-out;
}

input[type="checkbox"].form-check-input:focus-visible,
input[type="checkbox"].form-check-input:focus {
  border-color: #212529;
  border: var(--bs-border-width) solid #ff9800 !important;
}

The animation is controlled by JavaScript. I want to change the color of the checkbox tick without overwriting the existing options. Any suggestions on how to achieve this? Thank you in advance.

UPDATE: I have successfully implemented this in a project without using Bootstrap:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="col-lg-4 col-12 text-center">
      <div style="width: 80%; margin: auto">
        <ul class="list-group">
          <li
            class="list-group-item my-lg-2 my-1 list-group-item-secondary bg-dark text-light"
          >
            <input
              class="form-check-input me-1 animate-checkbox"
              type="checkbox"
              value=""
              id="checkbox_{{ loop.index0 }}"
            />
            <label class="form-check-label" for="checkbox_{{ loop.index0 }}"
              >test</label
            >
          </li>
        </ul>
      </div>
    </div>
  </body>
</html>
@keyframes shadowAnimation {
  0% {
    box-shadow: 0 0 0 0.25rem rgba(255, 152, 0, 0.9);
  }
  100% {
    box-shadow: 0 0 0 0.25rem rgba(255, 152, 0, 0.1);
  }
}

/* Apply your animation to the checkbox */
input[type="checkbox"].form-check-input.animate-checkbox {
  /* Add your animation styles here */
  animation: shadowAnimation 1s ease-in-out;
}

input[type="checkbox"]:checked {
  background-color: #ff9800;
}
input[type="checkbox"]:checked:after {
  content: "\2713";
  color: #212529;
}
input[type="checkbox"] {
  text-align: center;

  vertical-align: middle;
  width: 20px !important;
  height: 20px !important;
  appearance: none;
  border-radius: 25%;

  border: 1px solid #212529;
  box-shadow: none;
  font-size: 1em;
}

When using Bootstrap, a white "✓" is visible behind the black one, which is centered within the square. How can I resolve this issue?

Answer №1

I found a simple solution without adding multiple CSS rules. Upon inspecting the code, I discovered that bootstrap was utilizing the following rule:

.form-check-input:checked[type="checkbox"] {
  --bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
}

By disabling this option in the browser console, the tick disappeared. Further research revealed that the tick was customizable. To change the color, I only needed to adjust the stroke property. For instance, to set the color to #212529, I used stroke='%23212529'.

Note that the # symbol is encoded as %23 in the unicode browser format.

Ultimately, I added the following CSS rule to my stylesheet:

input:checked[type="checkbox"] {
  --bs-form-check-bg-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23212529' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e") !important;
}

I also managed to customize the tick using text:

--bs-form-check-bg-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'><text x='5' y='15' font-size='16' font-family='Arial' fill='%23212529' text-anchor='middle'>%E2%9C%93</text></svg>") !important;

This will display the "✓" tick as a marker. Please note that the position of the text may need adjustment to center it in the square by manipulating the x and y coordinates.

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

What is the best way to make the text align with the top of its designated space?

I've been experimenting with different CSS properties like vertical-align, line-height, and more. This example should give you an idea of my goal. I added a small red arrow to indicate where I intend for the text to be shifted upwards. ...

unable to execute PHP code

I am attempting to execute a PHP file that will update a database. On Chrome, I am encountering this error: https://i.sstatic.net/3ruNL.png This is the code I have in my index.html file: <!DOCTYPE html> <html> <body> <input type ...

Vertical scroll through a list of columns with a stationary header, while allowing both the list and the header to

Check out this JSFiddle example: https://jsfiddle.net/jefftg/1chmyppm/ The layout currently has orange header columns and white list rows that scroll together horizontally. However, the goal is to have the white list rows scroll vertically while keeping t ...

Tips for avoiding table column overlap during window resizing situations

My current issue involves using a table to present information. While resizing the window, I noticed that the columns in the table were overlapping. Below is the code snippet: <table style="width:100%;table-layout:fixed"> <tr style="border-bo ...

Guide to applying conditional styling to Material-UI TextField

After creating a custom MUI TextField, I implemented the following styling: const CustomDisableInput = styled(TextField)(() => ({ ".MuiInputBase-input.Mui-disabled": { WebkitTextFillColor: "#000", color: "#00 ...

What is the best way for Selenium in C# to identify and locate a specific span element by its value

I've been experimenting with different locators, but I'm struggling to find a straightforward solution for identifying the dynamic number within this span using C# Selenium. Initially, my focus is just on locating the span itself. <span inven ...

Pictures without a set width

Hey there! I'm working on a responsive website and facing an issue with image resizing. When I shrink the screen size, the images also shrink along with it. How can I set a fixed width for the images? I tried switching from rem to px but that didn&apo ...

CSS exclusive - achieving a horizontal scrolling list with all list items in a single row

I am working with a div that has a fixed width. Inside this div, there is an unordered list (ul) containing a random number of list items (li) with varying widths. My project utilizes the Bootstrap framework. Below is an example of the code structure: & ...

Position the image slider uniformly at the bottom using CSS (utilizing the Slick Carousel library)

I am utilizing the slick carousel library to create a unique and elegant slider. However, when using images with varying heights, I am encountering issues in aligning the images at the same bottom: https://i.stack.imgur.com/ab5s3.png Here is my code snip ...

The animation feature on the slideshow is dysfunctional

For this Vue component, I attempted to create a slideshow. The process is as follows: 1) Creating an array of all image sources to be included (array: pictures) 2) Initializing a variable(Count) to 0, starting from the beginning. 3) Adding v-bind:src=" ...

What is the method for encoding and decoding a query string within an HTML object tag?

Within my code, I am utilizing an object tag that utilizes a data attribute to call a servlet. When passing a parameter to the URL, if the value is not in English but instead Arabic, for example, the value appears distorted when retrieved in the specified ...

Using @media queries for mobile and desktop: preventing desktop from displaying mobile styles

I have been working on redesigning a website for mobile using only CSS. I set up media queries to deliver the appropriate CSS for desktop and mobile devices. However, I noticed that when resizing the browser to under 855 pixels, some of the mobile CSS is ...

The Body and HTML elements compress to sizes significantly smaller than the viewport

I am currently working on making my WordPress website responsive. On one of the pages, I have two images that I want to set a max-width of 100% to ensure they are responsive. However, when I shrink the page in Chrome dev tools, I noticed that the <html& ...

apply a visible border to the item that is clicked or selected by utilizing css and vue

I have a list of items that I want to display as image cards with an active blue border when clicked. Only one item can be selected at a time. Below is the code snippet: Template Code <div class="container"> <div v-for="(obj ...

The _rfs.scss width rule in Angular-Bootstrap-NgBootstrap is overriding the column width of the col-rules

The Dilemma I am currently working on an Angular project that incorporates Bootstrap and ng-bootstrap. My intention is to utilize the grid system within this project, but I encountered some bugs along the way. When trying to render the following HTML/TS ...

Is there a way for me to insert a new column into a table using jinja?

As a beginner in the final project of CS50x, I am working on creating a webpage that allows users to input weights into a database table and then view those weights along with weight gain/loss information. I am using jinja and python to render a query resu ...

style the table cells based on results of winner values retrieved from JSON

After fetching JSON data, I am utilizing angular JS to present it in a table. The table consists of multiple rows that are populated from the JSON file using ng-repeat. The value for Status.winner can either be 0 or 1. If the winner's value for a p ...

How to create a see-through background using three.js

I am new to working with three.js and recently came across a codepen that caught my attention. However, I am facing difficulties while trying to change the background color. After exploring various questions related to this issue, I attempted to add { alp ...

Style the div element with CSS

Is there a way to style a div element within an HTML document using Sencha framework? I have specific CSS properties that I would like to apply to my div. #logo{ position:absolute; top:20%; left:0%; } Below is a snippet of my Sencha code: Ex ...

Show the text area content in an alert when using Code Mirror

When I try to alert the content of a textarea that is being used as a Code Mirror on a button click, it appears blank. However, when I remove the script for Code Mirror, the content displays properly. I am puzzled about what could be causing this issue wi ...