Change the visibility of a div's content when a radio button is selected with only CSS

When the radio buttons are on the same div level as "content1" and "content2", it works properly. But how can I make it work if I move the radio buttons to another div outside of the "second" div?

For example, if toggle1 is checked then content1 should be displayed.

.content1 {
  display: none;
}

.content2 {
  display: none;
}

.toggle1:checked ~ .grid-container .content1 {
  display: block;
}

.toggle2:checked ~ .grid-container .content2 {
  display: block;
}
<div class="level1">
  <div class="level2">
    <input type=radio id="toggle1" name="toggle" class="toggle1">
    <label for="toggle1">toggle1</label>
    <input type=radio id="toggle2" name="toggle" class="toggle2">
    <label for="toggle2">toggle2</label>
    <div>
      <div>
        <div class="second">
          <div class="tab content1">Content1</div>
          <div class="tab content2">Content2</div>
        </div>

Answer №1

When using the general sibling combinator ~, it's important for the two elements to share the same parent element. To implement this in your code, add the "grid-container" class to the <div> that is a sibling of the <input> elements.

.content1,
.content2 {
  display: none;
}

.toggle1:checked ~ .grid-container .content1,
.toggle2:checked ~ .grid-container .content2 {
  display: block;
}
<div class="level1">
  <div class="level2">
    <input type=radio id="toggle1" name="toggle" class="toggle1">
    <label for="toggle1">toggle1</label>
    <input type=radio id="toggle2" name="toggle" class="toggle2">
    <label for="toggle2">toggle2</label>
    <div class="grid-container">
      <div>
        <div class="second">
          <div class="tab content1">Content1</div>
          <div class="tab content2">Content2</div>
        </div>
      </div>
    </div>
  </div>
</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

Guide on attaching an event to every dynamically created element in JavaScript

I'm currently generating 'li' elements dynamically using a loop and running into issues when it comes to assigning events to each generated element. My goal is to assign an onclick event to every li element that is created. Check out the co ...

Utilize Flexbox to arrange elements in a column direction without relying on the position property in CSS

Flexbox - align element to the right using flex-direction: column; without relying on position in CSS Hello everyone, I am looking to move my row element to the right without using positioning. This code includes flex-direction: column; and I do not have ...

CSS - Utilizing Flexbox for creating inline lists that adjust based on text length

I have a set of radio buttons arranged like this: I want to utilize flexbox to display them horizontally when there is sufficient space, similar to this: However, the current setup uses a min-width media query for layout breaking, which can lead to text ...

Retrieving specific data from an SQL database using PHP

My goal is to create a select form with fields populated from an SQL database containing a table of stores. I used a method to retrieve distinct store names and populate the select tool with them. echo '<select name="StoreName" />'."\ ...

Could a javascript loop be created to continuously activate a button with each iteration?

I have just started learning javascript and I am in the process of creating a website where users can change the background by simply clicking on a button. It's working perfectly fine so far, but I want to add another feature where the background imag ...

Navbar collapse not functioning properly on HTML, CSS, and JavaScript

I have developed a CSS code snippet: .nav{ right: 0px; left: 0px; margin-top: 0px; margin-bottom:20px; height: 35px; text-align: center; background-color: red; z-index: 1; } .sticky { background: #000; text-align: center; ...

Styling drop caps using CSS on Wordpress posts

I've successfully implemented a customized dropcap for my Wordpress blog posts using CSS. Is there a way to limit this effect to just the first paragraph, leaving subsequent paragraphs unaffected? Any guidance on achieving this would be highly apprec ...

The function to modify text color in Yii2 using the material bootstrap library is not functioning as intended

My attempt to modify the text color or background of the footer has been unsuccessful after integrating this library. I am unable to alter even a single text color. This is my MaterialAsset: <?php /** * @link http://www.yiiframework.com/ * @copyrigh ...

The :focus pseudo-class is failing to target the input element

I'm dealing with a simple input field of type text: <input matInput type="text" placeholder="{{placeholder}}" class="input-field" [ngClass]="{'error': hasErrors}" [readonly]="isReadonly&quo ...

How to hide text within a contenteditable element

I'm trying to create a contenteditable span that behaves like a password field in a Linux terminal; the text is entered, but not visible. I've experimented with different CSS properties like visibility: hidden and display: none, but they don&apos ...

Verifying the angular form without the need for a submit button

I'm currently utilizing angular.js form validation and I'm looking to implement validation without the use of a form submit button. Below is my HTML code: <form name="userForm"> <input type="hidden" ng-model="StandardID" /> < ...

Inheriting Django templates for a unique CSS class markup approach

I want to create a master.html file for inheritance, but I'm facing an issue where the code is repetitive in three different places except for the body class. Here's my current master.html: <html> <head>...</head> <body& ...

Challenges with implementing TailwindCSS classes in a Next.js application

I've encountered some issues in my nextJS app with utilizing certain TailwindCSS classes such as top, left, or drop-shadow. Even after attempting to update Tailwind from version 1.9.5 to 3.3.3, I have not seen any changes. I believe that I am import ...

The background of an HTML button does not appear in IE8

My buttons are not showing up in IE8. I tried adding background: transparent url('..'), but it didn't help. Here is the code: .wpcf7 input.wpcf7-submit { background: url("/wp-content/themes/ASC/images/<?=ICL_LANGUAGE_CODE;?>_submi ...

Passing references between multiple components

I'm currently working on an application using Material-UI in conjunction with styled-components. I am faced with the challenge of passing a ref down to the root <button> node that is created by Material-UI. Material-UI provides a buttonRef prop ...

The JQuery File-Upload plugin remains inactive even after a file has been chosen

I am currently working on integrating the JQuery File-Upload plugin (). The issue I'm facing is that it doesn't respond when a file is selected. Here are some potential problems to consider: No errors appear in the Chrome console. Selecting a ...

The impact is felt across every button in CSS

Why is this happening? When I hover over a button, all buttons apply the effect, which should not be the case. I only want the effect to be applied to the button that I am hovering over. .buttons{ font-family: arial; text-decoration: none; padding ...

I'm wondering why my second div is displaying a different size compared to the rest, even though they all share the same container-fluid class

Within the HTML snippet provided, utilizing Bootstrap, all three child divs possess the container-fluid class. However, why does the 2nd div differ from the others? Refer to this example Div Example. Div1 and div4 exhibit the same characteristics, as do d ...

Guide to resetting a CSS animation with pure JavaScript

My flexbox contains 4 images, each triggering an animation on hover. However, the animation stops if I hover over the same image again. I have tried various JavaScript solutions found online to restart the animation, but none seem to be working for me. S ...

Put the code inside a function. I'm new to this

My goal is to encapsulate this code: if($(window).width() > 980) { $(window).on("scroll", function() { if($(window).scrollTop() > 20) { //add black background $(".x-navbar").addClass("active"); $(".x-navbar .desktop ...