Styling elements with CSS and using it to toggle the visibility of divs based on radio

When the radio button #my102 is selected, the div #navi102 should be visible and all other #navi??? divs should be hidden. Similarly, if the radio button #my7 is selected, the div #navi7 should be visible and others hidden.

What would be the best approach to achieve this?

CSS snippet:

.list {
}

.opt ~ .list {
  display: none;
}

.opt:checked ~ .list {
  display: block;
}

input[type="radio"]#my1:checked + div #navi1 { display: block; }
input[type="radio"]#my2:checked + div #navi2 { display: block; }
input[type="radio"]#my3:checked + div #navi3 { display: block; }
input[type="radio"]#my4:checked + div #navi4 { display: block; }
input[type="radio"]#my5:checked + div #navi5 { display: block; }
input[type="radio"]#my6:checked + div #navi6 { display: block; }
input[type="radio"]#my7:checked + div #navi7 { display: block; }
input[type="radio"]#my99:checked + div #navi99 { display: block; }
input[type="radio"]#my100:checked + div #navi100 { display: block; }
input[type="radio"]#my101:checked + div #navi101 { display: block; }
input[type="radio"]#my102:checked + div #navi102 { display: block; }

HTML code excerpt:

<div id="chart" style="white-space: nowrap; position: relative; text-align: center;">
  <div style="display: inline-block">
    <!--
    --><input name="case" type="radio" id="my102" class="opt" /><div class="list" style="float:left;width:70px">&nbsp;
    <div id="navi102">
      <div style="z-index:100;position:fixed;right:0;top:50px">101</div>
    </div>
    </div><div style="display: inline-block;vertical-align:top;width:70px">extra
    </div><!-- ... continue with HTML code displayed in a similar manner ... </div>
</div>

Answer №1

  1. There seems to be some errors in your HTML code, such as a missing opening tag for the </span>.
  2. The page appears broken with excessive width and odd empty spaces.

Perhaps you would like a simpler explanation. This is why I will provide a basic example instead of analyzing your entire complex code, which can be difficult to comprehend and troubleshoot.

#content1
{
    display:none;
}

#content2
{
    display:none;
}

#toggle1:checked ~ #content1
{
  display: block;
}

#toggle2:checked ~ #content2
{
  display: block;
}
<input type=radio id="toggle1" name="toggle">Toggle1
<input type=radio id="toggle2" name="toggle">Toggle2

<br><br>

<span id="content1">Content1</span>
<span id="content2">Content2</span>

Let's focus on the HTML structure first. There is a crucial detail to note - both the input and span elements share the same parent in the document tree. Why is this important? It relates to the general sibling combinator in CSS.

Next, let's delve into the CSS portion.

To begin, I establish the initial state where #content1 and #content2 are hidden, meeting your requirement. (Although I could simply write

#content1, #content2 { display:none; }
, the exact method isn't critical.)

Subsequently, I implement the functionality you desire using element IDs only. You were previously using the + symbol in CSS, whereas I opted for the ~ symbol. In my view, utilizing element IDs is preferable over the + symbol.

The ~ combinator separates two selectors and matches the second element if it follows the first and they have a common parent.

Quoted from developer.mozilla.org

Learn more at w3.org – general sibling combinators

Answer №2

When faced with a complex code that's difficult to troubleshoot, creating a simplified version can make all the difference:

Check out this minimal version of the code

Below is the HTML snippet:

<div>
  <input type="checkbox" id="ch1" />
  <div>
    <div id="d1">first</div>
  </div>
  <input type="checkbox" id="ch2" />
  <div>
    <div id="d2">second</div>
  </div>
</div>

And here is the CSS snippet:

input#ch1:checked + div #d1 {
  border: 1px solid red;
}

input#ch2:checked + div #d2 {
  border: 1px solid blue;
}

Answer №3

Here's a unique solution I came up with.

CSS:

.list {
}

.opt ~ .list {
  display: none;
}

.opt:checked ~ .list {
  display: block;
}

