Steps for building a 2x2 grid of tables in HTML

I am struggling to design a 2x2 grid of collapsible tables in HTML. I have been experimenting with different solutions, but I haven't found one that works seamlessly. When I collapse the top left table, it messes up the alignment of the bottom row tables.

<!DOCTYPE html>
<html>
  <head>
    <title>2x2 Table Grid</title>

    <style>
      table, th, td
      {
      border-collapse:collapse;
      border:1px solid black;
      width=50%
      }
    </style>


    <script language="JavaScript" type="text/javascript">
    function setDivStyle(table, style) {
            var tbl = document.getElementById(table);
            tbl.style.display = style;
        console.log(table + " display style set to " + style)
          }
    </script> 

  </head>

  <body>

    <div id="r1">
      <h2>Row 1</h2>

      <div id="r1-controls">
    <a id="r1c1-hide" href="javascript:setDivStyle('r1c1', 'none')">Hide R1C1</a> 
    &nbsp;
    <a id="r1c2-show" href="javascript:setDivStyle('r1c1', 'inline')">Expand R1C1</a>
    &nbsp;
    <a id="r1c2-hide" href="javascript:setDivStyle('r1c2', 'none')">Hide R1C2</a> 
    &nbsp;
    <a id="r1c2-show" href="javascript:setDivStyle('r1c2', 'inline')">Expand R1C2</a>
    <br>
      </div>

      <table id="r1c1" style="display: inline; float: left">
    <tr>
      <th>R1C1a</th>
      <th>R1C1b</th>
      <th>R1C1c</th>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
      </table>

      <table id="r1c2" style="display: block; float: none">
    <tr>
      <th>R1C2a</th>
      <th>R1C2b</th>
      <th>R1C1c</th>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
      </table>
    </div>

    <div id="r2">
      <h2>Row 2</h2>

      <div id="r2-controls">
    <a id="r2c1-hide" href="javascript:setDivStyle('r2c1', 'none')">Hide R2C1</a> 
    &nbsp;
    <a id="r2c1-show" href="javascript:setDivStyle('r2c1', 'inline')">Expand R2C1</a>
    &nbsp;
    <a id="r2c2-hide" href="javascript:setDivStyle('r2c2', 'none')">Hide R2C2</a> 
    &nbsp;
    <a id="r2c2-show" href="javascript:setDivStyle('r2c2', 'inline')">Expand R2C2</a>
    <br>
      </div>

      <table id="r2c1" style="display: inline; float: left">
    <tr>
      <th>R2C1a</th>
      <th>R2C1b</th>
      <th>R2C1c</th>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
      </table>

      <table id="r2c2" style="display: block; float: none">
    <tr>
      <th>R2C2a</th>
      <th>R2C2b</th>
      <th>R2C1c</th>
    </tr>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
      </table>
    </div>


  </body>
</html>

Answer №1

Encountered a similar issue in the past. The culprit is indeed the use of float:left, which causes Row 2 to shift upwards when R1C2 is hidden. To address this, it's advisable to establish a new function that toggles the float:left property specifically for such instances:

function adjustFloat(elementId, style) {
    var selectedElement = document.getElementById(elementId);
    selectedElement.style.float = style;
    console.log(elementId + " now floats with style: " + style)
}

To implement this solution, simply call:

javascript:adjustDisplay('r1c2', 'none'); adjustFloat('r1c1', 'none');

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

How can I use console.log to display a message when a variable reaches a specific value?

As a beginner coder, I am struggling to find the solution to this coding issue. Here is the code snippet that I have attempted: var X = "Angry"; if X = "Angry" console.log(X is Angry) After running this code, I encountered an error message specifically p ...

How to format numbers in JavaScript post calculations

Struggling to find a solution to format calculation results with commas for thousand separators (e.g., 10,000). After implementing the .toLocaleString('en-US', {maximumFractionDigits:1}); method to format numbers in the output, I encountered unex ...

Monitoring a location within a grid using AngularJS

I have a question: How can I track the position within a 10x10 grid in AngularJS and update it using arrow keys? Initially, I considered implementing this functionality in a directive with the following code: angular.module('mymodule').directiv ...

