The feature of Switch css does not appear to function properly when used in conjunction with input type

Why does the switch button not work with the input type radio but works with a checkbox button? How can I maintain the radio input and solve this issue?

@charset "utf-8";
/* CSS Document */

/* ---------- GENERAL ---------- */

body {
background: #4a4a4a;
color: #151515;
font: 100%/1.5em "Lato", sans-serif;
margin: 0;
}

input {
    font-family: inherit;
    font-size: 100%;
    line-height: normal;
    margin: 0;
}

input[type="radio"] {
    box-sizing: border-box;
    padding: 0;
}

/* ---------- SWITCH ---------- */

.container {
height: 64px;
left: 50%;
margin: -32px 0 0 -80px;
position: absolute;
top: 50%;
width: 160px;
}

.switch {
background: #fff;
border-radius: 32px;
display: block;
height: 64px;
position: relative;
width: 160px;
}

.switch label {
color: #fff;
font-size: 48px;
font-weight: 300;
line-height: 64px;
text-transform: uppercase;
-webkit-transition: color .2s ease;
-moz-transition: color .2s ease;
-ms-transition: color .2s ease;
-o-transition: color .2s ease;
transition: color .2s ease;
width: 100px;
}

.switch label:nth-of-type(1) {
left: -75%;
position: absolute;
text-align: right;
}

.switch label:nth-of-type(2) {
position: absolute;
  right: -75%;
text-align: left;
}

.switch input {
height: 64px;
left: 0;
opacity: 0;
position: absolute;
top: 0;
width: 160px;
z-index: 2;
}