input[type="radio"]#my1:checked + div #navi1 { display: block; }
input[type="radio"]#my2:checked + div #navi2 { display: block; }
input[type="radio"]#my3:checked + div #navi3 { display: block; }
input[type="radio"]#my4:checked + div #navi4 { display: block; }
input[type="radio"]#my5:checked + div #navi5 { display: block; }
input[type="radio"]#my6:checked + div #navi6 { display: block; }
input[type="radio"]#my7:checked + div #navi7 { display: block; }
input[type="radio"]#my99:checked + div #navi99 { display: block; }
input[type="radio"]#my100:checked + div #navi100 { display: block; }
input[type="radio"]#my101:checked + div #navi101 { display: block; }
input[type="radio"]#my102:checked + div #navi102 { display: block; }

HTML:

<div id="chart" style="white-space: nowrap; position: relative; text-align: center;">
  <div style="display: inline-block">
    <!--
    --><DIV STYLE="DISPLAY:INLINE-BLOCK"><input name="case" type="radio" id="my102" class="opt" /><div class="list" style="float:left;width:0">
    <div id="navi102">
      <div style="z-index:100;position:fixed;right:0;top:50px">101</div>
    </div>
    </div></DIV><div style="display: inline-block;vertical-align:top;width:70px">extra
    </div><!--
    --><DIV STYLE="DISPLAY:INLINE-BLOCK"><input name="case" type="radio" id="my101" class="opt" /><div class="list" style="float:left;width:0">
    <div id="navi101">
      <div style="z-index:100;position:fixed;left:0;top:100px">102</div>
      <div style="z-index:100;position:fixed;right:0;top:100px">7</div>
    </div>
    </div></DIV><div style="..."
    ...
    ...(remaining HTML code to be displayed)
    ...
    ...
    ... 
  </div>
</div>

Answer №4

Presenting my proposal, an enhanced version of Sylogista's response.

  • HTML :
<div name="tab-container">
    <!-- tabs -->
    <input type="radio" name="tab" id="tab1" label="Tab 1">
    <input type="radio" name="tab" id="tab2" label="Tab 2">

    <!-- tabs content containers -->
    <div id="content1" class="tab-content"></div>
    <div id="content2" class="tab-content"></div>
</div>
  • CSS :
/* display contents */
#tab1:checked ~ #content1,
#tab2:checked ~ #content2 {
  display: block;
}

/* hide contents */
#tab1:checked ~ .tab-content:not(#content1),
#tab2:checked ~ .tab-content:not(#content2) {
  display: none;
}

The key improvement here is that the css definitions for hiding and showing tab contents are consolidated into one concise set.

The showing mechanism remains largely unchanged while the hiding functionality utilizes a more sophisticated combination of selectors for increased efficiency.

This advanced selection process operates as follows: using the .tab-content class to target all tab contents, while excluding the displayed tab content element by utilizing :not(#id), preventing it from being hidden according to the hiding rule.

Alternatively, you can simplify the hiding of contents by duplicating and adjusting the rules used for displaying them, like so:

/* hide contents */
#tab1 ~ #content1,
#tab2 ~ #content2 {
  display: none;
}

Changes include altering the display property value and eliminating the need for the :checked selector on the tabs' IDs.

This approach ensures that the visibility states of all tab contents are directly linked to the checked state of their associated tab.

If you wish to experiment with this concept, feel free to explore this codepen which offers additional explanations.

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

Opening a dynamically generated HTML file in a separate window in Flutter web

