What is causing the radio button's background color not to change after it is selected?

I've been searching and exploring various solutions, but I'm encountering some difficulties with the following scenario: There are a few restrictions:

  1. Cannot utilize Javascript or Jquery.
  2. Must be achieved using pure CSS.

My objective is to change the background color of the label from Blue to Orange after the input is highlighted. Despite my efforts, the desired effect isn't being achieved. I've read through multiple suggestions, but none of them seem to work for me. I'm puzzled about where I might be going wrong.

Here's the CSS code snippet:

    
    .header-tabs-container {
        position: relative;
        float: center;
        left: 25%;
        clear: both;
        z-index: 2;
        border-collapse: collapse;
        white-space: normal;
        border: unset;
       
    }
   
    .header-tabs-container .header-label {
        position: relative;
        padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
        font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
        background-color: blue;
        color: #fff;
         cursor: pointer; 
           user-select: none; 
          text-align: center;  
        z-index: 1;
        margin: 0px;
        border: white 1px solid;     
        white-space: nowrap;
       border-radius: 40px 40px 0px 0px; 
    
    }
    
    
    .header-tabs-container .header-label:hover {
        background: orange;
        color: blue;
        transition: 0.2s;
    }
    
   
    .header-tab-content {
        position: relative;
        background: #eee;
        margin-top: -10px;
        width: 100%;
        min-height: 100vh;
        padding: 0px;
        float: left;
        box-sizing: border-box;
        z-index: 2;
        display: none;
        white-space: nowraπ.
        border: 1px solid blue;
      
    }
    
    .header-tab-content:after {
        content: "";
        clear: both;
    }
   
    input[name="header-tab"] {
        display: none;
    }
 
    input[name="header-tab"]:checked + .header-tab-content{display:block ;transition:  ease-out;}    
        
    input[name="header-tab"]::after + .header-label{
        background-color: orange;
    }         

The HTML code:

    
  <section class="header-tabs-container">
    <label class="header-label" for="header-tab1">Tab1</label><!-- --><label class="header-label" for="header-tab2">Tab2</label>          
  </section>

  <input name="header-tab" id="header-tab1" type="radio" checked/>
  <section class="header-tab-content">
     <h3>Test</h3>
         Content 
     </section>

  <input name="header-tab" id="header-tab2" type="radio" />
   <section class="header-tab-content">
      <h3> test</h3>
      content
   </section>

  </section>

Everything seems to be functioning correctly except for the specific problematic section that refuses to cooperate:

.
input[name="header-tab"]:checked + header-label {background-color:orange}

If you have any insights or guidance on this matter, I would greatly appreciate it!

Thank you in advance.

Answer №1

To style the tabs in CSS, you will need to adjust the position and appearance of the input element along with using the ~ selector for sibling content selection:

.header-tabs-container {
        position: relative;
        float: center;
/*            left: 25%;*/
        clear: both;
        z-index: 2;
        border-collapse: collapse;
        white-space: normal;
        border: unset;
    }
    /* tabs names */
    .header-tabs-container .header-label {
        position: relative;
        padding: clamp(-1.5rem, -3.2rem + 8.8889vw, 3rem);
        font-size: clamp(0.95rem, -0.925rem + 8.333vw, 3rem);
        background-color: blue;
        color: #fff;
        cursor: pointer;
        user-select: none;
        text-align: center;
        z-index: 1;
        margin: 0px;
        border: white 1px solid;
        white-space: nowrap;
        border-radius: 40px 40px 0px 0px;
    }
    
    /* Hover effect on tabs names */
    .header-tabs-container .header-label:hover {
        background: orange;
        color: blue;
        transition: 0.2s;
    }
    
    /* Content area for tabs */
    .header-tab-content {
        position: relative;
        background: #eee;
        margin-top: -10px;
        width: 100%;
        min-height: 100vh;
        padding: 0px;
        float: left;
        box-sizing: border-box;
        z-index: 2;
        display: none;
        white-space: nowrap;
        border: 1px solid blue;
    }
    
    .header-tab-content:after {
        content: "";
        clear: both;
    }
    /* Hide input radio from users */
    input[name="header-tab"] {
        display: none;
    }
    /* Show tab when input checked */
    input[name="header-tab"]:nth-of-type(1):checked ~ .header-tab-content:nth-of-type(1),
    input[name="header-tab"]:nth-of-type(2):checked ~ .header-tab-content:nth-of-type(2) {
        display: block;
        transition: 0.5s ease-out;
    }
    
    input[name="header-tab"]:checked + .header-label {
        background-color: orange;
    }
<section class="header-tabs-container">
      <input name="header-tab" id="header-tab1" type="radio" checked/>
        <label class="header-label" for="header-tab1">Tab1</label>
      <input name="header-tab" id="header-tab2" type="radio" />
        <label class="header-label" for="header-tab2">Tab2</label>
    
      <section class="header-tab-content">
        <h3>Test</h3>
           Content 
        </section>
    
      <section class="header-tab-content">
          <h3> test</h3>
          content
        </section>
    
    </section>

Answer №2