.switch input:checked~label:nth-of-type(1) { color: #fff; }
.switch input:checked~label:nth-of-type(2) { color: #808080; }

.switch input~:checked~label:nth-of-type(1) { color: #808080; }
.switch input~:checked~label:nth-of-type(2) { color: #fff; }

.switch input:checked~.toggle {
left: 4px;
}

.switch input~:checked~.toggle {
left: 100px;
}

.switch input:checked {
z-index: 0;
}

.toggle {
background: #4a4a4a;
border-radius: 50%;
height: 56px;
left: 0;
position: absolute;
top: 4px;
-webkit-transition: left .2s ease;
-moz-transition: left .2s ease;
-ms-transition: left .2s ease;
-o-transition: left .2s ease;
transition: left .2s ease;
width: 56px;
z-index: 1;
}

.c-window{
  display: block;
  position: absolute;
  width: 235px;
  height: 235px;
  margin: 0 auto;
  border-radius: 100%;
  border: 8px solid #FFB399;
  background: #5ddfe8; 
  box-shadow: 0px 0px 5px rgba(0,0,0,0.25) inset;
  overflow: hidden;
transition: background 1s ease-in-out;
}


input[type="radio"]:checked ~ .c-window {
  background: #111;
}
 
<div class="container">

       <div class="c-window"></div>
      
<div class="switch white">

<input type="radio" name="switch" id="switch-off">
<input type="radio" name="switch" id="switch-on" checked>

<label for="switch-off">On</label>
<label for="switch-on">Off</label>

<span class="toggle"></span>


  

</div> <!-- end switch -->

</div> <!-- end container -->

I am looking to make the .c-window change its background color when the switch is toggled.

Answer №1

  • There were some typos pointed out by @fcalderan in the comments.
  • You must ensure that .c-window is a sibling, modify the HTML markup and include z-index:-1 to it.
  • Additionally, you should specify which input will alter the background color.

/* CSS Document */

/* ---------- GENERAL ---------- */

body {
  background: #4a4a4a;
  color: #151515;
  font: 100%/1.5em"Lato", sans-serif;
  margin: 0;
}
input {
  font-family: inherit;
  font-size: 100%;
  line-height: normal;
  margin: 0;
}
input[type="radio"] {
  box-sizing: border-box;
  padding: 0;
}
/* ---------- SWITCH ---------- */

.container {
  height: 64px;
  left: 50%;
  margin: -32px 0 0 -80px;
  position: absolute;
  top: 50%;
  width: 160px;
}
.switch {
  background: #fff;
  border-radius: 32px;
  display: block;
  height: 64px;
  position: relative;
  width: 160px;
}
.switch label {
  color: #fff;
  font-size: 48px;
  font-weight: 300;
  line-height: 64px;
  text-transform: uppercase;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
  width: 100px;
}
.switch label:first-of-type {
  left: -75%;
  position: absolute;
  text-align: right;
}
.switch label:last-of-type {
  position: absolute;
  right: -75%;
  text-align: left;
}
.switch input {
  height: 64px;
  left: 0;
  opacity: 0;
  position: absolute;
  top: 0;
  width: 160px;
  z-index: 2;
}
.switch input:checked~label:first-of-type {
  color: #fff;
}
.switch input:checked~label:last-of-type {
  color: #808080;
}
.switch input:checked~label:first-of-type {
  color: #808080;
}
.switch input:checked~label:last-of-type {
  color: #fff;
}
.switch input:checked {
z-index: 0;
}
.switch input:checked~.toggle {
  left: 4px;
}
.switch input~:checked~.toggle {
  left: 100px;
}
.toggle {
  background: #4a4a4a;
  border-radius: 50%;
  height: 56px;
  left: 0;
  position: absolute;
  top: 4px;
  -webkit-transition: left .2s ease;
  -moz-transition: left .2s ease;
  -ms-transition: left .2s ease;
  -o-transition: left .2s ease;
  transition: left .2s ease;
  width: 56px;
  z-index: 1;
}
.c-window {
  display: block;
  position: absolute;
  width: 235px;
  height: 235px;
  margin: 0 auto;
  border-radius: 100%;
  border: 8px solid #FFB399;
  background: #5ddfe8;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.25) inset;
  overflow: hidden;
  transition: background 1s ease-in-out;
  z-index:-1
}
input:last-of-type[type="radio"]:checked ~ .c-window {
  background: #111;
}
<div class="container">
 
  <div class="switch white">
    <input type="radio" name="switch" id="switch-off">
    <input type="radio" name="switch" id="switch-on" checked>
    <label for="switch-off">On</label>
    <label for="switch-on">Off</label>
    <span class="toggle"></span>
    <div class="c-window"></div>
  </div>
  <!-- end switch -->
</div>
<!-- end container -->

Answer №2

Is it possible to achieve this using javascript or jQuery?

Here is the HTML code snippet:

<div class="container">
    <div class="c-window"></div>
    <div class="switch white">
        <input type="radio" name="switch" value="0" id="switch-off" class="switch">
        <input type="radio" name="switch" value="1" id="switch-on" class="switch" checked>

        <label for="switch-off">On</label>
        <label for="switch-on">Off</label>
        
        <span class="toggle"></span>
    </div> <!-- end switch -->
</div> <!-- end container -->

The following jQuery code can be used to handle the change event:


$(document).ready(function(){
    $('.switch').on('change', function(ev){
        if ($(this).val() == 0){
            // Perform actions when off
        } else {
            // Perform actions when on
        }
    });
});

Answer №3

To utilize the ~ selector, your code should be structured like this:

body, html {
  margin: 0;
  padding: 0;
}

.content {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  position: relative;
}

.c-window {
  position: absolute;
  transition: all 0.3s ease-in;
  width: 235px;
  height: 235px;
  border-radius: 50%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: #aaa;
  border: 5px solid black;
  z-index: 1;
}

input {
  position: relative;
  z-index: 2;
}

#switch-off:checked ~ .c-window  {
  background: blue;
}

#switch-on:checked ~ .c-window  {
  background: #aaa;
}
<div class="content">
  <input type="radio" name="switch" id="switch-off">
  <input type="radio" name="switch" id="switch-on" checked>
  
  <div class="c-window"></div>
</div>

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

Positioning and resizing elements based on varying screen sizes and levels of zoom

I recently designed a basic webpage using HTML with two main elements - a chat window for user interaction and a chart.js plot. Here is a snippet of the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

The resulting line will consist of the discovered string

Let's say there's an interesting HTML document presented below with associated line numbers: 30 - <div id="myDiv"> 31 - <span class="mySpan">Some Text</span> In this scenario, PHP is being utilized for a specific purpose: $ ...

Jquery Timer that can be toggled on and off with a single button

I'm really struggling to find a way to make this work smoothly without any bugs. The button in the code below is supposed to perform three actions: Initiate a countdown when clicked (working) Stop the countdown automatically and reset itself when it ...

Is it possible to include padding within a textarea element?

Is it possible to add some left margin inside a textarea? I would like there to be some extra space because I am using an image icon as my button within the textarea, and when I type, the words end up covering the image. <div id="inputBox"> < ...

Utilizing multiple dropdown buttons in navigation menu items with Bootstrap 4

Trying to find a solution for having sub menus within the menu items. There are 2 dropdown buttons (Reports and Views) within a menu item that is itself a dropdown item. Clicking on the first button displays the submenu below, but clicking on the second ...

What is the maximum file size that the data link is able to store?

For instance, a file like an image, video, or sound can be saved in the data link Take an image for example, it may be stored with the initial link: data:image/jpeg;base64,/..... followed by various characters. But, is there a specified size limit at whic ...

Tips for setting a default value in a Reactive Form

How can I transfer a default value from HTML to TypeScript using reactive forms? <ul class="list list_2"> <li>Subtotal <span>{{cartTotal | currency:'INR':true:'2.0'}}</span></li> <li>Shippin ...

Conditional text-boxes can be added to table columns based on certain conditions

I am working with a table that has three columns: type, 1st type, and 2nd type. The type column contains a button which toggles between two values, Db and Cr. I need to dynamically add a text box to the 1st type column when the button's value is Db, a ...

Populate the row with an image while maintaining its size

I have encountered an issue with containing images within a Bootstrap row. When I insert an image, it does not stay contained within the row and instead overflows causing scrollbars to appear. My goal is to have the image fit seamlessly into the container ...

Struggling to create a search bar and dropdown menu that adapts to different

Currently, I'm working on creating an advanced search bar and came across this template here. Everything seems to be functioning properly except for the fact that the search bar and dropdown menu are not fully utilizing the entire width of 100%. I wou ...

Broaden the default className attribute of the component

Greetings! I'm currently using Bootstrap with React and I am trying to figure out how to extend my component by passing className props deeper. Within my atom component, I have two separate files. The first one contains the component declaration. B ...

Arranging cells within a table with numerous rowspan configurations

I need some help creating a table that will be divided using rowspans for a PDF rendering with BFOreports. I have included an example layout below (numbers are just for reference): https://i.stack.imgur.com/Wbe96.png However, the output I am currently ge ...

Is it possible to insert a button into a specific column of an ngx-datatable within an Angular 8 application?

I am having trouble with this particular HTML code. <ngx-datatable-column name="Option" prop="option" [draggable]="false" [resizeable]="false [width]="250"> <span> <button class="optionButton" type="button" data-to ...

Conceal Choices During Search using jQuery Select2

I'm having trouble figuring out how to make the Select2 plugin work for my specific color selection feature. My goal is to allow users to choose 5 colors from a list, including an "Other" option. When the user selects "Other", they should be able to ...

Is the order of elements in a CSS file important?

In my situation, I am dealing with a nested list and enclosing div that was created by Drupal's menu system, so I am unable to modify it. My goal is to replicate the menu from a hardcoded website (link removed). How can I include the sub-items (ul l ...

Flickity remains in plain sight on desktop devices

I am trying to hide the flickity slider on desktop and larger devices. I have followed the instructions in the documentation, but for some reason, it's not working as expected. Here is how the div looks: <div class="w-full flex pl-4 pb-16 overflo ...

Invoke a jQuery function in the parent page using regular JavaScript from an iframe

I have successfully created an iframe using regular javascript. Inside the iframe is a Jquery function along with JQuery itself and it functions correctly within the iframe. However, I am now looking to execute this function from the parent page instead of ...

Tips for forwarding an image uploaded using Flask Dropzone to a different page

I recently used Flask Dropzone to upload an image into the uploads folder. My goal is to pass this uploaded image to my makeMeme page so that I can display it using the html img tag. Despite my efforts, when I attempt to reference the file for displaying, ...

What is the best way to add two hyperlinks within a single tab of a Bootstrap navigation menu?

I attempted to place two links in one tab, but I was unsuccessful. Here is my navbar code: <!DOCTYPE html> <html lang="eng"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scal ...

Guide to centering a list on a webpage using Bootstrap 3

Looking to create a centered list using the latest version of Bootstrap 3. Any tips? The desired list should be positioned in the middle of the page. ___________________________________________ | | | ...