CSS: The addition selection operator is not functioning as expected

The span's background color is not changing correctly when the radio button is changed. Why is this happening and how can it be fixed?

div {
  margin: 0 0 0.75em 0;
}
.formgroup input[type="radio"] {
  display: none;
}
input[type="radio"],
label {
  color: brown;
  font-family: Arial, sans-serif;
  font-size: 14px;
}
input[type="radio"],
label span {
  display: inline-block;
  width: 19px;
  height: 19px;
  margin: -1px 4px 0 0;
  vertical-align: middle;
  cursor: pointer;
  -moz-border-radius: 50%;
  border-radius: 50%;
}
input[type="radio"],
label span {
  background-color: brown;
}
  

input[type="radio"]:checked + p + span{
     background-color:orange;
}
input[type="radio"] + span,
input[type="radio"]:checked + span {
  -webkit-transition:background-color 0.4s linear;
  -o-transition:background-color 0.4s linear;
  -moz-transition:background-color 0.4s linear;
  transition:background-color 0.4s linear;
}
<div class="main">
  <section class="artistpage">
    <div class="search">
      <h1>Artist Directory</h1> 
    </div>
    <ul class="artistlist">
      <li class="artist cf">
        <div class="info">
          <label class="formgroup">
            <input type="radio">
            <span></span>Op1
            <span></span>Op2
            <span></span>Op3
          </label>
        </div>
      </li>
    </ul>
</div>

No matter which radio option I select, the first one always ends up being selected.

Answer №1

Your issue lies in the usage of the direct sibling selector.

input[type="radio"]:checked + span {
     background-color:orange;
}
input[type="radio"] + span,
input[type="radio"]:checked + span {
  -webkit-transition:background-color 0.4s linear;
  -o-transition:background-color 0.4s linear;
  -moz-transition:background-color 0.4s linear;
  transition:background-color 0.4s linear;
}

Here is the updated code with your new query below.

You should consider utilizing the general sibling selector ~

div {
  margin: 0 0 0.75em 0;
}
.formgroup input[type="radio"] {
  display: none;
}
input[type="radio"],
label {
  color: brown;
  font-family: Arial, sans-serif;
  font-size: 14px;
}
input[type="radio"],
label span {
  display: inline-block;
  width: 19px;
  height: 19px;
  margin: -1px 4px 0 0;
  vertical-align: middle;
  cursor: pointer;
  -moz-border-radius: 50%;
  border-radius: 50%;
}
input[type="radio"],
label span {
  background-color: brown;
}
input[type="radio"]:checked ~ span {
     background-color:orange;
}
input[type="radio"] + span,
input[type="radio"]:checked span {
  -webkit-transition:background-color 0.4s linear;
  -o-transition:background-color 0.4s linear;
  -moz-transition:background-color 0.4s linear;
  transition:background-color 0.4s linear;
}
<div class="main">
  <section class="artistpage">
    <div class="search">
      <h1>Artist Directory</h1> 
    </div>
    <ul class="artistlist">
      <li class="artist cf">
        <div class="info">
          <label class="formgroup">
            <input type="radio">
            <span></span>Op1
          </label>
          <label class="formgroup">
            <input type="radio">
            <span></span>Op2
            <span></span>Op2.1
            <span></span>Op2.2
            <span></span>Op2.3
          </label>

        </div>
      </li>
    </ul>
</div>

Answer №2

Your radio button group is not set up correctly. I recommend making some adjustments to it.

<ul class="artistlist">
  <li class="artist cf">
    <div class="info">
      <label class="formgroup">
        <input type="radio" name = "op"><span></span>Option 1
        <input type="radio" name = "op"><span></span>Option 2
        <input type="radio" name = "op"><span></span>Option 3
      </label>
    </div>
  </li>
</ul>

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

What is the best way to keep a text editable in a razor editor without it vanishing?

I'm on a quest to find the name for a certain functionality that has been eluding me, and it's truly driving me up the wall. Check out the razor code snippet I've been using to exhibit my form inputs: <div class="col-sm"> ...

Spacious width that doesn't require a horizontal scrollbar for navigation

My dilemma is with the width of my body and background image. I set the body width to 1920px to match the background-image for the header. However, since I have a narrower wrapper inside the body, the header background's width gets cut off by the wrap ...

Discover the power of using SVG sprites in Vue JS for dynamic, visually appealing CSS background

Utilizing an SVG sprite file in a Vue JS HTML template is successful with the following setup: <svg class="icon"> <use xlink:href="~@/assets/images/icons.svg#edit"></use> </svg> The contents of the icons.svg file typically resem ...

