Matching up columns across tables

Suppose there are two tables with text in between:

<table>
    <tr><td>A</td><td>0000</td><td>Some text goes here...</td></tr>
    <tr><td>B</td><td>11</td><td>... and here</td></tr>
</table>

Some text goes here.

<table>
    <tr><td>CCC</td><td>1</td><td>Some text goes here...</td></tr>
    <tr><td>DDD</td><td>0</td><td>... and here</td></tr>
</table>

How can I ensure the two tables have equal column width? For example, make the first column of table 1 three characters wide (e.g. CCC), and the second column of table 2 four characters wide (e.g. 0000).

I don't want to predefine column widths in a CSS file since I'm unsure of what they should be. Instead, Iā€™m looking for a dynamic solution using JavaScript.

The steps involved would include:

  1. Constructing the two tables;
  2. Determining the maximum width of columns 1 and 2;
  3. Adjusting the CSS properties of the tables based on these values.

Answer ā„–1

  1. (assign identifiers to your table elements)
  2. To retrieve all the width values of your tds in a table, you can utilize jQuery selectors as shown below:

     var maxWidth = 0;
         $("#table1 td").each(function() {
          var width = $(this).css("width");
          if(width > maxWidth) maxWidth = width;
         });
    
  3. Afterwards,

     $("#table1 td").css("width", width);
    

Answer ā„–2

To achieve the desired outcome, you can utilize JQuery in the following manner:

 $(document).ready(function() {
     var maxWidth = 0;
     $('td').each(function() {
        var width = $(this).width();
        if(width > maxWidth) {
            maxWidth = width;
        }
     });

     $('td').css("width", maxWidth);
 });

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

Issue with Tailwind CSS: The 'overflow-y' property does not scroll all the way to the bottom when using 'top-[63px]'

I'm facing an issue with my Tailwind CSS code that is hindering me from scrolling to the bottom of an aside element. <body> <div> <nav class=" bg-white px-2 py-2.5 dark:bg-gray-800 sm:px-4 fixed w-full z-20 border-b borde ...

Working with MySQL in Node.js using async/await

Struggling with utilizing async/await in Node.js with MySQL as it consistently returns an undefined value. Can someone shed light on what could be causing this issue? See my code snippet below. const mysql = require('promise-mysql'); var co ...

Prevent the appearance of a scrollbar on a fixed-position popup element

Whenever I click on a button, a Sidebar opens up and fills the entire screen due to its position being set to fixed. Here's how it looks with TailwindCSS: return ( <div className="fixed z-40 flex left-0 top-0 h-screen w-screen"> ...

Troubleshooting a problem with a CSS dropdown menu

For the past couple of hours, I've been trying to troubleshoot the issue with the fly-out menu on the homepage of this website. When hovering over the capabilities link, the fly-out works correctly but the main background doesn't stretch to fit ...

Why is Puppeteer failing to download to the designated folder using "Page.setDownloadBehavior" in Windows?

When trying to download a file using Puppeteer, I found that the code works perfectly on a Mac OS machine but fails on a Windows machine. The code snippet I used is shown below: await page._client.send( 'Page.setDownloadBehavior', { beha ...

Data merging in Firebase 9 and Vue 3 is not functioning properly

I am facing an issue with merging data in my firebase database. I consulted the documentation at https://firebase.google.com/docs/firestore/manage-data/add-data for guidance. After attempting to merge data using setDoc, I encountered an error (Uncaught Ty ...

Creating a webpage with a form is a simple process that involves utilizing both PHP and HTML

Just like creating a post in WordPress or asking a question on this website, I am curious about how to create a page simply by filling out a form. I understand how to build an HTML form, manage data in a database, and have some knowledge of PHP. However, ...

Experience the magic of a customized cursor that disappears with a simple mouse movement in your website,

I have been experimenting with designing a custom cursor for my website. After finding a design I liked, I made some adjustments to suit my needs. However, an issue I encountered is that when I scroll, the custom cursor also moves away from its original po ...

Ways to align a div in relation to a span element?

I have a large block of text within a div. I am seeking a way to create a hover effect on specific words within the text, which will then display a div below the word containing a customized div. This functionality resembles the feature on Wikipedia where ...

Tips for selecting an element within a span by clicking on it?

Is there a way to click on an element that is contained within a span tag? I have attached a screenshot for reference. https://i.sstatic.net/FkhDB.jpg I would greatly appreciate any help on how to successfully click on that specific element. ...

Position the image in a responsive manner relative to the text

I've successfully centered my h1 in a div using the following CSS properties: text-align: center; line-height: 150px; vertical-align: top; Now, I'm facing a challenge where I want to position an image in the top-left corner of the text. I want ...

Morphing BufferGeometry in Three.js

Can two buffer geometries be morphed in three.js? Are there any helpful examples to explore for reference? I am particularly keen on learning about manual morphing using morph target influences. ...

Form Validation Using a Separate Function

Currently, I am utilizing the jQuery Validation plugin from http://jqueryvalidation.org/ to validate my forms. By default, the validation process is triggered when the form is submitted. However, I would like to initiate the validation when a specific func ...

Vue JS encounters difficulties when attempting to lazy load various images from a randomized image URL

I've implemented a code snippet for lazy loading images with different URLs: <img src="https://source.unsplash.com/1600x900/?hotel,booking.com?v=1" loading="lazy" /> <img src="https://source.unsplash.com/1600x900/?hot ...

Issue encountered when calling theme.breakpoints.down('') function from Material UI

As a novice, I have ventured into using material UI on the front-end of my project. My aim is to achieve responsiveness by leveraging theme.breakpoints.down as indicated in the material UI documentation. However, when attempting to implement this, I encoun ...

Tips for successfully including a forward slash in a URL query string

My query involves passing a URL in the following format: URL = canada/ontario/shop6 However, when I access this parameter from the query string, it only displays "canada" and discards the rest of the data after the first forward slash. Is there a way to ...

Modify the class and color of a Font Awesome icon when clicked

My goal is to change the class and color of an icon when it is clicked. Here is the code I am using: $(".sim-row-edit-icon").click(function(){ alert("Icon clicked"); // on click please change icon and color to red }); <link href="https://stackpath. ...

Tips for Minimizing DIV Space with the Flex Property

I need some help with reducing the space between the individual content (1, 2, 3, 4) and its border in this Fiddle. As a newcomer to Flexbox, I'm unsure if there's an easy solution to this issue. Here is a visual representation of what I mean: ...

Tips for eliminating flickering when updating arrays in angular

Attempting to maintain real-time updates on an Angular page by polling a REST service hosted locally and updating the array with new content is crucial. The code snippet below demonstrates this: JS angular.module("WIMT").controller('overviewControll ...

Using Angular JS to Implement Multi-filtering for Lists

Having trouble with the filter function in Angular JS when trying to filter a list using different links. I've been struggling to get it right even after trying some examples. Can anyone provide guidance on how to correctly filter the list? HTML < ...