Issue: Checkbox background color does not change when checked

I've encountered some issues with customizing my checkbox. Despite trying various solutions that have worked for others, I can't seem to get it right. My goal is to have a green background color and a white check mark appear on the checkbox once it has been clicked. Below is the code snippet I have been working on, and you can find more details in the following Codepen link.

Codepen link: --> https://codepen.io/paulamourad/pen/oPpbEm

/* Custom styling for the label (the container) */

.form-check {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 0.9em;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


/* Hide the default browser checkbox */

.form-check input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}


/* Custom design for the checkbox */

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  border: 2px solid #D6D4D4;
}


/* Styling for the checkmark/indicator when checked */

.form-check input:checked~.checkmark:after {
  display: block;
}


/* Additional visual style for the checkmark/indicator */

.form-check .checkmark:after {
  left: 2px;
  top: 3px;
  width: 5px;
  height: 10px;
  border: solid #8ABE57;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}
<label class="form-check">
                                <input class="form-check-input" type="checkbox" value="" id="checkPeriodDays">
                                <span class="checkmark"></span>
                                <label class="form-check-label ml-2" for="checkPeriodDays">
                                    Días
                                </label>
</label>

Answer №1

.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;
}


.container input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}


.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background-color: #eee;
}

.container:hover input ~ .checkmark {
    background-color: green;
}


.container input:checked ~ .checkmark {
    background-color: green;
}

.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}

.container input:checked ~ .checkmark:after {
    display: block;
}


.container .checkmark:after {
    left: 9px;
    top: 5px;
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 3px 3px 0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Checkbox Example</title>
</head>
<body>
<label class="container">Option One
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>
<label class="container">Option Two
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
<label class="container">Option Three
  <input type="checkbox">
  <span class="checkmark"></span>
</label>
</body>
</html>

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

Using jquery to refresh several backgrounds at once

While I have some knowledge of CSS and various JavaScript libraries, I've encountered difficulty in integrating multiple backgrounds into the code that I found on this link: http://codepen.io/quasimondo/pen/lDdrF. My goal was to overlay a transparent ...

Combining PHP and MySQL with a join statement

I am trying to calculate the average rating for each store in a directory list using MySQL's AVG function. Whenever someone reviews a store, they assign a rating ranging from 1 to 5. This rating is then inserted into the rating column of the reviews ...

Different position of navigation list-style visible in Chrome browser

Currently, I am working on a website where the main menu navigation is designed using the list-style property to separate each menu item. In Firefox, everything appears as intended with small circles between each menu item. However, when viewed in Chrome, ...

Determining the Width of a DIV Dynamically with CSS or LESS Depending on the Number of Siblings

One of my challenges involves a parent DIV with a width set to 100%. Dynamically, this parent DIV is filled with numerous children DIVs. I am trying to calculate and assign their widths using only the calc method in CSS or LESS. This is because the flex ...

Unable to scroll further, however, an iframe is overflowing

After adding an iframe and various tags on the page to enable scrolling, I was hopeful that it would work. Unfortunately, it did not. You can view the HTML and CSS of the page here: What steps should I take next? ...

I could use some help navigating payment gateways

I'm new to payment gateways and would appreciate any recommendations or advice. ...

What is the secret to creating double-width characters that are precisely double the size of single-width characters?

When using terminal, many fonts that support double-width characters display them as exactly twice the width of single-width characters. For instance, these two lines should appear with the same width: あああ aaaaaa However, this consistency is not ma ...

Numerous web pages featuring the same title and navigation menus

Creating my personal academic website involves multiple pages that share a common structure. Question: I'm searching for a method to avoid duplicating the header and menu code in each HTML file. Is there an approach where I can store the code for the ...

What could be the reason for the discrepancy between my get_token() function and the value obtained from request.META.get("CSRF_COOKIE")?

Can anyone shed light on why I'm facing this particular issue? I am currently exploring the integration of Angular 17 as a Frontend with Django as a Backend. While validating a form, I encountered an issue where the token obtained from Django does no ...

What is the most effective way to retrieve the full height of a webpage using JavaScript

  Is there a way to get the actual height of the webpage, not just the browser screen height? I noticed that clientHeight only returns the height of the window! Thank you for your help! ...

Ways to customize hover color of Bootstrap 5 Dropdown menu

I'm having trouble modifying the hover color for a dropdown menu using Bootstrap 5. I'm unsure of the correct method to achieve this. The desired outcome is as follows: However, at the moment it appears like this: Apologies for the oversight, h ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

`Weaving mesh into place with three.js`

I'm struggling to grasp how to position my cubes on the canvas. I can't quite figure out how positioning actually works. I'm trying to find a way to determine if my mesh reaches the limits of the canvas. But what exactly is the unit of posit ...

Utilizing the power of Scoped CSS with Bootstrap/Bootstrap-Vue Integration

I'm currently working on a chrome extension and utilizing Bootstrap-Vue in my Vue files. I have imported bootstrap/bootstrap-vue into my js file, but the styling is being applied globally. Is there a way to scope the Bootstrap only onto specific inser ...

Developing a chat room functionality with HTML5 websockets

I am interested in creating a multi-room chat application using HTML5 WebSockets, but I could use some guidance to get started. My plan is to write the server-side code in C#, however, I am having trouble finding tutorials on how to implement a WebSocket ...

What is the trick to vertically aligning two inline block divs with different font sizes?

Within a single div element, there are two child divs styled as inline-block with different font sizes. This results in varying heights for each child div. What criteria does the browser use to vertically position the left child div? Why don't they b ...

Modifying the Vue page background directly from a page: a step-by-step guide

UPDATE: I recently discovered that I need to modify the body background-color, but I am unsure of how to accomplish this from within the style of this page based on the wrapper's class. The class is determined by the data values. I aim to change the ...

Having trouble aligning image and expanding video to fit the screen in a NextJS application

I need help with: centering the image above the waitlist sign up scaling the background video in a relative way to fill the screen regardless of browser size This is the relevant part of index.js: import Head from 'next/head' import Image from ...

updating the background image in CSS after uploading a photo using Ajax

Whenever I click on an element, it triggers an input dialog that allows me to upload a photo. The server then responds with a JSON OBJECT containing 4 items: NAME, TYPE, HEIGHT, WIDTH. My goal is to refresh the element's background with the newly upl ...

Avoid changing the size of the button based on the number of words it contains

How can I prevent a button from resizing based on the number of characters inside it? I have noticed that two buttons with the same CSS class are displaying different sizes due to the amount of text inside them. https://i.sstatic.net/BggFW.png https://i. ...