display/hide a div within a separate container

Greetings, I am seeking assistance in understanding why the second tab button is not functioning properly. I am attempting to create a layout with a wrapper div, a left side div containing label buttons, and a right side div for displaying content. When I place the show/hide div on the left with the buttons, it works as expected. However, when it is placed in the div on the right, the button does not respond.

#chk:checked~#tab1,
#chk2:checked~#tab2 {
  display: none;
}

div.wrapper {
  width: 100%;
  background: black;
  height: 300px;
}

div.forred {
  width: 80%;
  float: right;
}

div.left {
  width: 20%;
  float left;
}

div#tab1 {
  background: red;
  width: 100%;
  text-align: center;
}

div#tab2 {
  background: red;
  width: 100%;
  text-align: center;
}

div.right {
  width: 80%;
  float: right;
}

input.tabs {
  display: none;
}

label.tab_button {
  float: left;
  padding: 15px 0px;
  font-weight: 600;
  text-align: center;
  color: #FFFFFF;
  border-bottom: 1px inset black;
  background: #30E514;
  width: 100%;
}
<div class="wrapper">
  <div class="left">
    <input type=checkbox id=chk class=tabs>
    <label for=chk class=tab_button>tab 1</label>
    <input type=checkbox id=chk2 class=tabs>
    <label for=chk2 class=tab_button>tab 2</label>
    <div id=tab1>
      CONTENT HERE
    </div>
  </div>
  <div class="right">
    <div id=tab2>
      CONTENT HERE
    </div>
  </div>
</div>

Answer №1

The reason why your second "button" isn't working is due to the following section in your CSS:

#chk:checked ~ #tab1,
#chk2:checked ~ #tab2 { display:none; }

What's happening here is that you are applying a display:none; style to the sibling of #chk:checked, which works for the first "button" because #tab1 is indeed a sibling of #chk:checked. However, #chk2:checked is not a sibling of #tab2. To fix this, move the div#tab2 right after div#tab1 in your HTML so that they are siblings, and then it should work properly.

I've also taken the liberty to adjust your code so that the tabs are initially invisible and only become visible when clicked on tab1 or tab2.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Untitled 1</title>

<style>

    div.wrapper {width: 100%; background: black; height: 300px;}
    div.left {width: 20%; float left;}
    input.tabs { display: none;}
    label.tab_button { 
        float: left; 
        padding: 15px 0px; 
        font-weight: 600;
        text-align: center; 
        color: #FFFFFF; 
        border-bottom: 1px inset black;
        background: #30E514; 
        width: 100%;
    }
    #tab1, #tab2 {background: red; width: 100%; text-align: center;}
    div.right { width: 80%; float: right;}

    #tab1, #tab2 {display:none;}

    #chk:checked ~ #tab1,
    #chk2:checked ~ #tab2 { display:block; }

</style>
</head>
<body>
<div class="wrapper">
    <div class="left">
        <input type="checkbox" id="chk" class="tabs">
        <label for="chk" class="tab_button">tab 1</label>
        <input type="checkbox" id="chk2" class="tabs">
        <label for="chk2" class="tab_button">tab 2</label>
        <div id="tab1">
            TAB 1 HERE
        </div>
        <div id="tab2">
            TAB 2 HERE
        </div>
    </div>
    <div class="right">
        Right div
    </div>
</div>
</body>
</html>

I hope this explanation and solution are helpful to you!

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

MUI: Set the height of the div element to dynamically expand based on its content

I am currently working on styling a React app with MUI and I need help figuring out how to make a div's height adjust to its content. Specifically, I have a Card component that contains a Button. Upon clicking the Button, the content below the Card ge ...

Activate a dropdown menu following the selection of a date on a calendar input using vue.js

What I need to do is have two select fields, one for 'days' and the other for 'hours'. When a day is selected, the user should then be able to choose between two available time slots. If they choose day two, the available time slots sh ...

Tips on displaying a successful message and automatically refreshing the page simultaneously

I am working on a JSP Servlet code that utilizes AJAX to handle user data. In the success function of AJAX, I aim to display a success message to the user and clear all fields in the form by reloading the page. Unfortunately, when the page reloads, the a ...

Leveraging retrieved data for conducting further JavaScript assessments through ajax

