Personalized radio buttons for displaying or concealing content

My goal is to create an interactive email content block that can display or hide content based on user selection. It was functioning properly until I decided to customize the label. After wrapping the input and label in a div, the functionality ceased. I suspect that this change may have caused some issues with my selectors, but I am uncertain about how to address the problem.

.custom-radios input[type="radio"] + label span img {
  opacity: 0;
  transition: all 0.3s ease;
}
.custom-radios input[type="radio"]#red + label span {
  background-color: red;
}
.custom-radios input[type="radio"]#blue + label span {
  background-color: blue;
}
.custom-radios input[type="radio"]#green + label span {
  background-color: green;
}
.custom-radios input[type="radio"]#orange + label span {
  background-color: orange;
}
.custom-radios input[type="radio"]:checked + label span img {
  opacity: 1;
}


    #red{
    display: none
    }
    #blue{
        display: none
    }
    #green{
        display: none
    }
    #orange{
        display: none
    }
    input[type="red"]:checked ~ #red {
    display: block
    }

    input[value="blue"]:checked ~ #blue {
        display: block;
    }

    input[value="green"]:checked ~ #green {
        display: block;
    }

    input[value="orange"]:checked ~ #orange {
        display: block;
    }
<div class="custom-radios">

  <div>
    <input type="radio" id="red" name="color" value="red">
    <label for="red">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="blue" name="color" value="blue">
    <label for="blue">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="green" name="color" value="green">
    <label for="green">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="orange" name="color" value="orange">
    <label for="orange">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

<p style="margin:0;" id="red">
<img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="blue">
<img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="green">
<img src="https://via.placeholder.com/580x200/00FF00/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="orange">
<img src="https://via.placeholder.com/580x200/FFA500/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>

<div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

</div>

Answer №1

It seems like selectors might be a bit challenging for you at the moment. I recommend taking a look at the CSS Selectors Reference from w3school to strengthen your understanding.

Nevertheless, I have created a simplified version closely resembling your current structure so you can observe it in action.

.hidden_input {
  display: none;
}

#red_input+label>span {
  background-color: red;
}

#blue_input+label>span {
  background-color: blue;
}

label>span>img {
  opacity: 0;
  transition: all 0.3s ease;
}

.hidden_input:checked+label>span>img {
  opacity: 1;
}

#red_content,
#blue_content {
  display: none;
}

#red_input:checked~#red_content,
#blue_input:checked~#blue_content {
  display: block;
}
<div class="custom-radios">
  <input type="radio" id="red_input" class="hidden_input" name="color" value="red">
  <label for="red_input">
    <span>
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
    </span>
  </label>
  <input type="radio" id="blue_input" class="hidden_input" name="color" value="blue">
  <label for="blue_input">
    <span>
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
    </span>
  </label>

  <div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

  <p style="margin:0;" id="red_content">
    <img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
  </p>
  <p style="margin:0;" id="blue_content">
    <img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
  </p>
</div>

Feel free to experiment and position elements within your HTML using grid for further customization.

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 it possible to adjust the background color based on the current time of day?

I have successfully designed a visually appealing website using HTML5, CSS, and Bootstrap. I am interested in finding a way to automatically change the background color and navigation bar selection color based on the time of day - blue during the day and d ...

Using JSON response directly in loaded HTML in Angular 2 is a straightforward process. Follow these steps to

As I delve into Angular 2 and explore its components, one question that arises is how to display data received from a server in JSON format on an HTML page. In Angular 1.x, we could directly assign a JSON object to a variable and use it in HTML using the & ...

Firefox does not support event.dataTransfer.files for drag and drop operations