Encountering a 404 error with Next.js 13 dynamic routing

Whenever I click on an item, it redirects to the dynamic route "http://localhost:3000/products/{id}" but instead of displaying the data, I get an error. Here is my file structure: app components Product.js pages Products [id].js layou ...

Need a clearfix solution for Internet Explorer 6/7?

While developing a website, I have come across the micro clearfix hack from here. It's working well, but I do have a question regarding it that I'd like some clarification on. Within the clearfix hack on the webpage, there is the following code ...

Javascript Error - Issue with Fuse.js: e.split() function is not recognized

Scenario - I am in need of implementing a fuzzy search feature, and therefore utilizing fuse.js for this purpose. I obtained the code snippet from fuzzy.min.js at https://github.com/krisk/Fuse/blob/master/src/fuse.min.js Challenge - Despite using the cod ...

Accessing information from a database table using Highchart and Ruby on Rails (ROR

I have a Ruby on Rails application that utilizes highcharts. I am currently working on enhancing it to display the amount of time spent on specific projects. To demonstrate what I am trying to achieve, I have created a JSFiddle example which can be found h ...

Create a Bootstrap multi-bar component for visually displaying the daytime information

I am looking to create a horizontal bar with multiple intervals. Here is what I have tried so far: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"& ...

Major Technical Issues Plague School-wide Celebration

In my JavaScript code, I am creating a 16x16 grid of divs. Each div should change its background color from black to white when the mouse enters (inherited based on a common class). However, I am facing an issue where all the divs change color simultaneou ...

Sorting an array of strings that contain years in JavaScript

I am attempting to organize an array of seasons based on season and year, all while eliminating any duplicate seasons. Merely sorting the array did not produce the desired outcome, so I believe utilizing a regex might solve the issue. const myStringArra ...

Use JavaScript to submit a form in Django containing the <a></a> tag

I am having an issue with my form submission using JavaScript. When I try to submit the form, it is sending a GET request to the backend, but I am expecting it to send a POST request instead. Here is the HTML file code snippet: <form id="my_form&qu ...

Exploring the features of AngularJS ui-router: ui-sref functionality and utilizing custom

My HTML looks like this: <span ui-sref="{{detailsArt(art.Id)}}" check-val></span> Within my directive check-val, I have the following code: link: function(scp, el, attr) { el.bind('click', function(event) { //some logic with ...

What is the reason for the discrepancy in displaying between these two arrays?

I am facing an issue where I have an array called order containing objects and another array called test that contains a string. With jQuery, I am able to output the contents of test to a <div>, but strangely I cannot do the same for the order array. ...

Am I using async/await correctly in this code?

Here is the code snippet I am currently working with: var process = async (items: DCXComposite[], session: Session) => { // This function returns a response object with a statusCode property. If the status Code is 200, it indicates a ...

Alternative Options for Playing Audio in Internet Explorer 8

I'm in the process of setting up a button that can both play and pause audio with a simple click. While this functionality is working smoothly in modern browsers like Google Chrome, I am facing compatibility issues with IE 8. Even after attempting to ...

Storing items in a pre-save hook using Mongoose

I have files that I need to generate, but these files include links, so the structure of the file looks like this: const file = { name: 'some name', content: 'some content, type: 'file type', links: [{ path: 'some ...

Utilizing ASP.NET with JavaScript to target elements by ID within an iframe

Struggling to populate my form within an iframe by inputting a value in a textbox and selecting an option from a dropdown menu. webform1 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title ...

The grid layout is being disrupted by a concealed input

As I work on styling my admin panels with Twitter Bootstrap, I've encountered a strange issue. Upon testing in Chrome 28 and Firefox, adding a simple hidden input seems to disrupt the grid layout. If the hidden input is moved into div.span6 or remove ...

Calculate the total rows within a specified date range

Within my database, there is a column named IsStaff, which returns a value as a bit. This signifies whether a staff member in a company has an illness (1) or not (0). How can I construct an SQL query to count the occurrences of both 1's and 0's w ...

How can you add a border before and after text as well as around the entire box

Is there a way to add borders to the image below? I would like to include a border that looks similar to this: text added before and after the entire box How can I achieve a border using css either with before, after, or background? ...