I have a mail.php file that is triggered by an ajax script. The purpose of the mail.php file is to conduct server-side validation and, if successful, send an email using mail() along with the data received from the ajax request. Now, I am looking to take ...

Tips for pressing the enter key to submit when faced with two buttons

I am developing a form with two input fields and their respective submit buttons. I want users to be able to enter text into either field, hit the Enter key, and have it trigger the same action as clicking the submit button. Currently, pressing Enter after ...

Say goodbye to my HTML5 creations

I am currently working on an HTML5 grid project that involves implementing a rectangle select tool for use within the grid. Everything is going smoothly, except for the fact that when I attempt to use the rectangular select tool, the grid disappears from t ...

Is there a way to switch between different ag-grid themes using SCSS when loading them?

I have attempted to include the following code in my main.scss file: @import '~ag-grid-community/src/styles/ag-grid'; @import '~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham'; @import '~ag-grid-community/src/st ...

Exploring the Versatility of Axios

Today, I implemented an AJAX request using Axios in JavaScript to submit form data. While the requests are being sent successfully, it seems that the backend is not able to receive the username and password information from the frontend. What could be ca ...

Creating a visually appealing website with Bootstrap 3

Looking to create a unique HTML design using Bootstrap. So far, this is what I've come up with: Plunker Any suggestions or assistance would be greatly appreciated. ...

Trouble Aligning Bootstrap Dropdown Menu Link with Other Navbar Links

Issue with dropdown links not aligning properly in the Bootstrap navbar. The positioning seems off and can be seen in the screenshot provided where the blue outline represents the ".dropdown" https://i.stack.imgur.com/QmG7v.png Facing a similar problem a ...

Creating a NgFor loop in Angular 8 to display a dropdown menu styled using Material

I'm currently facing an issue with incorporating a Materialize dropdown within a dynamically generated table using *ngFor. The dropdown does not display when placed inside the table, however, it works perfectly fine when placed outside. <p>User ...

"Using jQuery to target parent element of a checkbox and apply

Looking to modify the background color of a label based on whether the checkbox inside is checked or unchecked. Please note: Currently, the label's background changes on the first click, but it does not revert back when unchecking the box. <div c ...

Enhancing page accessibility through the implementation of shortcuts

Greetings to all members of the community! I am currently in the process of improving a website by implementing better accessibility practices. I have some concerns regarding a page that displays a large number of repeated result blocks, each containing ...

Adding style using CSS to a dynamically generated table row in Vue.js

Currently, I am working with a table that dynamically generates rows using v-for: <table> <tr><th class='name'>Name</th><th>Surname</th></tr> <tr v-for='data in datas'><td class=&a ...

Interactive search tool with multiple fields using HTML and JavaScript

I need help developing a search box for structured data, where I want to implement two types of typeahead searches: one for fields and another for values within those fields. The image below illustrates what I am aiming for. https://i.sstatic.net/MRsJ2.png ...

What steps can I take to ensure my header appears perfectly aligned? (HTML/CSS)

My header on my website is all messed up with the links appearing incorrectly. I suspect it's an issue with style.css, so I attempted to modify the style.css file but had no luck. I have very limited experience with .css and cannot seem to figure out ...

What could be the reason for my post container not functioning properly?

I am facing an issue with my HTML and CSS code. The container is not displaying as expected, specifically the green bar that should appear below the image. I suspect that the container's auto attribute is causing it to overlook the content inside. Can ...

Is there a way to switch the cursor to a pointer when hovering over a bar on a ChartJS bar graph?

Here's the chart I created: createChart = function (name, contributions, idArray) { globalIdArray = idArray; var ctx = document.getElementById("myChart"); var myChart = new Chart(ctx, { type: "bar", data: { lab ...

How can I showcase the initial page on my local server?

Currently, I am utilizing Xamp for website development. Within my project directory, there is a folder named HTML which contains a page called Cart.html. This HTML folder is located in the htdocs directory. My goal is to have the Cart.html page automatic ...

Help me understand how to display the data in a JSON array

{ "entries": [ { "id": 23931763, "url": "http://www.dailymile.com/entries/23931763", "at": "2013-07-15T21:05:39Z", "message": "I ran 3 miles and walked 2 miles today.", "comments": [], "likes": [], ...