How can grid be used in CSS/HTML to create two collapsible columns that are side-by-side, maintain equal width, and keep their size when expanded?

Hello all, I'm new here and I have a question that I hope I can explain clearly. I am currently working on creating two collapsible "buttons" positioned side by side (each taking up half of the screen width), which expand when clicked to reveal an equally wide column of text (containing code). I am using CSS grid and HTML for this task, but I am also open to using flexbox if it's more suitable in this scenario. This two-column layout will be repeated multiple times further down the page.

Currently, the buttons are not displaying at the correct width, and when expanded, the content inside each column does not behave as expected (it expands to fill the entire screen width and then wraps).

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
.collapsible {
  background-color: #eee;
  color: green;
  cursor: pointer;
  padding: 18px;
  width: 50%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.collapsible:after {
  content: '\02795';
  font-size: 13px;
  color: white;
  float: right;
  margin-left: 20px;
}

.active:after {
  content: "\2796";
}

.active,
.collapsible:hover {
  background-color: #ccc;
}

.code {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  width: 50%;
  background-color: #f1f1f1;
}

.code-buttons {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-rows: 1;
}

.collapsible[name="css_button"] {
  grid-column: 1;
}

.collapsible[name="html_button"] {
  grid-column: 2;
}
<div class="code-buttons">
  <button type="button" class="collapsible" name="css_button">Media Object CSS</button>
  <pre class="code">
           <code id="media_objects_css">
        <!-- Expanded Column Content -->
           </code>
         </pre>

  <button type="button" class="collapsible" name="html_button">Media Object HTML</button>
  <pre class="code">
          <code id="media_objects_html">
       <!-- Expanded Column Content -->
          </code>
        </pre>
</div>

Answer №1

Your approach is on the right track, but the issue arises from your grid area expecting only two immediate children elements while you have included 4 immediate children (the button and pre tags). To rectify this, I have enclosed each pair of button and pre tag within their own div containers, ensuring that there are only 2 immediate children in your grid:

var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
.collapsible {
  background-color: #eee;
  color: green;
  cursor: pointer;
  padding: 18px;
  width: 50%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

.collapsible:after {
  content: '\02795';
  font-size: 13px;
  color: white;
  float: right;
  margin-left: 20px;
}

.active:after {
  content: "\2796";
}

.active,
.collapsible:hover {
  background-color: #ccc;
}

.code {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  width: 50%;
  background-color: #f1f1f1;
}

.code-buttons {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-rows: 1;
}

.collapsible[name="css_button"] {
  grid-column: 1;
}

.collapsible[name="html_button"] {
  grid-column: 2;
}
<div class="code-buttons">
  <div>
    <button type="button" class="collapsible" name="css_button">Media Object CSS</button>
    <pre class="code">
           <code id="media_objects_css">
        <!-- Expanded Column Content -->
           </code>
         </pre>

  </div>
  <div>
    <button type="button" class="collapsible" name="html_button">Media Object HTML</button>
    <pre class="code">
          <code id="media_objects_html">
       <!-- Expanded Column Content -->
          </code>
        </pre>
  </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

Receiving an unknown result from the bodyParser function

Currently, I am in the early stages of learning nodejs + express + handlebars and facing some challenges with my post request. The issue lies in extracting data for "province and municipality" from req.body.province and req.body.municipality, which is not ...

The CSS form appears to be too large for the page

On every single one of my pages, the content fits perfectly within the body: However, when I have forms on the page, they extend beyond the footer and the body doesn't resize accordingly: Where could the issue be coming from? ...

Generate a new array using a single value extracted from an existing array

I'm new to working with arrays. Before this, I used to manually code everything in HTML. My question is, can you create an array based on the values of another array? var pmmain =["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", ...

Angular JS causing text alignment issues in table cells when viewed on mobile devices

I have created a web application that requires both desktop and mobile views, utilizing Angular JS. Everything is functioning as expected except for the text alignment within tables. I attempted using <td align="left">, but it did not produce any cha ...

Positioning an individual element to the right side of the navigation bar in Bootstrap 5

I've been attempting to shift a single nav-item to the right side of the navbar, but I'm having trouble. My navbar is fairly basic, without a search tool or dropdown menu, as seen below: <nav class="navbar navbar-expand-lg bg-body-tertiar ...

Tips for ensuring Opera browser compatibility with buttons using the CSS background image property

Is there a way to make background images work on buttons in Opera web browser using CSS? I've tried using the background image property, but it doesn't show up when I use Opera. Strangely enough, it works fine in Firefox. However, I have noticed ...

Selenium is throwing an error indicating that a list object does not have the attribute to find an element

In my quest to extract nutritional data from a website through web scraping, everything was going smoothly until I encountered pages with slightly different formatting. When using selenium and a specific line of code, I noticed that it returned an empty l ...

Updating the Wordpress menu: Get rid of the list items and instead add the class ".current-menu-item" directly to the anchor tag

Is there a way to customize the output of wp_nav_menu(); to display a navigation menu like this: <nav> <a>Menu Item</a> <a class="current-menu-item">Menu Item</a> <a>Menu Item</a> <a>Menu Ite ...

Challenge encountered while attempting to implement grid system in ionic framework

I'm completely new to the world of ionic framework, and I really need some assistance in completing this view. Please see the image reference https://i.stack.imgur.com/WOBFw.png I have made an attempt at it with the following code: <div class ...

Produces consistent results despite variations in tag names within the DOM

While iterating over each element (post) in an array, I have assigned each HTML tag name a value of post._id. In the DOM, the outputs have different values as expected. However, when I try to capture these values in the React Profile component, the console ...

Locate an original identifier using Selenium WebDriver

In search of a distinctive identifier for the "update profile picture button" on my Facebook account to utilize it in automation testing with Selenium WebDriver and Java. I attempted driver.findElement(By.className("_156p")).click();, but unfortunately, i ...

Displaying an image on a jsp page

In my current JSP, within the HTML section, I have the following: <select> <%if(size == 1)%> <option>None selected</option> <%if(size > 1)%> <option>1</option> </select> Additionally, I have this ima ...

What is the best way to apply a top padding to a background image using CSS?

I attempted to adjust the top padding using various pixel values in the style, but the image padding relative to the webpage remained unchanged. For example: padding-top: 5px; Here is part of my code snippet: <div class="row pb-5 mt-4" style ...

JavaScript: Retrieving the following element from the parent container

Is there a way to prevent the next element within the parent tag from being enabled or disabled based on a certain condition? HTML: <span> <input type="checkbox" onchange="toggleInput(this)"> </span> <input type="text" ...

Automate tasks without the need for a traditional form, simply using a text box and a

I've exhausted my search efforts on Google, looking for answers to any question relating to my issue. The webpage I am attempting to submit resembles this setup: there is no form present and the objects are not organized within a form structure. While ...

Adjusting the width of a Material UI select/dropdown component

In my material-ui grid setup, each <Grid> contains a <Paper> element to define the grid's boundaries precisely. Inside every <Paper>, there is a custom <DropDown> component (a modified version of Material-UI's Select). I ...

What is the best way to ensure that a div containing lengthy text wraps to the next line as if it were only text overflow?

Is there a way to make a div or span within another div act as text, causing overflow to shift to the next line? I'm unsure of which CSS properties would achieve this effect. I've attempted using overflow-wrap: break-word; word-break: break-al ...

Retrieve the concealed division element's HTML content along with its formatting

Help needed with appending a hidden div with its styles intact. Despite using the code provided below, the appended div does not retain its styles. Any suggestions for an alternative method? var warningMessage = $('#warningDiv').html() fun ...

Is there a way to reposition the arrows in this Bootstrap 5 accordion to appear ahead of the text?

Here's an example from Bootstrap 5 web page that I've been working with (https://getbootstrap.com/docs/5.0/components/accordion/). My query pertains to positioning the arrows before the text in the accordion items. Despite attempting various appr ...

In Bootstrap 4.4.1, there is a known issue where HTML may not be rendered properly after a certain section

There seems to be an issue with the placement of my buttons section in the markup. It does not appear after the DIV containing the select tag, but it does when placed before it. I have tried moving the buttons section around to troubleshoot, and it does s ...