Why cannot change checkbox color whatever I do?
Checkboxes are not able to be styled. You would need a third party js plugin there are many available.
If you want to do this yourself it basically involves hiding the checkbox creating an element and styling that as you want then binding its click event to two functions one to change its look and another to activate the click event of the checkbox.
The same problem will arise when trying to style that little down arrow on a drop-down select element.
Changing color of the checkbox
I leave you a possible example: How TO - Custom Checkbox
First we hide the browser's default radio button
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
and now we create a custom radio button
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
FULL EXAMPLE
body {
background-color: grey;
}
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
color: white;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input~.checkmark {
background-color: #ccc;
}
/* When the radio button is checked, add a blue background */
.container input:checked~.checkmark {
background-color: yellow;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked~.checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
How to change checkbox background color in simple HTML
You can't style the browser's default checkbox, so we have to hide it and create a custom checkbox like so:
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 20px;
background-color: #fff;
border: 2px black solid;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #002B4E;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 6px;
top: 3px;
width: 5px;
height: 8px;
border: solid white;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(35deg);
-ms-transform: rotate(35deg);
transform: rotate(35deg);
}
<label class="container">
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
Checkbox CSS cannot change background color for the checkbox
You can't modify checkbox color.
Instead, create pseudo elements with background custom background color, like this :
.form-check {
position: relative;
}
input[type=checkbox] {
width: 20px;
height: 20px;
}
input[type=checkbox]:checked+label::before {
content: "";
display: block;
position: absolute;
text-align: center;
height: 20px;
width: 20px;
left: 0;
top: 5px;
background-color: #FA9E57;
font-family: "Montserrat";
border-radius: 2px;
border: 1px solid rgb(150 150 150 / 30%);
}
input[type=checkbox]:checked+label::after {
content: url('data:image/svg+xml; utf8, <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="white" viewBox="0 0 24 24"><path d="M20.285 2l-11.285 11.567-5.286-5.011-3.714 3.716 9 8.728 15-15.285z"/></svg>');
display: block;
position: absolute;
left: 3px;
top: 3px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="register.css">
<div class="form-group my-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="privacy">
<label class="form-check-label ml-3" for="privacy">
Agree privacy
</label>
</div>
</div>
How to style a checkbox using CSS
UPDATE:
The below answer references the state of things before widespread availability of CSS 3. In modern browsers (including Internet Explorer 9 and later) it is more straightforward to create checkbox replacements with your preferred styling, without using JavaScript.
Here are some useful links:
- Creating Custom Form Checkboxes with Just CSS
- Easy CSS Checkbox Generator
- Stuff You Can Do With The Checkbox Hack
- Implementing Custom Checkboxes and Radio Buttons with CSS3
- How to Style a Checkbox With CSS
It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported :checked
selector, you can make your replacement correctly reflect the checked status of the box.
OLDER ANSWER
Here's a useful article about styling checkboxes. Basically, that writer found that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.
It's not hard to imagine a workaround where you would use JavaScript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without JavaScript would see the default checkbox.
Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.
How to change checkbox color in angular
you can try like below.
html
<div class="checkbox">
<input type="checkbox" disabled="disabled" />
<label>Disabled</label>
</div>
<br />
<div class="checkbox">
<input type="checkbox" />
<label>Abled</label>
</div>
css
.checkbox {
position: relative;
}
input[type='checkbox'] {
cursor: pointer;
position: absolute;
top: 0;
left: 0;
opacity: 0 !important;
outline: 0;
z-index: 3;
width: 17px;
height: 17px;
}
input[type='checkbox'] + label {
padding-left: 1.85714em;
}
input[type='checkbox']:disabled + label::before {
background: #7e7eda;
}
input[type='checkbox']:disabled + label:hover::before {
background: gray;
border: 1px solid #d4d4d5;
}
input[type='checkbox'] + label:before {
position: absolute;
top: 0;
left: 0;
width: 17px;
height: 17px;
content: '';
background: #fff;
border-radius: 0.21428571rem;
-webkit-transition: border 0.1s ease, opacity 0.1s ease;
transition: border 0.1s ease, opacity 0.1s ease;
border: 1px solid #d4d4d5;
}
input[type='checkbox']:checked ~ label:before {
background: #fff;
border-color: rgba(34, 36, 38, 0.35);
}
input[type='checkbox']:checked ~ label:after {
content: '✔';
position: absolute;
font-size: 14px;
top: 0;
left: 0;
width: 17px;
height: 17px;
text-align: center;
opacity: 1;
color: rgba(0, 0, 0, 0.87);
}
You can check Working Demo
Let me know if you face any issue.
How to change the color of a CheckBox?
If your minSdkVersion
is 21+ use android:buttonTint
attribute to update the color of a checkbox:
<CheckBox
...
android:buttonTint="@color/tint_color" />
In projects that use AppCompat library and support Android versions below 21 you can use a compat version of the buttonTint
attribute:
<CheckBox
...
app:buttonTint="@color/tint_color" />
In this case if you want to subclass a CheckBox
don't forget to use AppCompatCheckBox
instead.
PREVIOUS ANSWER:
You can change CheckBox
s drawable using android:button="@drawable/your_check_drawable"
attribute.
Flutter - change fill color of datatable checkbox
Here is an attempt to recreate the checkbox theme:
CheckboxThemeData(
side: MaterialStateBorderSide.resolveWith(
(_) => const BorderSide(width: 1, color: Colors.blue)),
fillColor: MaterialStateProperty.all(Colors.red),
checkColor: MaterialStateProperty.all(Colors.white),
),
Try it out using:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool checked = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
checkboxTheme: CheckboxThemeData(
side: MaterialStateBorderSide.resolveWith(
(_) => const BorderSide(width: 1, color: Colors.blue)),
fillColor: MaterialStateProperty.all(Colors.red),
checkColor: MaterialStateProperty.all(Colors.white),
),
),
home: Scaffold(
body: Center(
child: Checkbox(
value: checked,
onChanged: (b) {
setState(() {
checked = b ?? false;
});
},
),
),
),
);
}
}
Edit: After doing some digging, I found that the DataTable
uses ThemeData.colorScheme.primary
as the fill color.
Try this:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool checked = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
colorScheme: ThemeData.light().colorScheme.copyWith(
onPrimary: Colors.white, // Color for checkmark in datatable
primary: Colors.red, // Color used for checkbox fill in datatable
),
checkboxTheme: CheckboxThemeData(
side: MaterialStateBorderSide.resolveWith(
(_) => const BorderSide(width: 1, color: Colors.blue)),
fillColor: MaterialStateProperty.all(Colors.red),
checkColor: MaterialStateProperty.all(Colors.white),
),
),
home: Scaffold(
body: DataTable(
columns: const [
DataColumn(label: Text('Checkbox')),
],
rows: [
DataRow(
selected: checked,
onSelectChanged: (b) {
setState(() {
checked = b ?? false;
});
},
cells: const [
DataCell(Text('hi')),
],
),
],
),
),
);
}
}
Related Topics
How to Adjust Layout When Soft Keyboard Appears
How to Hide Action Bar Before Activity Is Created, and Then Show It Again
How to Pass the Values from One Activity to Previous Activity
Counting Chars in Edittext Changed Listener
Scroll Recyclerview to Show Selected Item on Top
Asynctask Won't Stop Even When the Activity Has Destroyed
How to Change Default Dialog Button Text Color in Android 5
Mapview in a Fragment (Honeycomb)
Find Distance Between Two Points on Map Using Google Map API V2
How to Change Android Minsdkversion in Flutter Project
Viewpager as a Circular Queue/Wrapping
How to Access Call Log for Android
How to Prevent an Android Device from Going to Sleep Programmatically
Change Text Color of One Word in a Textview
How to Automatically Update Application on Android
How to Build the Android Sdk with Hidden and Internal APIs Available