Arrange the parallel table columns within their own individual divs to be perfectly aligned

I have a layout with two divs stacked on top of each other, each containing a table with the same number of columns. I need to ensure that the columns in both tables are the same width and aligned properly. If a column in one table expands, I want the corresponding column in the other table to expand to the same width.

Note: The columns currently do not have a set width. Here is a link to the HTML code for reference.


HTML:

<div id="A">
   <table>
       <tbody>
           <tr>
               <th></th>
               <th>Blah! Blah</th>
               <th>adsdsdadasdasdasdasd</th>
           </tr>
       </tbody>
   </table>
</div>
<br/>
<div id="B">
   <table>
       <tr>
          <th>left head</th>
          <td class="col1"></td>
          <td class="col2">Hello World!</td>
       </tr>
       <tr>
          <th>left head</th>
          <td class="col1">Hello World!</td>
          <td class="col2">dsfsdfgdsgdsgdsfgdsgdsgfdfgdf</td>
       </tr>       
   </table>
</div> 

JQuery:

$(document).ready(function() {
    $('#updatetopdiv').click(function(){        
            //Properly align parallel div's top to bottom 
            var tbl1 = $("#A table tr td");
            var tbl2 = $("#B table tr td");
            tbl1.each(function(i) {
                if (tbl2.eq(i).width() < $(this).width()) {
                    tbl2.eq(i).width($(this).width());
                } else {
                    $(this).width(tbl2.eq(i).width());
                }
            });
     });
});​

Answer №1

When the document is ready, the following function is executed:
$(document).ready(function(){
   var tbl1 = $("#A table tr td");
   var tbl2 = $("#B table tr td");
    tbl1.each(function(i){
        if (tbl2.eq(i).width() < $(this).width()){            
           tbl2.eq(i).width($(this).width());
        } else {
           $(this).width(tbl2.eq(i).width()); 
        }    
    });
});​

This code functions properly, but it waits until the page has fully loaded.

You can also find this code here: http://jsfiddle.net/gwUFb/3/

UPDATE:

Although you mentioned that you've already resolved the issue, here is my original code tweaked to suit your requirements:

$(document).ready(function(){
   var tbl1 = $("#A table tr th");
   var tbl2 = $("#B table tr td,#B table tr th");
    tbl1.each(function(i){
        if (tbl2.eq(i).width() < $(this).width()){            
           tbl2.eq(i).width($(this).width());
        } else {
           $(this).width(tbl2.eq(i).width()); 
        }    
    });
});​

Answer №2

Here is the HTML structure:

<div id="X">
   <table class="static-table">
       <tr>
          <td class="col-one">Greetings!</td>
          <td class="col-two"></td>
       </tr>
   </table>
</div>
<br/>
<div id="Y">
   <table class="static-table">
       <tr>
          <td class="col-one"></td>
          <td class="col-two">Greetings!</td>
       </tr>
   </table>
</div>

And here is the corresponding CSS code:

.static-table {
   table-layout: fixed;
}

.col-one {
    width: 50%;
}

.col-two {
   width: 50%;
}

This solution is static and may encounter alignment issues if a scroll bar appears. To address this dynamically, consider implementing JavaScript.

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

Error: Invalid hook calls detected in React using Material-UI components

Hey there! I'm relatively new to the world of React and I've been tackling an issue with implementing the SimpleBottomNavigation component. Unfortunately, I keep running into an error message that says: "Uncaught Error: Invalid hook call. Ho ...

Is the useNavigate() function failing to work consistently?

Currently facing an issue while working on a MERN Web App. When logging in with an existing account, the backend API call returns user properties and a JWT Token. Upon saving it, I use the navigate function to redirect the User to the homepage. Everything ...

Create a page turning effect in React that simulates scrolling a certain amount at a time

Is there a way to disable default scrolling and have the page automatically scroll to the next item? For example, if I have element2 below element1, how can I set it up so that when I scroll down once, the page scrolls directly to the position of element2 ...

Is AJAX the Solution for Parsing XML: A Mystery?

While trying to parse XML data, I am facing an issue where I can't retrieve the image. Can someone assist me with this problem? Here is my code snippet below or you can view it at http://jsfiddle.net/4DejY/1/: HTML <ul data-role="listview" data-f ...

What's the best way to display a bootstrap modal window popup without redirecting to a new page?

I am having trouble implementing a modal window that will display validation errors to the user when they submit a form. Currently, the window is opening as a new view instead of overlapping the existing form's view. How can I adjust my code so that t ...

Is it possible to capture and generate an AxiosPromise inside a function?

I am looking to make a change in a function that currently returns an AxiosPromise. Here is the existing code: example(){ return api.get(url); } The api.get call returns an object of type AxiosPromise<any>. I would like to modify this function so ...

I am encountering a problem with IE11 where I am unable to enter any text into my

When using Firefox, I can input text in the fields without any issues in the following code. However, this is not the case when using IE11. <li class="gridster-form" aria-labeledby="Gridster Layout Form" alt="Tile Display Input column Input Row ...

Using the Google Identity Services JavaScript SDK in conjunction with Vue and TypeScript: A comprehensive guide

I am currently working on authorizing Google APIs using the new Google Identity Services JavaScript SDK in my Vue / Quasar / TypeScript application. Following the guidelines provided here, I have included the Google 3P Authorization JavaScript Library in ...

Error: The function _this[("render" + data.type)] is not defined as a valid function

https://i.stack.imgur.com/Y5Dz5.jpg I encountered an error in the console that I can't seem to figure out. Despite following the Syncfusion library's documentation, the error persists. Here is the code snippet that I implemented: import React f ...

The MongoDB GridFS is refusing to accept the buffer being written

Hey everyone, I've been working on this issue for nearly a day now and can't seem to figure it out. I'm utilizing multer's inMemory flag to upload an image from my website. My approach involves writing the buffer received from multer to ...

Expanding the <li> elements to create a wide horizontal navigation menu that fits within a 960 grid layout with 12 columns

I'm having trouble making the individual "li" elements inside the "nav" element stretch to fill a 600px space defined by the class "grid_8" in the HTML. I've tried setting width:100%; for the nav, li { width: 100%;} and various other solutions wi ...

Adjust element based on the position of another element (sticky)

Is it possible to rotate a text block so that it remains anchored to the bottom of a red rectangle, even when the rectangle is rotated? Similar to how it works in Figma, but simpler. For example, if I rotate the rectangle by 180 degrees, the text should be ...

Is there a way to position two forms next to each other in alignment?

I would like to display two forms side by side. Currently, I have one form shown here: http://gyazo.com/093efe95337ace40fe633adb88b76c2d. I want the same form to be displayed on the right-hand side as well. .comments-section .comment-form { padding: 20p ...

Adjusting the height of a CSS div to be 0 while also modifying the

I have encountered a unique dilemma <div class="parent"> <p>stuff stuff stuff</p> </div> Using CSS, I set the width and height of parent = 0; because I intend to modify it when a button is clicked. However, for this purpose, I ...

Images are not appearing correctly on Chrome browsers when using Windows operating system

img tags seem to have unusual margins in Chrome, Edge, and Opera browsers, while Firefox displays them correctly. Queries What is causing these margins specifically in Chrome? The Devtool does not detect any margin properties. Is there a straightforward s ...

Having trouble with your Javascript Ajax call?

I've been attempting to make a POST request using Ajax, but I keep encountering an error with status code 0. Strangely, all the request parameters seem to be functioning correctly in the Advanced REST Client. Here's My Code Snippet: <button& ...

Can anyone suggest an effective method for displaying images using JavaScript by specifying a route and filename?

Currently, I have the following code snippet: const route = '/Images/Banner/'; const slides = [ { name: "banner-01", url: `${route}banner-01.png` }, { name: "banner-02", url: `${route}banner-02.pn ...

Execute a function once the router-view in the App.vue component has been refreshed following a route alteration triggered by vue-router

Is there a way to trigger a function after the contents in <router-view/> within App.vue have been updated following a route change? I attempted using the updated() lifecycle hook, but the function is not being executed. Interestingly, the function ...

Exploring the possibilities of toggling between a personalized CSS design and a Bootstrap CSS layout

Is it possible to implement a dropdown menu on my sample-page using javascript/jquery in order to switch between a custom layout and a bootstrap layout? ...

JavaScript function fails to execute when attempting to call it after opening email client with attachment

//deliver the email to the recipient in eml format Response.ClearHeaders(); Response.Clear(); Response.Buffer = true; Response.ContentType = "message/rfc822"; Response.AddHeader("content-length", bin.Length.ToString()); Response.AddHead ...