Enhancing the appearance of radio buttons and checkboxes with Bootstrap 4's

Having trouble customizing radio and checkbox styles in Bootstrap 4?

I'm trying to achieve something like this: 

https://i.sstatic.net/Wp6SR.jpg

I've successfully changed the background, but I'm struggling to change the style when the radio or checkbox is selected. I'm not familiar with using SVG data that can be pasted, although I've read about it on some forums.

Additionally, I attempted to use my own image as an SVG file for the css background-image: url(), but it wasn't recognized.

Below is the HTML and CSS code snippet:

.custom-control-label::before {
  background-color: #f1f2ec;
  border: 2px solid #678000;
  height: 21px;
  width: 21px;
  border-radius: 22px;
  box-sizing: border-box;
}


/* This is the checked state */

.custom-radio .custom-control-input:checked~.custom-control-label::before,
.custom-radio .custom-control-input:checked~.custom-control-label::after {
  /* background-color: #678000; */
  /* border: 2px solid #678000; */
  /* Replace this bg image SVG with any valid SVG code */
  background-image: url("/images/radio-pressed.svg");
  border-radius: 50%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<!-- Radio -->
<div class="col-6 pt-5">
  <form>
    <div class="custom-control custom-radio">
      <input type="radio" class="custom-control-input" id="radioBtn2">
      <label for="radioBtn2" class="custom-control-label"></label>
    </div>
  </form>
</div>



<!-- Checkbox -->
<div class="col-6 pt-5 pb-5">
  <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input" id="customCheck1">
    <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
  </div>
</div>

I've looked at some online demos, but they haven't provided much help. Any assistance you can offer would be greatly appreciated!

Answer №1

To make changes to the bootstrap.css file controlling the checked and indeterminate states, you will need to override certain classes. One way to do this is by using the following CSS:

.custom-control-label::before {
  background-color: #f1f2ec;
  border: 2px solid #678000;
  height: 21px;
  width: 21px;
  border-radius: 22px;
  box-sizing: border-box;
}

.custom-control-input:checked ~ .custom-control-label::before {
  color: #678000;
  border-color: #678000;
  background-color: #f1f2ec;
}

.custom-radio .custom-control-input:checked ~ .custom-control-label::after {
  content: "⬤";
  left: -20.5px;
  top: 1.5px;
  color: #678000;
}

.custom-checkbox .custom-control-input:checked ~ .custom-control-label::after {
  content: "";
  color: #678000;
  position: absolute;
  border: 1px #fff solid;
  border-radius: 3px;
  background-color: #678000;
  background-image: none;
  top: 7px;
  left: -21.5px;
  height: 15px;
  width: 16px;
}

.custom-checkbox .custom-control-input[indeterminate] ~ .custom-control-label::after {
  color: #678000;
  background-image: none;
  background-color: #678000;
  position: absolute;
  height: 2px;
  width: 14px;
  top: 13px;
  left: -20.5px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

<!-- Radio -->
<div class="col-6 pt-5">
  <form>
    <div class="custom-control custom-radio">
      <input type="radio" class="custom-control-input" id="radioBtn2">
      <label for="radioBtn2" class="custom-control-label"></label>
    </div>
  </form>
</div>



<!-- Checkbox -->
<div class="col-6 pt-5 pb-5">
  <div class="custom-control custom-checkbox">
    <input type="checkbox" class="custom-control-input" id="customCheck1">
    <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
  </div>
</div>

Check out the codeply demo here

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

Executing Basic Calculations Instantly with Live Data in Qualtrics

I am encountering an issue with displaying the values on a slider in Qualtrics. I need to show the value where the respondent has placed the handle, as well as the complementary value from the other end of the scale. For example, if the user has chosen 55, ...

A common inquiry: how can one pinpoint a location within an irregular figure?

I have been studying Html5 Canvas for a few weeks now, and the challenge mentioned above has puzzled me for quite some time. An irregular shape could be a circle, rectangle, ellipse, polygon, or a path constructed by various lines and bezier curves... I ...

Deactivate the BACKSPACE button

Similar Question: How to disable backspace except textbox using jQuery I am looking for a solution to prevent the BACKSPACE button from functioning unless it is within a TEXT field. Although I have tried the code below, it ends up disabling the backs ...

AngularJS application is throwing an error indicating provider $q is not recognized

Could someone please advise on what might be the issue with my code snippet below: var app = angular.module('app', [ 'angular-cache', 'angular-loading-bar', 'ngAnimate', 'ngCookies', &a ...

Troubleshooting issue with CSS SVG Clip path functionality - facing technical difficulties

Having an issue with a clip path in my CSS. When I apply the clip path to my CSS fill, the image isn't showing up... I'm using Chrome. Any ideas on how to fix this? I found this helpful generator https://example.com .card .content .picture img { ...

Ensure that the width does not decrease

Edited: included fiddle and code snippets I am facing an issue with the width of columns in my layout. The width of the columns is dynamically calculated based on the content they hold. However, when I filter the results using a search input for a specifi ...

Ways to establish a default search outcome in a search box

Looking to create a search bar with JavaScript and JSON to display default search results for visitors. Currently, the search bar only shows results after text is removed. How can I show predefined search results when the page is loaded? const search = ...

history.js - failure to save the first page in browsing history

I have implemented the history.js library to enable backward and forward browser navigation on an AJAX products page triggered by clicking on product categories. While I have managed to get this functionality working smoothly, I am facing a particular iss ...

"Looking to dynamically adjust attributes within a popover using JavaScript in Bootstrap 5? Let me show you how

I have been attempting to utilize JavaScript to set an HTML attribute in a popover within Bootstrap 5, but I am encountering difficulties. Below is the code snippet for the popover: <button type="button" class="btn btn-secondary mx-3&quo ...

Retrieving information from the table based on the user currently logged in

I have a scenario with two different tables, NGO and Volunteer. When a volunteer selects an NGO to work with, I want to display only those volunteers who are interested in the current logged-in NGO. Below is the code snippet I am using: [<?php ...

Struggling with determining the perfect transition speed for the sidemenu display

I'm a newcomer to web development and facing an issue with setting the transition speed for opening and closing this side menu. Despite adding transitions in the CSS and specifying duration in the Javascript, the menu continues to open instantly. I di ...

Having trouble retrieving a particular element using xpath in selenium with Python

Currently, I am working on extracting wind direction information using selenium and I have found that using xpath is the most straightforward approach to retrieve this data. There is a table containing all the necessary details, and the structure of the el ...

Include links to external CSS and JS files within an Angular 2 component

To exclusively use "Bootstrap Select" in my.component.ts, I attempted the following approach: @Component({ templateUrl: 'my.component.html', styleUrls: [ 'my.component.css', //Bootstrap Select 'https:/ ...

What are the benefits of keeping PHP separate from HTML?

As I am relatively new to the world of programming, I have been learning how to separate PHP from HTML. However, I am curious to know if there is a difference between executing the following code: <?php $rand="I love apples" ?> <h1>This is a t ...

Tips for incorporating a personalized touch to your radio buttons

Is there a way to customize the appearance of radio buttons in React or HTML elements, such as the screenshot below? I want to maintain the display of the number No.01 followed by the label next to it. If you have any suggestions or solutions, please shar ...

Transitioning the style code from inline to the head of the document disrupts the straightforward JavaScript intended to

As I delve into the world of web development, I encountered a simple issue that has been causing me frustration for the past hour. It involves code to display the border color of a div element using an alert. The code works perfectly fine when the style is ...

Is it necessary to specify the declared language in an HTML document?

Is there a way for me to include 'lang="en"' at the end of my opening html tag without manually writing it in? Unfortunately, I don't have direct access to edit the code (ecommerce package), but I want to indicate the site language somehow. ...

Looking to achieve a stretch-auto-auto column layout in Bootstrap 4 - how can this be done?

Essentially, I am looking for something similar to this: https://i.sstatic.net/ss3Ir.png Can this layout be achieved using columns instead of floats? <div class="clearfix"> <div class="float-left"><button class="rounded btn btn-outline ...

Presentation Slider (HTML, CSS, JavaScript)

Embarking on my journey of creating webpages, I am eager to replicate the Windows 10 start UI and its browser animations. However, my lack of JavaScript knowledge presents a challenge. Any help in reviewing my code for potential issues would be greatly app ...

Fancybox is experiencing some technical difficulties

Can anyone help me troubleshoot why my fancybox code isn't working properly after adding the script below to the body? Any thoughts on what might be causing this issue? $(document).ready(function() { $("a#example4").fancybox({ 'opa ...