When using the + combinator in CSS selectors, it will only match the second element if it immediately follows the first element. Learn more about CSS Selectors here

To experiment with this concept, try moving the labels and inputs inside the same section and then use the ~ or + combinators. Remember that the input must come first in the sequence.

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

Is AJAX malfunctioning while the success function is functioning properly?

the function for success is operational, however the data isn't being saved in the database $(document).ready(function() { $("#ChatText").keyup(function(e){ if(e.keyCode == 13) { var ChatText = $("#ChatText").val(); ...

Looking to place the bootstrap menu icon on the right side of the page

I have a bootstrap menu item on my website and I want to move the menu icon to the right corner of the page. How can I modify the code to achieve this? #menu { position: relative; color: #999; width: 200px; padding: 10px; margin: auto; font-fa ...

Is there a problem with the way divs are being displayed when using jQuery to show the first 12 elements with the class "hide-show"?

I am encountering a problem with displaying data using the jQuery slice() and show() methods to reveal dynamically generated divs from a PHP function. The code is as follows: <style> .hide-show { display :none; } </style> <div class="c ...

Deactivate the form element when a user selects another button

I am facing an issue with two buttons that are linked to the same modal form. Here is the code snippet for the form: <form name="addUser" ng-submit="(addUser.$valid) ? add() : '';"> <div class="form-group has-feedback" ng-class="ad ...

How to create a slideToggle effect for nested ul and li elements

Initially, everything was working fine until I put it through the W3C validator. After fixing the errors, now it's not functioning as expected. I have created a jsfiddle The setup involves nested ul elements being used as dropdowns, with headers tha ...

Utilizing Piwik Analytics in jQuery Mobile Framework

Having an issue with tracking users on my mobile Web App using Piwik. Due to AJAX, only views on the first page are being tracked. I attempted to use the pageinit function to load the Piwik tracking script on every page, but it still only tracks the firs ...

What causes the difference in behavior between absolute positioning in <button> and <div>?

I am noticing that the code below is not positioning my span to the top-left corner of the button as I expected. Can anyone explain why? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> &l ...

Troubleshooting a malfunctioning custom controller in AngularJS

<html ng-app> <head> </head> <body data-ng-controller="Myfunc"> <ol type="i"> <li data-ng-repeat="c in cust | filter:name"> {{ c.name | lowercase}} - {{c.city | lowercase}}</li> </ol> ...

What is the best way to ensure consistency in a value across various browsers using Javascript?

I am currently developing a feature on a webpage that displays the last update date of the page. The functionality I am aiming for is to select a date in the first input box, click the update button, and have the second box populate the Last Updated field ...

Embedding a thread in an HTML document (Difficult task)

I'm currently working on a method to achieve the following task: Embedding a specific string (such as "TEST") into the content of an HTML page, after every certain number of words (let's say 10). The challenge here is ensuring that the word count ...

Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation ...

Ways to customize label appearance within a TextField using the createTheme function

I attempted to modify the margin of a label using em/rem units instead of px (refer to the image attached to this question), but I am unsure where to apply the styles for proper structuring. I have checked the MUI documentation but couldn't find infor ...

Ensure that the HTML input element only accepts positive values, including decimal numbers

I need to build an input field that only accepts positive numbers, including decimal values. Leading zeros are not allowed, and users should not be able to paste invalid values like -14.5 into the field. Here is my current implementation: <input type= ...

Tips for aligning a logo in the center of a navigation bar

I'm struggling to center a logo within the navigation bar of a website I am designing. Despite my attempts, I have not been successful in achieving the desired result. My goal is to have the logo positioned in the middle of the navigation bar, with ot ...

What is the best way to adjust the spacing in my bootstrap Navbar? I am struggling to make it stretch to the entire width of the screen

I am currently working on a website project where I want to have a navigation bar that spans the full width of the screen with a black background. I also need the links to be centered and evenly distributed within the navbar. My main issue right now is re ...

Layout your Angular components with a column design using Flex Layout

Is there a way to adjust the width of a child component in an fxLayout set to column? Take this example for reference: https://stackblitz.com/edit/angular-fxlayout-custom-breakpoints?file=app%2Ftest.component.ts In the provided example, there are three f ...

ReactJs CSS - This file type requires a specific loader for processing. There are currently no loaders configured to handle this file

I've noticed that this issue has been raised multiple times before. Despite going through all the questions, I still can't seem to resolve it. The transition from Typescript to Javascript went smoothly until I reached the implementation of CSS. U ...

ng-grid defines different cellClass based on the value of the field

I am currently using the ng-grid and I want the column to display in a different color based on different values. I have tried, but not succeeded so far... $scope.gridOptions = { ........ columnDefs: [ { field: "status", displayName: "St ...

Mastering the correct application of flex properties within nested flex containers

I'm struggling with utilizing flexbox properly and would like some clarification on how nesting parent and child elements functions. I understand that the child elements inherit the parent's flex properties, but I'm unsure if this inheritan ...

Click the icon to introduce a fresh row into the table

I have embedded a table with a "plus" font awesome icon. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorig ...