When I select a checkbox, the mark should not appear as selected, and conversely

I've been working on styling a checkbox with some code. However, the checkbox mark is not hidden by default. I would like the mark to be hidden initially and only shown when selected. Can someone please help me solve this issue? Thank you in advance.

.checkbox {
    width: 38px;
    height: 38px;
    margin: 10px;
//  border: 1px solid #000;
    background: #A53692; /*blue purple*/
}
.label {
    position: relative;
    width: 645px;
    margin: 10px;
    padding-left: 20px;
    line-height: 38px;
    text-align: left;
    color: #fff;
    background: #A53692; /*blue purple*/
    cursor: pointer;
}
.checkbox input[type=checkbox] {
    content: "";
    width: 38px;
    height: 38px;
    margin: 0;
    left: 0;
    opacity: 0;
    cursor: pointer;
}
.label label::after {
    content: "";
    height: 6px;
    width: 9px;
    border-left: 2px solid;
    border-bottom: 2px solid;
    transform: rotate(-45deg);
    position: absolute;
    left: -45px;
    top: 14px;
    cursor: pointer;
}
/*Hide the checkmark by default*/
.checkbox input[type="checkbox"] + .label label::after {
    content: none;
}
/*Unhide the checkmark on the checked state*/
.checkbox input[type="checkbox"]:checked + .label label::after {
    content: "";
<div class="request-sample-form-items">
    <div class="checkbox"><input type="checkbox" id="checkbox"></div>
    <div class="label"><label for="checkbox">Select if you want to fill this form automatically next time.</label></div>
</div>

Answer №1

Give this code a shot:

/* Custom Checkbox Styling */
.label-check {
  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 default checkbox */
.label-check input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create custom checkbox design */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #A53692;
}

/* Change color on hover */
.label-check:hover input ~ .checkmark {
  background-color: #A53692;
}

/* Show different color when checked */
.label-check input:checked ~ .checkmark {
  background-color: #A53692;
}

/* Design the checkmark shape */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Display checkmark when checked */
.label-check input:checked ~ .checkmark:after {
  display: block;
}

/* Style the checkmark shape */
.label-check .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);
}
<label class="label-check">Checkbox One
  <input type="checkbox" checked="checked">
  <span class="checkmark"></span>
</label>

Answer №2

Here's a trick to eliminate opacity in your CSS code:

.checkbox input[type=checkbox] {
    content: "";
    width: 38px;
    height: 38px;
    margin: 0;
    left: 0;
    display: none; //REPLACE OPACITY
    cursor: pointer;
}

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

Creating a responsive design for mobile apps in Ionic using CSS

I need help with making my Ionic app responsive across all mobile devices. The design on the page was created using CSS, but it is not displaying properly on every device. How can I ensure that it adapts to different screen sizes? <template> <Io ...

What is the process for incorporating a scrolling feature into my HTML page?

I need to display the following content on my website: What code do I need to add in order to achieve this? I tried using the code snippet below but it did not work as expected: <td nowrap align="left" style="word-wrap: break-word"><pre style="w ...

The column count is not properly set by Bootstrap 4.3 card-deck

I am working with Bootstrap 4.3 and custom Bootstrap SCSS imports, attempting to configure my card-deck for a custom theme. However, I am facing an issue with the $card-columns-count, as it is not being set correctly to 3 columns (as shown in the screensho ...

The Bootstrap 4 navigation bar remains expanded on tablet screens

I recently obtained and modified a basic bootstrap 4 theme. By customization, I mean that I ensured all menu items have the same margin and added my logo. The issue I encountered is that on tablets, the navbar does not collapse as expected. I have attache ...

Is it possible to create clickable titles in highcharts that also interact with bootstrap popovers?

Hello, I'm trying to figure out how to turn the title in highcharts into a clickable link that works with bootstrap's popover feature. You can find an example of what I'm working on here: http://jsfiddle.net/287JP/4/ $(function () { ...

I'm having an issue with a jQuery navigation plugin that is causing the entire page to refresh. Does anyone

My website includes a jquery slider plugin that I use for navigating between slides. I decided to apply absolute positioning for the previous/next slide navigation, but now I am facing an issue where the navigation refreshes the entire page. Can someone ...

Bootstrap does not conform to standard image sizing conventions

Why is the sizing property of Bootstrap not applying to images when using h-100? When I try to style them inline, everything works as expected, but setting it as h-100 doesn't work for the images. Can someone please explain this issue? <div id=" ...

What is the best way to convert HTML into a React component?

This is the situation I am facing : 1) The application requests a CMS (Content Management System) for page contents. 2) The CMS responds with "<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>" 3) The applicat ...

Vue 3 now allows for disabling the automatic renaming of CSS properties like "top/bottom/left/right" to "inset"

I noticed that Vue (or maybe Vite) automatically changes my css style attributes from "top: 0; right: 0; left: 0; bottom: 0;" to "inset: 0px;" However, this adaptation doesn't work well for older browsers that do not support the i ...

Unstyled Cards Failing to Receive Design

I am currently working on creating a prototype that utilizes two Bootstrap 4 cards to display information from a form and store related information from another form in the second card. The current layout of this setup can be observed below: https://i.sst ...

Unexpected problem discovered with Firefox and JqueryUI - content is misaligned within the dialog box

I seem to be having an issue with a dialog that I recently set up. I've been working with jqueryUi for quite a while so I'm not sure why I can't figure this out. When using Firefox, I noticed that the dialog opens and closes when clicking " ...

AJAX: Displaying the contents of a folder. Issue with URL resolution

I'm attempting to showcase a collection of images stored in a specific folder within a div. My approach involves utilizing AJAX within a JavaScript file titled edit, which is positioned one directory away from the index route. This implementation is b ...

"Utilize the on() method to bind a click event to dynamically generated elements received

After reading several discussions on using on() to link events to dynamically generated HTML elements, I decided to create an example (FIDDLE) where the click event is bound to elements div.clicktitle fetched via AJAX. These div elements contain data attri ...

The ideal caret position while utilizing flexbox for centering within a contenteditable element

After testing on OSX Chrome 45, it appears that align-items: center; works for content alignment, but there is an issue with caret positioning in an empty editable field until text is typed. Is there a solution to this problem that doesn't involve ad ...

My goal is to create a stylish form using bootstrap and CSS3

I am aiming to replicate the layout of this particular page: https://i.sstatic.net/HxOhC.png Struggling to utilize css3 for designing, how can I achieve a form similar to this? Facing difficulty in creating the outer red border and inserting spacing betw ...

Ways to display an SVG spinner prior to a substantial UI refresh

I am currently facing an issue with updating 10 apexcharts bar charts simultaneously in a Vue app. When this process occurs, it takes approximately one second to load completely, and during that time, I would like to display an svg spinner. However, the co ...

Accessing and displaying a randomly selected PDF file from a directory using HTML coding

Welcome to my recipe collection website where I document all the delicious recipes I love to eat. One feature I would like to add is a 'random' button, so that a recipe will be selected at random for me without having to make a decision. Althou ...

"Embedding PHP code within HTML tags, which is then embedded within

Running into an issue within a while loop... echo 'var contentString = '<div id="content" > <div id="bodyContent"> <p>' + $row[name]+ '</p> ...

Retrieving data within individual HTML elements

Is there a way to combine and extract data from different HTML tags for validation purposes? I am interested in fetching values from two separate input fields, merging them into one string, and passing this combined string as an argument to a function with ...

What is causing my nested class to be treated as a sibling rather than a child in HTML code

I have been searching for a clear answer to my query, but haven't found one yet. In my HTML script, I have nested divs structured like this: <ul id="navbar"> <li><a href='#' class='dropdown'>Rules</a> ...