Is there a way to modify the text color of a radio button?

My project involves creating a segmented switch, and I'm attempting to implement transitions to alter the color of text. Unfortunately, I've encountered an issue where the text color change does not seem to be working as expected.

Check out my code on JSFiddle

Below is the CSS styling I am using:

#Favorites_Switch:checked ~ #Selected_Container {
position: absolute;
left: 50%;
transition: all .15s ease;
border-radius: 0px 5px 5px 0px;
}

#Favorites_Switch:checked ~ #Favorites_Text {
color: #38D1A9;
transition: color .2s ease;
}

#Favorites_Switch:checked ~ #All_Text {
color: #FFFFFF;
transition: color .2s ease;
}

#All_Switch:checked ~ #Selected_Container {
position: absolute;
left: 0%;
transition: all .15s ease;
}

This is how the HTML is structured:

<div id="Segmented_Control">

    <input type="radio" name="Switch" value="All" id="All_Switch" class="Radio_Switch"/>
    <input type="radio" name="Switch" value="Favorites" id="Favorites_Switch" class="Radio_Switch"/>

    <div id="Selected_Container">
    </div> <!-- Selected Container -->

    <label for="All_Switch">
        <div class="Switch_Containers" id="All_Container">
            <p class="Switch_Text" id="All_Text">All</p> <!-- All Switch -->
        </div> 
    </label> <!-- All Switch Label -->

    <label for="Favorites_Switch">
        <div class="Switch_Containers" id="Favorites_Container">
            <p class="Switch_Text" id="Favorites_Text">Favorites</p> <!-- Favorites Switch -->
        </div> <!-- Favorites Container -->
    </label>

</div> <!-- Segmented Control -->

I'm currently stuck trying to understand why the font color remains unchanged when clicking on the label divs.

Answer №1

There seems to be an issue with the functionality because the input checkbox elements are not directly linked to the text elements (#Favorites_Text/#All_Text). In order to resolve this, you should target the sibling label elements first, and then access the text elements nested within.

To clarify, the code snippet would need to be adjusted as follows:

#Favorites_Switch:checked ~ label #Favorites_Text { ... }
#Favorites_Switch:checked ~ label #All_Text { ... }

Check out the updated example here.

#Favorites_Switch:checked ~ label #Favorites_Text {
  color: #38D1A9;
}

#Favorites_Switch:checked ~ label #All_Text {
  color: #FFFFFF;
}

Additionally, I included a transition on the .Switch_Text element to ensure that the effect is consistent both when checking and unchecking the checkboxes.

If the transition is only applied when the checkboxes are unchecked, it may result in inconsistent behavior.

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

Issues with Input-group-Addon functionality in Bootstrap 4

During my current JavaScript course project using Bootstrap 4, I encountered an issue with the input-group-addon class on a span element. The icon is not displaying as shown in the tutorial. <!doctype html> <html lang="en"> <head&g ...

Tips for implementing clickable elements on both desktop and mobile platforms

I have encountered an issue with my CSS function focusCard(element) { element.getElementsByClassName("front")[0].classList.toggle("show"); element.getElementsByClassName("back")[0].classList.toggle("show"); } .box { cursor: pointer; width: 270px ...

When the parent element is centered, align the children to the left

Can children be aligned to the left when the parent is aligned to the center? I have a list of elements where the parent block should be centered, but the children need to align to the left. https://i.sstatic.net/ixRxQ.jpg Now I found a solution to alig ...

Avoid button wrapping in Bootstrap 4 navbar

Is there a way to make the search bar shrink on mobile view while keeping everything in line? Currently, the button is wrapping underneath the search bar. Check out the solution on JSFiddle body { margin: 10px; } .navbar-container { display: fle ...

Angular error message: Trying to access the property 'name' of an undefined object leads to a TypeError

I'm having trouble phrasing this question differently, but I am seeking assistance in comprehending how to address this issue. The error message I am encountering is as follows: TypeError: _co.create is not a function TypeError: Cannot read property ...

What could be causing Firefox to display the slideshow, while Chrome refuses to show it?

Can anyone figure out why this slideshow is working in Firefox but not in Chrome? (I haven't tested it in IE). Looking for some help! CSS: #slideshow { margin:50px auto; width:60em; height:18em; overflow:hidden; border:0.4em solid; borde ...

The Cordova advanced HTTP POST request functionality is not functioning as expected

I'm currently in the process of connecting my backend to a cordova application, and at this early stage of development, I'm attempting to test it in the browser. Unfortunately, I'm facing an issue with the post request not working, as well a ...

Is there a way to prompt the user to enable their Bluetooth functionality using JavaScript?

Currently working on a web application that requires the user to enable their Bluetooth connection once the app is loaded. I plan to implement a pop-up message asking the user to turn on their Bluetooth, and if they click "OK", their Bluetooth will be ac ...

Eliminate the hover and click effects on buttons

I'm trying to change the hover and click states of a jQuery Mobile button, but my current CSS override doesn't seem to be working as expected. Here's the button code I have: <a class="aButton" href='#' data-role="button" data- ...

Button remains behind the image, not moving to the front

I have placed my button on the image, but I want it to sit on top of the image. However, the z-index: 1; property is not working as expected. I have tried all possible position attributes to see if it works, but there was no change. I also added z-index to ...

What is the best way to create images that move at random speeds down the screen in a JavaScript video game?

I have a game in development where players control a spaceship moving towards the screen using PC controllers. I am now working on implementing fireball images that drop randomly from the top of the screen at a specific speed and quantity (since there is o ...

Centering text in a pseudo element vertically

I’ve been trying to find a way to centrally position vertically an ::after pseudo-element that includes text (a content attribute). Previous inquiries: I reviewed some previous inquiries on this topic, however, none of the solutions seem to apply to thi ...

Is there a way to determine if a browser is operating in compatibility mode?

Is there a way to determine if the browser is in compatibility mode or not? I need to apply different CSS depending on the mode. ...

Is there a way to add a gradient of colors to the background?

I'm looking for instructions on how to achieve a multi-color background screen similar to the image I have provided. Can anyone help with this? ...

Place a vertical slider adjacent to an HTML element on either the left or right side

I'm struggling to bind a range slider to the left or right side of a canvas that displays video. Despite my efforts, CSS seems to be putting up a strong fight. I can handle issues like stretching and slider values, but attaching it to the canvas is pr ...

Troubleshooting problems with CSS background-image cover styling

My sanity is hanging by a thread as I struggle with an issue on my new webpage. I'm working on a project at www.romaheritage.co.uk/beta, and I can't seem to get a background image to show up in the "contact" section near the bottom of the page wh ...

Chrome not displaying scrollbar when using overflow-y property set to scroll

I am experiencing an issue with the scrolling functionality in Chrome version 84.0.4147.89. I have a div that is set to overflow-y:scroll;, but unfortunately, the scroll bar is not visible. This poses a challenge for users without a mouse or touchscreen as ...

The content of the HTML request may not always be displayed exactly as it appears in the browser

I've been trying to extract comments from a website using Python and urllib. Although I am successful in obtaining the HTML, I noticed that the comment section is missing. Here's what my Python code retrieves: <div data-bv-product-id="681012 ...

Gradually reveal the background image as the page loads

Is there a way to add a fadeIn effect to the random background image loading using jQuery? Currently, the script I am using displays a random background on refresh without the desired transition effect. How can I achieve the fadeIn effect when the image ...

Attempting to align DIV horizontally in the center

I am currently attempting to horizontally center the div with the id "lastrow". <div class="container"> <div class="row" style="text-align:center"> <div class="col-md-12">Name: <input type="text" id="name"><span class="v ...