What strategies can I use to solve this JavaScript puzzle?

One of my acquaintances has a webpage online that contains an inline script tag, featuring a JavaScript function: (#1): var domain = window.location.protocol + "//" + window.location.host + '/'; $(document).ready(function() { var count = 5 ...

Trouble with arranging nested bootstrap grid cells

Having a bit of trouble with the bootstrap framework. I am trying to set up a simple box with an image on the left side and some text and other elements on the right side. However, I am encountering issues when it is viewed on smaller screens. Here is ...

Initiate Bootstrap 4 dropdown menus from the initial menu point

I need the dropdown menus to align with the position of the first dropdown. There are multiple dropdowns on the page. Check out the screenshot for reference. JSFiddel: https://jsfiddle.net/kfh9Lbep/ Any ideas on how to achieve this alignment? <div cl ...

Modifying the color of drawings on a Javascript canvas

I am currently working on developing a drawing board using HTML and JavaScript (Node.js on the server side). One challenge I'm facing is implementing a color picker that allows users to change the paint color dynamically. While I could hard code the c ...

Guide to creating a rising or waving effect for text using CSS or JavaScript without the need for animation

Is there a way to style dynamic text like this using HTML? I'm open to using CSS or Javascript, as long as it does not involve animation. ...

Interactive Geography Selector

When updating your personal details on , you are required to choose your country first. Once the country is selected, the system automatically adjusts the city and/or state options based on that specific country's requirements. Can someone provide exa ...

I'm running into an InvalidSelectorError and I could use some assistance in properly defining

As I gaze upon a massive dom tree, my task using NodeJS/Selenium is to locate an element by the title attribute within an anchor tag and then click on the associated href. Despite being a newcomer to regex, I am encountering numerous errors already. Below ...

Create a new file using PHP by submitting a form in HTML

Currently, I am developing a PHP script that is designed to modify a specific file for the user. This script retrieves the contents of the file and places them in a textarea within a form. How can I ensure that any changes made in this textarea are saved b ...

HTML5 sortable lists, ajax posting, and inactive elements are not functioning correctly

I'm currently utilizing a fantastic plugin for jQuery known as HTML5 Sortable. It can be found at . I am attempting to disable certain items using this plugin. My approach involves adding the class="disabled" to the <li> tag and including items ...

Removing white spaces from response HTML in JBoss 5.1 version

Does anyone know of a JBoss 5.1 plugin or utility that can automatically strip leading and trailing white spaces from HTML being sent as a response? Similarly, is there a way to do this for JSP files upon deployment? Any application-specific settings would ...

Apache Server Efficiently Retrieves .php Files from Subfolders

After tirelessly searching the vast expanse of the internet for a solution to my dilemma and trying every method I stumbled upon, I am still facing the same issue. The problem arises when I attempt to access a .php file in a subdirectory from another file ...

Width of flex container overflowing from relative position is at its full capacity

I'm attempting to create a horizontal line across an overflowing flex container by using an absolutely positioned child element with both the left and right set to 0. This requires the container to have a relative position to contain the absolutely po ...

Is there a way to automatically send 404 errors to index.html and update the URL to the homepage URL?

I transformed a large and intricate website into a concise one-page site, necessitating users to be guided from 404 errors to the index.html page. To accomplish this, I added the following code snippet to my .htaccess file: ErrorDocument 404 /index.html ...

What is the best way to set a specific image as the initial image when loading 'SpriteSpin'?

I'm currently working on creating a 360-degree view using the SpriteSpin API. Everything is functioning as expected, but I have an additional request. I need to specify a specific image to be the initial landing image when the page loads. The landing ...

Prevent clicking until the IE 10 and 9 windows have fully loaded

My goal is to prevent clicking on an image element until all the resources have finished loading, enabling the click only after the window.load() or when document.readystate reaches complete status. While this code works well in Chrome and Safari, I am fac ...

Ensure that col-6 expands to fill the entire row in the event that the other col-6 is hidden on a

I'm currently working on a project that involves two columns. <div class="col-6"> <p> Here is some text for the paragraph. </p> </div> <div class="col-6 d-none d-sm-block"> <img class="goal-f1 rounded" src=" ...

Store the ID of a div in a variable

Currently, I have transformed rock paper scissors from a terminal/console game using prompts and alerts to something playable in a web browser through the use of the jQuery library. In order to achieve this transition, I created images for each hand - roc ...