Skip to content

Inconsistent use of CSS variables for form-check-input:checked state #41652

@true-perfect-code

Description

@true-perfect-code

Describe the issue

The :checked state for .form-check-input checkboxes cannot be styled dynamically using CSS variables.

The issue is that while the unchecked state uses a CSS variable (--bs-form-check-bg), the :checked state sets background-color and border-color directly with SASS variables ($form-check-input-checked-bg-color), which are compiled into a static color. This prevents runtime customization.

Problematic code in Bootstrap's SCSS (https://github.com/twbs/bootstrap/blob/main/scss/forms/_form-check.scss) :

&:checked {
  background-color: $form-check-input-checked-bg-color;
  border-color: $form-check-input-checked-border-color;
}

Proposed solution:
Replace the direct SASS variable assignment with a CSS variable. This would allow for dynamic theme adjustments at runtime.

Example of a potential fix:

SCSS

&:checked {
  --#{$prefix}form-check-bg-checked: #{$form-check-input-checked-bg-color};
  background-color: var(--#{$prefix}form-check-bg-checked);
}

pc

Reduced test cases

  1. Go to https://getbootstrap.com/docs/5.3/forms/checks-radios/#without-labels
  2. Check in inspection window unchecked checkbox:
.form-check-input
Specificity: (0,1,0)
 {
    --bs-form-check-bg: var(--bs-body-bg);
    background-color: var(--bs-form-check-bg);
    background-image: var(--bs-form-check-bg-image);
  1. Check in inspection window now checked checkbox:
.form-check-input:checked {
    background-color: #0d6efd;
    border-color: #0d6efd;
}
  1. If you set --bs-form-check-bg the unchecked checkbox will be changed (background) but checked checkbox not!

What operating system(s) are you seeing the problem on?

Windows

What browser(s) are you seeing the problem on?

Chrome

What version of Bootstrap are you using?

v5.3.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions