Apply CSS styles to the checkbox

I am new to CSS and I need help styling a checkbox element.

When the user selects one of the checkboxes, I want the background color to change to yellow.

Thank you for any assistance you can provide!

.radio_toggle {
  float:left;
}

.radio_toggle label {
  float:left;
  width:100px;
  overflow:auto;

}

.radio_toggle label span {
  text-align:center;
  font-size: 15px;
  padding:5px 0px;
  display:block;

}

.radio_toggle label input {
  position:absolute;
}

.radio_toggle .venue {
  background-color:#edf1f2;
  color: #58666e;
  min-height: 100% !important;
  border: thin #ced9db solid;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;

}
<div class="categories">
 <div class="radio_toggle">
    <label class="venue" style="">
      <input type="checkbox" name="toggle">
         <span>Category</span>
      </label>
  </div>
</div>
<div class="categories">
  <div class="radio_toggle">
    <label class="venue" style="">
       <input type="checkbox" name="toggle">
           <span>Category</span>
    </label>
  </div>
</div>
<div class="categories">
  <div class="radio_toggle">
       <label class="venue" style="">
           <input type="checkbox" name="toggle">
               <span>Category</span>
       </label>
  </div>
</div>

Answer №1

To make the input disappear, apply the CSS rule display: none;

.radio_toggle label input {
  display: none;
}

To add color to the button, utilize the :checked selector in the CSS rule

.radio_toggle label input:checked + span {
  color: white;
  background: gold;
}

Example

.radio_toggle {
  float:left;
}

.radio_toggle label {
  float:left;
  width:100px;
  overflow:auto;

}

.radio_toggle label span {
  text-align:center;
  font-size: 15px;
  padding:5px 0px;
  display:block;

}

.radio_toggle .venue {
  background-color:#edf1f2;
  color: #58666e;
  min-height: 100% !important;
  border: thin #ced9db solid;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;

}

.radio_toggle label input {
  display: none;
}

.radio_toggle label input:checked + span {
  color: white;
  background: gold;
}
<div class="categories">
 <div class="radio_toggle">
    <label class="venue" style="">
      <input type="checkbox" name="toggle">
         <span>Category</span>
      </label>
  </div>
</div>
<div class="categories">
  <div class="radio_toggle">
    <label class="venue" style="">
       <input type="checkbox" name="toggle">
           <span>Category</span>
    </label>
  </div>
</div>
<div class="categories">
  <div class="radio_toggle">
       <label class="venue" style="">
           <input type="checkbox" name="toggle">
               <span>Category</span>
       </label>
  </div>
</div>

Answer №2

After some advice from @UnknownDeveloper, it seems the best approach is to utilize JavaScript. Firstly, create a new class called .venue_selected and define your desired color schemes. Next, implement a click event for all elements in order to add the class to the label and check the respective checkbox.

<style type="text/css>
    .radio_toggle {
      float:left;
    }

    .radio_toggle label {
      float:left;
      width:100px;
      overflow:auto;

    }

    .radio_toggle label span {
      text-align:center;
      font-size: 15px;
      padding:5px 0px;
      display:block;

    }

    .radio_toggle label input {
      position:absolute;
    }

    .radio_toggle .venue {
      background-color:#edf1f2;
      color: #58666e;
      border: thin #ced9db solid;
      border-top-left-radius: 3px;
      border-bottom-left-radius: 3px;
      cursor: pointer;
    }

    .radio_toggle .venue_selected{
        background: #f9d342 !important;
        color: #ffffff !important;
    }

    .categories input[type="checkbox"] {
      display: none;
    }
</style>

<div class="categories">
    <div class="radio_toggle">
        <label class="venue" style=""><input type="checkbox" name="toggle"><span>Category</span></label>
    </div>
</div>

<div class="categories">
    <div class="radio_toggle">
        <label class="venue" style=""><input type="checkbox" name="toggle"><span>Category</span></label>
    </div>
</div>

<div class="categories">
    <div class="radio_toggle">
        <label class="venue" style=""><input type="checkbox" name="toggle"><span>Category</span></label>
    </div>
</div>

<script type="text/javascript>
    var elms = document.querySelectorAll("label.venue");
    elms.forEach(function(el,i) {
        el.onclick = function() {
            console.log(el);
            el.classList.toggle("venue_selected");
            el.querySelector("input[type='checkbox']").click();     
        }       
    });
</script>

Answer №3

To achieve this task, there are 2 different approaches you can take.

You have the option of utilizing either JavaScript or CSS to accomplish the desired effect.

If you opt for CSS, the process is quite straightforward. You will need to make use of the :checked attribute.

.radio_toggle[type=checkbox] + label {
  // Styling for when checkbox is not checked
} 
input[type=checkbox]:checked + label {
  // Styling for when checkbox is checked
} 

On the other hand, if you choose to utilize JavaScript, you would first need to select the radio_toggle element and then create an event listener to trigger actions based on its checked state. In your scenario, the code snippet might look something like this:

var element = document.getElementsByClassName("radio_toggle").checked = true;
function check(element) {
    element.checked = true;
    element.style.color = "Desired color"
}

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

Extracting information from a hyperlink without the need to actually click on it

Hello, I have recently started learning JavaScript and I am looking to accomplish a specific task. Currently, I am navigating on A.com/. Within the content of A.com/, there is a link labeled as A.com/B. Upon clicking on the link A.com/B, I can see ...

The contents of a Javascript array are not appearing within a div element

I have developed a program that reads JSON data related to a concert event. The JSON file consists of an object named global, which includes details about the band name and venue. Additionally, there is a tickets object that contains information on all ava ...

Aligning divs, prevent duplicate HTML within multiple divs

Currently, I am attempting to troubleshoot a jsfiddle issue. The main problem lies in my desire for the first two divs (with class="fbox") to be aligned next to each other on the same level. Furthermore, I am looking to enable dragging the image into the ...

The Chocolat.js Lightbox plugin is experiencing issues with jQuery as it is unable to detect the

I am in the process of integrating chocolat.js into a basic website. An issue I encountered was that the thumbnail was directly processing the anchor link, which resulted in the image opening in a new window instead of launching it in a modal on the screen ...

New design in TinyMCE V5: Vertical toolbar labels arranged within a CSS grid layout

When TinyMCE version 5 is placed inside a CSS grid layout, the toolbar labels are displaying vertically. It appears that the "width:auto" property is not being applied correctly, and I am struggling to find a solution to fix it. I would greatly appreciat ...

Exploring Techniques for Streamlining HTML/CSS Editing in Browser

Is there a way to automatically edit specific lines on websites right after they load? Specifically, I want to change <div class="1"> to <div class="1" style="display:none">, and <div class="2" style ...

HTML5 allows users to play a extended video for a set duration without the need for any video editing. Users are encouraged to utilize any approach

I have a video file named myvideo.mp4 that is 3 minutes long (or any length). <video width="320" height="240" controls="controls"> <source src="myvideo.mp4" type="video/mp4"> </video> Is there a way to play this myvideo.mp4 for only 20 ...

Can you provide me with instructions on how to change the background image?

Having trouble with the CSS code I wrote for a background-image. It's not showing up when I set it in my CSS stylesheet. I tested it and found that it only works when written in the body... <body background="Tokyo.png"> ...but not in the styl ...

What are some methods to achieve consistent alignment of input fields across different mobile devices using CSS?

I have been working on a login page that looks good on desktop and laptop devices. However, I am facing an issue with its display on mobile devices. The design appears differently on various mobile devices - for instance, it looks acceptable on an iPhone 1 ...

Is it possible to apply a style change to several components at once using a single toggle switch

I am looking to implement a day/night feature on my web app, triggered by a simple toggle click. While I can easily add this feature to a single component using the navigation menu, I am faced with the challenge of incorporating it into multiple component ...

Designing personalized sass components

Seeking advice on developing a custom CSS unit that can be utilized in Sass with Node.js. Is there a tutorial available for creating a Sass plugin specifically for this purpose? For instance, I am looking to establish a unit called "dpx" which functions as ...

Uniqid in React fails to generate unique IDs

Currently working on creating my first react project and developing a todo list. I have incorporated the uniqid generator into my todo object, but it seems to be returning an empty id rather than a random one. Oddly enough, if I move the todo state outsi ...

Firefox triggers drag events while passing over a div scrollbar

I am looking to create a file drag and drop feature using a styled div: When dragging files over the div, I want the border color to change When dragging files out of the div, I want the border color to revert back to the original In Firefox 35 (Ubuntu) ...

Breadcrumb floating to the right, yet it still manages to obstruct other content, forcing it into a

Within the Breadcrumbs section, I have the path home/profile The desired layout involves floating the Breadcrumbs to the right while keeping the Other contents unfloated to the left. Ver1 -------------------- home/profile -------------------- Ot ...

Having trouble getting an Angular directive to bind a click event to an external element?

I've been working on creating a unique custom event for toggling with Angular. The directive I'm using is called toggleable. It may sound simple at first, but the tricky part is that I want to be able to use any button or link on the page for to ...

A guide on detecting overflow in a React functional component

I am in search of a way to determine if a div contains overflowing text and display a "show more" link if it does. While researching, I came across an insightful Stack Overflow answer on checking for overflow in a div. The answer suggests implementing a fu ...

sending a value from HTML to jQuery

I'm attempting to send a parameter from HTML to jQuery, but I seem to be getting the same answer each time even though I'm using a for loop in my PHP file like this: for ($x = 0; $x < $length; $x++) { echo "<button ><a class=' ...

Is there an HTML editing tool that has the ability to collapse code blocks by tags and rearrange them?

I'm in search of a text editor or IDE that includes syntax highlighting and allows me to collapse HTML code blocks by tag. I currently use Eclipse, but its "folding" feature only works on top level tags like TABLE, not child tags like TD and TR. Are t ...

Adjust the header's alignment to seamlessly coincide with the uppermost part of the

I'm encountering this peculiar issue with a website I recently started designing. My aim is to make the header perfectly aligned with the top of the page. However, despite my attempts, there persists a stubborn gap that measures around 20px, defying a ...

Creating a flexible image layout within a container with relative positioning

I attempted to create a basic slideshow with images sliding from right to left as an animation: let slides = document.querySelectorAll('.img-container'); let l = slides.length; let i = 0; setInterval(function(){ i = (i + 1) % l; if(i == 0){ f ...