What is the best way to target the label of an unchecked radio button only if there is a single radio button checked?

Consider this HTML structure:

<div>
  <input type="radio" id="a" name="a"><label for="a">aaaa</label>
  <input type="radio" id="b" name="a"><label for="b">bbbb</label>
  <input type="radio" id="c" name="a"><label for="c">cccc</label>
  <input type="radio" id="d" name="a"><label for="d">dddd</label>
</div>

Initially, all labels are not struck through. By clicking on a label, nothing happens. Let's change that using CSS:

input:not(:checked) + label {
    text-decoration: line-through;
}

This implementation strikes through all labels when no radio button is checked, but we want only the selected label to be struck. Can this be achieved with CSS alone?

I experimented with a solution that partially works – it strikes through labels after the selected one. How do we target the first ones?

input[type="radio"]:checked+label+input+label,
input[type="radio"]:checked+label+input+label+input+label,
input[type="radio"]:checked+label+input+label+input+label+input+label {
  text-decoration: line-through;
}

Demo available below

input[type="radio"]:checked+label+input+label,
input[type="radio"]:checked+label+input+label+input+label,
input[type="radio"]:checked+label+input+label+input+label+input+label {
  text-decoration: line-through;
}
<div>
  <input type="radio" id="a" name="a"><label for="a">aaaa</label>
  <input type="radio" id="b" name="a"><label for="b">bbbb</label>
  <input type="radio" id="c" name="a"><label for="c">cccc</label>
  <input type="radio" id="d" name="a"><label for="d">dddd</label>
</div>

Related discussion: CSS selector for a checked radio button's label

Answer №1

You can utilize a combination of :has() and :not() + selectors together.

div:has(input:checked) :not(input:checked) + label {
  text-decoration: line-through;
}
<div>
  <input type="radio" id="a" name="a"><label for="a">aaaa</label>
  <input type="radio" id="b" name="a"><label for="b">bbbb</label>
  <input type="radio" id="c" name="a"><label for="c">cccc</label>
  <input type="radio" id="d" name="a"><label for="d">dddd</label>
</div>

Answer №2

If you want to achieve this, you can utilize the pseudo-class :has(). Check out the mdn entry here.

div:has(> input[type="radio"]:checked)
> input[type="radio"]:not(:checked)
+ label {}

To put it simply: we are aiming to target a label right beside a radio button that is inside a div with at least one checked radio button child.

A more concise version that follows the same logic, inspired by G-Cyrillus, would be:

div:has(:checked) :not(:checked) + label

Demo:

div:has(> input[type="radio"]:checked) > input[type="radio"]:not(:checked) + label { text-decoration: line-through; }
<div>
  <input type="radio" id="a" name="a"><label for="a">aaaa</label>
  <input type="radio" id="b" name="a"><label for="b">bbbb</label>
  <input type="radio" id="c" name="a"><label for="c">cccc</label>
  <input type="radio" id="d" name="a"><label for="d">dddd</label>
</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

Expansive visuals with the Masonry plugin

Looking to create a Masonry layout with images but running into the issue of large image sizes. Wanting to limit each image to 30% of the screen width while maintaining proportionate height. How do websites like Flickr and Pinterest handle this when uplo ...

Image Slider's Functionality Hindered by AJAX

I have been working on implementing a menu bar on a website using an image library called Jcoverflip, where you can find a demo. The Image Slider works perfectly, however, things take a turn when I attempt to load content from different HTML files using AJ ...

Display images that have been uploaded on the page either as a grid view or a list of

In my Reactjs application, I am uploading multiple images to Azure Blob Storage. On a page, there is a button to open the upload form. Once the upload completes, I want to display all these images on the same page. Since the images have different aspect ...

Simple method for swapping out an HTML string with image tags solely using JavaScript

In our Ionic 2 App, we are displaying content from a backend API in the form of Questions and Answers. The Answers are generated using summernote, allowing us to render raw HTML in the app. However, we need to replace all instances of img tags in the answe ...

Adding a 'dot' to the progress bar in Bootstrap enhances its visual appeal