Currently, I am implementing drag and drop functionality into one of my projects and encountering an issue specifically with Firefox. The code I am using for testing purposes is as follows: document.getElementById("folder_files").addEventListener("drop", ...

How to target the following sibling element (not a child) with CSS

I am working with the following HTML structure: <div class="col-sm-12"> <input id="my_id"> <div class="col-sm-1 col-3-box" style="position:absolute; margin-left:340px;"> </div> </div> How can I target and change ...

Tips for Customizing the Width of Your Material UI Alert Bar

At the moment, I have refrained from using any additional styling or .css files on my webpage. The width of the Alert element currently spans across the entire page. Despite my attempts to specify the width in the code snippet below, no noticeable change ...

Form Validation Issues Detected

Could someone help explain why the code below is causing an error according to W3C guidelines: <form id="" method="post" action="" /> <input type="text" name="" title="" tabindex="10" class="" /> <inp ...

PHP Email attachments are not being saved in the sent folder

Working with PHP to save emails in the sent folder. Facing an issue with storing attachments in the sent folder as only the email message is stored. Output for $attachment: iVBORw0KGgoAAAANSUhEUgAABQAAAAOyCAIAAACjVO/kAAAAAXNSR0IArs4c6QAAAARnQU1BAACx jwv8 ...

What steps can be taken to address the build problem with Angular version 13?

Encountering a problem while working with Angular 13: https://i.sstatic.net/CbAUhh6r.png Attempting to build using ng build --configuration=test, however facing errors as mentioned above. Interestingly, if I remove the reference to bootstrap.min.css in t ...

Fetching the link and heading using beautifulSoup from the <a> elements

I am in the process of creating a script that will extract all the links from the div elements with a class of "pntc-txt." After that, I plan to retrieve the href attribute and the text within the <a> tags. This data will then be inserted into a da ...

Why does Flutter keep sending me back to my default values?

Having encountered an issue with my code, I sought help and received a solution that resolved the initial error. However, a new problem has arisen. I'm expecting a non-null variable but, despite printing its value and confirming it is not null, the sc ...

Are there any portable stylus options available?

Hey there! I'm dealing with a situation where the computers at my school are locked down and I can't install software. Does anyone know if there's a way to compile my Stylus code without having to install Node first? Or perhaps, is there a p ...

Query the MySql database with PHP to locate an HTML hyperlink

I am working with a mysql table which has item_id and link columns where the link column contains both html tags and actual links. I need to search the database for a link that does not contain any html tags. What is the most efficient way to achieve thi ...

What is the method for obtaining distinct hover-over values for individual items within a dropdown menu?

If utilizing angular2-multiselect-drop down, what is the correct method to obtain distinct hover over values for individual items in the drop down? When dealing with standard HTML, you can directly access each element of the drop down list if it's ha ...

Using pixels and percentages to specify the width in HTML dimensions

I am facing an issue with the layout of two tables. The table on the right side has a fixed width of 470px, while the other table should occupy the remaining space in the window. Is there a way to make the width of the first table dynamic so that as the wi ...

Choosing a Radio Button with Python and Selenium

After exploring past inquiries, I have yet to find a solution - as a total beginner in this field, please excuse my lack of knowledge... My goal is to choose the third 'Radio' button on the webpage using the script below: from selenium import we ...

Using images in R Shiny with htmlTemplate: A step-by-step guide

In my current project, I have a file named template.html that I am loading using the code snippet below: https://i.sstatic.net/TipvS.png One issue I'm facing is including an image in this template.html file. Despite having the correct relative path ...

What is the best way to create a video that adjusts to different screen sizes within

I am working on the responsive version of client's website and I stumbled upon this issue - when I make smaller the browser window, the youtube video is not centered - the padding-right: 10px; is ignored - why? How could I fix that? Here is the CSS ...

What is the process for switching between Material Cards produced by a single component?

I need help with swapping material cards using button clicks. Can anyone guide me on how to achieve this? Below is a snippet of code and some screenshots: miner-view.component.html: This component should contain a total of 10 cards <button mat-raised ...

What is the best way to use Google Material-Design-Icons in my project once I have installed it

Once I've installed the material-design-icons package using npm, how can I incorporate them into my React application? The instructions provided here do not mention the npm method. ...

The execution of my JavaScript code does not pause for the completion of an ajax request

I'm currently working on a register validation code in JavaScript. One of the key aspects checked by the validation function is whether the email address provided by the user already exists in the database. To handle this, I'm implementing ajax, ...