I am facing an issue with the url_launcher package while attempting to open a link using the following code snippet: try { final Uri launchUri = Uri.dataFromString( '<html><body>hello world</body></html>', ...

Unable to run a Bash script using PHP

I'm currently facing a challenge while attempting to run a simple Bash Script from within a PHP script. The process involves collecting data from an interactive HTML5 front-end page, transmitting it through ajax to the PHP script, parsing the variable ...

React and Material UI: troubleshooting problems with layout columns

I'm working on a project with three columns and I want to include a column for removing each row. Is it possible to add a "removing" column on the right? If so, how can I go about doing it? VIEW CODESANDBOX: HERE const CustomTableRow = ({ row, index ...

binding swipe action to click event

I am utilizing images from a database and implementing next and previous buttons for pagination. What I aim to do is connect the click events of these buttons to swipe actions, so that swiping from right to left triggers the next link, while swiping from l ...

How can I use AJAX to remove a record from a MySQL table?

I am looking to create a button with an image X that, when clicked, will delete a row from the database at that exact moment. teste-checkbox.php <style> table { font-family: verdana; font-size: 12px; } table th { text-align: left; backgrou ...

What could be causing the lack of functionality in my nested CSS transition?

My markup includes two classes, fade-item and fade. The fade class is nested within the fade-item class as shown below: <a class="product-item fade-item" (mousemove)="hoverOn(i)" (mouseleave)="hoverOff(i) > <div class= ...

Ways to retrieve the path of a button found within table cells

https://i.stack.imgur.com/pUYHZ.jpgI'm currently working on a table where I've created a button that's being used in various rows and tables based on certain conditions. There's a specific scenario where I need to display the button for ...

JavaScript code for transitioning between a thumbnail and a full-width image header on a webpage

I am looking to create transitions in NextJs that involve a smooth shift from a thumbnail image to a full-screen header when clicking on a project from the home page. I prefer utilizing internal routing rather than React Router, but I am open to using that ...

What is the best way to ensure the logo is centered when the screen size reaches 750?

Looking to center the logo on a media screen size breakpoint? View the photo (Here): https://i.stack.imgur.com/UnB2F.png Check out my code below: .logo img { padding: 15px; width: 125px; } <nav> <ul class='nav-bar'> < ...

Text that only occupies a portion of the screen width

My unordered list (ul) contains three list items (li). The first li element displays the text "opptakskrav" and the last li element displays "ja". Can anyone explain why the text in my second li element does not use the full width and starts a new line hal ...

Ensure that the table is padded while keeping the cells collapsed

I am currently working on creating a table with borders between each row. However, I am facing an issue where the row borders are touching the outer border of the table. Despite my efforts, I have been unable to find a solution to this problem. I feel like ...

Tips for adjusting the button spacing on a bootstrap navbar

As someone who doesn't work on front-end tasks frequently, I have encountered an issue while creating a navbar using Bootstrap. The problem arises when trying to add multiple buttons as it causes the spacing between them to appear strange. Does anyon ...

What is the method for configuring automatic text color in CKEditor?

https://i.sstatic.net/yEM3p.png Is there a way to change the default text color of CKEditor from #333333 to #000000? I have attempted to modify the contents.css in the plugin folder: body { /* Font */ font-family: sans-serif, Arial, Verdana, "Tr ...

Enhancing URLs with jQuery/AJAX: A Comprehensive Guide to Adding Parameters

Whenever I try to use the get method on an HTML form, the submitted data automatically appears in the URL. You can see what I mean here. Interestingly, when attempting to achieve the same result with JQuery and AJAX using the get method, the data doesn&apo ...

Unexpected Error: Null value prevents accessing 'getElementsByClassName' property in HTML, along with Troubleshooting Inactive Button Behavior in HTML

Can someone identify the error in my HTML code? I keep getting an "Uncaught TypeError: Cannot read property 'getElementsByClassName' of null" error on the second line of the script tag. Additionally, my implemented active header code is not funct ...

switching the color of a path in an SVG when hovering over a

In the midst of working on SVG, I'm trying to implement a color change effect upon hover. The desired functionality is such that when hovering over the .business div, the color of the SVG business path should also change accordingly as specified in th ...

Enhancing an element with a modifier in CSS using BEM conventions without the need for @extend

Trying to grasp the best method for writing BEM modifier rules within parent classes. I've utilized SASS's @extend but question if this is the right approach. For example: If there is a class called .form structured like this: <div className= ...

Choosing more than one item to submit may pose an issue - the POST request will be empty

One of the challenges I'm facing is with a multiple select feature like the one below: This allows for selecting various options, such as 3 sports in this case. Successful form submission occurs when items are selected correctly. However, if no spor ...

I am having trouble aligning the element perfectly in the center

I'm working on a radio input design featuring a circle in the middle, achieved via the pseudo element ::before However, I've noticed that when the input size is an odd number, the centering isn't quite perfect This issue seems more promine ...

"Eliminating the visual feedback frame from your Samsung Galaxy browser: A step-by-step

After clicking a link, a dark orange frame is displayed around it. This frame can sometimes persist until the next scroll or screen touch. Similarly, when other elements are clicked, an orange frame may appear, adjusting in size to reflect the element cli ...