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

Eliminating divs and preserving content

Is there a way to remove certain DIV elements using jQuery or pure JavaScript without affecting the content within them? Here is an example structure: <div class="whatever_name"> <div class="whatever_name"> <h2>subtitle</h2&g ...

Is it possible to isolate specific HTML elements within a designated area?

Is it possible to identify which elements are located within a specific rectangular region on a web page identified by a URL? UPDATE: The default settings for screen resolution, font size, etc. can all be adjusted to reasonable values. ...

Maintaining Scene Integrity in THREE.JS: Tips for Handling Window Resizing

My layout includes a div with a scene that looks great initially; however, as soon as I start moving or resizing the window, the scene overflows the boundaries of the div and goes haywire, almost filling the entire window. Are there any techniques or solu ...

How can I make the scrollbar invisible in the sticky header of a Material UI Table?

In my React project, I have implemented a Material-UI Table with a fixed header. The issue I am facing is that the scrollbar in the table header overlaps it and I want to hide it while still displaying it in the body of the table. I have attempted to modi ...

Place a "sticky" button directly below a second "sticky" navigation bar

Within my app file, I have customized components like an appbar and various page elements: const styles = theme => ({ appBar: { width:'100%', }, }); class App extends Component { render() { const {classes} = this.props; ...

- Lorem ipsum dolor sit amet, consectetur adipiscing elit

I am looking to create a list with lower-roman indexes that are enclosed in parentheses (ii), where the content of each item is indented. Here is an example: (i) lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut metus nec urna ...

The find element will yield an empty result when using XPath contains with Text() method

When checking for Gender and Date in the assertion, it returns false and the IWebElement displays an empty string. This anomaly only occurs with Gender and Date, while the rest provide accurate results. CODE IWebElement actualname = BrowserHelper.WebDri ...

I rely on the angular-responsive-carousel library for my project, but unfortunately, I am unable to customize the arrow and dots

When it comes to CSS, I utilize ng deep style in Angular 10 to make changes for browser CSS. However, I am facing an issue where the problem is not being resolved by my CSS code. Here is a snippet of my code: > ::ngdeep .carousel-arrow { > b ...

Trouble with Displaying 3rd Level JQuery Dropdown Menu

Currently working on implementing a 3 level dropdown feature. I have been using a third-party responsive menu in Opencart and it's been working well. You can see a demo of it here: Unfortunately, Opencart does not natively support 3 level categories, ...

Styles for Django Rest Framework admin interface are not displaying correctly

Has anyone else experienced an issue with the styling in their Django admin panel? The CSS appears to be functioning properly, but once you navigate to a specific model, the layout becomes distorted. I can't seem to figure out what could be causing th ...

The unique GopikaTwo Gujarati font is unfortunately not compatible with Android mobile browsers and hybrid applications

I am currently trying to incorporate the custom font GopikaTwo into my hybrid application. Below is the code snippet I have added to my CSS file: @font-face { font-family: 'GopikaTwo' !important; src: url('GopikaTwo.eot') !impo ...

Is there a fixed header table feature that comes built-in with HTML5?

Is there a native feature in HTML 5 for implementing a fixed header table with a scrollable tbody, while keeping the thead and tfoot fixed? ...

What is the best way to center a button element horizontally within a div element?

I am looking for a way to center a button horizontally without adding CSS directly to the div element (for example, using <div style="text-align: center;">). My goal is to apply CSS code specifically to the button element. <div> <butto ...

What is the process for implementing the tooltip plugin within a modal dialog in Twitter Bootstrap?

Hello there, I am currently using the latest version of Bootstrap, v2.1.1. I am trying to implement a tooltip in my modal dialog. <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Bootstrap v2.1.1</title> <meta htt ...

Custom Homepage with Integrated Google Web Search and Autocomplete Feature

Is there a way to incorporate the standard Google web search with autocomplete into my own webpage, without using a custom search engine like www.google.com? I have been unable to find any examples or guides on how to accomplish this. All the resources see ...

Different Positions in Flexbox/Container

I am trying to create a series of flexbox items that have both an image and content. I want the images to alternate sides within each flexbox item (one on the left, one on the right, and so on), but I can't figure out how to make it work. Any advice o ...

Updating the Position of an Element in ElectronJS (e.g. Button, Label, etc)

Is there a way to change the positioning of a button in a window using JavaScript and Electron? I am trying to create new input boxes next to existing ones, but they always appear below the last one created. Is it possible to specify x and y coordinates fo ...

The _rfs.scss width rule in Angular-Bootstrap-NgBootstrap is overriding the column width of the col-rules

The Dilemma I am currently working on an Angular project that incorporates Bootstrap and ng-bootstrap. My intention is to utilize the grid system within this project, but I encountered some bugs along the way. When trying to render the following HTML/TS ...

Discover how to access and manipulate JSON files in an Angular application using

Currently, I am diving into learning TypeScript with Angular and I'm interested in reading a JSON file. The structure of my JSON file is as follows: { "nb": "7", "extport": "1176",, "REQ_EMAIL": ...

Obtain pictures from MongoDB for a website using Angular6

I'm currently in the process of developing my website and I want to showcase an image from my database on the header. Each customer is assigned a unique logo based on their ID, but I'm unsure how to use Angular to display it. Below is my code s ...