I am currently working on a progress bar and I would like it to look similar to this: https://i.sstatic.net/TSDEy.png However, my current output looks like this: https://i.sstatic.net/rQhYc.png I'm puzzled as to why the tooltip is floating there, ...

Can CSS be used to horizontally align individual characters by adjusting letter-spacing?

When I apply the CSS rule letter-spacing: 16px;, I observe that the characters are aligned to the left within their designated space, causing the cursor to jump towards the right as if an additional space was added after each character. I am wondering if ...

How can you declare variables in CSS using LESS?

I am facing a challenge where I need to dynamically change the branding color and other styling variables on the portal based on certain conditions at runtime. I have successfully implemented this functionality using CSS with the code snippet provided be ...

Is there a way to use javascript to ensure that CSS variables stick even when overwritten by onclick events?

I have successfully implemented a method to change CSS variables using advice found here. However, I am running into an issue where the changes do not persist. After clicking on a navigation link, the styles momentarily switch to the new values but quickly ...

"Utilize jQuery to superimpose an image on top of

I'm attempting to create a feature where clicking on an image will reveal text underneath, similar to how retailmenot.com displays coupon codes. In addition, when the image is clicked, users should be directed to an external URL. Here is an example o ...

Utilizing JavaScript to dynamically alter an image based on selected dropdown option

I am currently facing an issue with my code where the selected image is not changing when I choose an option from the dropdown list. There are a total of 5 images, but for some reason, they are not displaying correctly. Here is the snippet of my code; < ...

Quality of check-boxes and radio-buttons

Looking for a list of checkboxes with a single selection option, similar to radio-buttons The ability to only select one checkbox is important. Although I need the functionality of checkboxes, such as being able to deselect them with an additional click, ...

What is the best way to customize the styles of an autofilled input in Chakra UI?

Currently, I am customizing the styling of my input fields using Chakra UI's extendTheme function. However, I have encountered a challenge with styling inputs that have been auto-filled. The :autofill pseudo selector does not seem to work as expected ...

Here is a step-by-step guide on creating a navigation bar with a Login and Sign Up button using Bootstrap 4

I am in the process of creating a navigation bar in bootstrap 4, which includes Login and Sign Up buttons. To get a clear idea of what I'm aiming for, please refer to the images below: https://i.sstatic.net/ibZKa.png https://i.sstatic.net/OUWsX.png ...

Arrange the list based on one of its offspring

My navigation bar consists of a first list, an image, and a second list. I am trying to center the image and position the lists around it. To better illustrate my issue, I have created a visual representation on Tailwind Playground since I am implementing ...

Align the text within the span element to be at the top of its containing

How can I align the text with the very top border of its span element? In this snippet, there seems to be some extra space caused by the line-height property. Is there a workaround for this? .name { display: inline-block; line-height: 1.25; font- ...

The button's color doesn't seem to change to the correct one when clicked

Despite my efforts, I can't seem to get the button border to match the color of the hover state. I've tried using the focus selector and even the active state, but nothing seems to work. Below is the relevant code snippet from the project, and yo ...

Incorporating input utilities into a text field

Currently working on a Django blog website catering to bloggers who are not well-versed with Markdown/Markup. I am considering incorporating these tools into my textarea, as shown in the image below: https://i.sstatic.net/ud4hV.jpg Any recommendations on ...

Enhancing CSS and HTML: Increasing the Border Color of Dropdown Menus and Utilizing Multiple Words per Dropdown Item

https://i.sstatic.net/ZI8Ch.png CODE div.box { display: block; text-align: center; margin: auto; z-index: 5 } #navMenu { margin: 0; padding: 0; } #navMenu ul { margin: 0; padding: 0; line-height: 30px; } #navMenu li { margin: 0; padding: 0; list- ...

PNG file unable to display in Google Chrome

I created an image using paint.net and saved it as a .png file. However, when I try to display the image on my website, only the borders show up without any image content. Here is the code I used: HTML <a href="home.php"><img id="logo" src="../i ...

What is the best way to utilize PHP to run various functions using multiple buttons on a webpage?

In my recent project, I successfully implemented a feature where clicking a button on a webpage triggered a text change for all viewing devices. This was achieved by running PHP code on button click to instruct all devices to execute a JavaScript function ...