Make sure the widths of the two table columns are perfectly aligned

I have two tables, one on top of the other, and I want to align their column widths exactly with each other. Is there a way to do this? I've tried fixed table column widths but haven't had any success.

You can see in the fiddle that the columns are slightly off from each other: http://jsfiddle.net/askhe/

HTML

<table class="tblresults txtblack">
                            <tr class="tblresultshdr bold">
                                <td class="col1">Company</td>
                                <td>Currency</td>
                                <td>Bid</td>
                                <td>Ask</td>
                                <td>YTD Vol</td>
                            </tr>
                            <tr>
                                <td class="col1">ABC</td>
                                <td>GBP</td>
                                <td>94</td>
                                <td>16</td>
                                <td>3,567,900</td>
                            </tr>
                            <tr>
                                <td class="col1">DEF</td>
                                <td>GBP</td>
                                <td>3</td>
                                <td>46</td>
                                <td>10,000</td>
                            </tr>
                            <tr>
                                <td class="col1">GHI</td>
                                <td>GBP</td>
                                <td>3</td>
                                <td>46</td>
                                <td>10,000</td>
                            </tr>

</table>
                        <table class="tblresults txtblack margintop10">
                            <tr>
                                <td colspan="5" class="bold" >Investments</td>
                            </tr>
                            <tr>
                                <td class="col1">ghjk</td>
                                <td>GBP</td>
                                <td>13</td>
                                <td>6</td>
                                <td>130,000</td>
                            </tr>
                            <tr>
                                <td class="col1">asdsa</td>
                                <td>GBP</td>
                                <td>120</td>
                                <td>46</td>
                                <td>16,000</td>
                            </tr>
                            <tr>
                                <td class="col1">dfdsfsdf </td>
                                <td>GBP</td>
                                <td>1</td>
                                <td>4</td>
                                <td>13,000</td>
                            </tr>
                       </table>​

CSS

table.tblresults {
    width:100%;
    *width:99.5%;
    border: 1px solid #b9b8b8;
    top: 0;
}
table.tblresults tr.tblresultshdr {background: lightgrey;}
table.tblresults tr.tblresultshdr td {padding: 6px;}
table.tblresults td {padding: 8px; border: 1px solid #b9b8b8;}
table.tblresults td.col1 {width: 70%;}
​

Answer №1

table elements were originally designed for scientific data, such as findings from experiments, rather than for controlling layout:

It's important to note that tables should not be solely used for formatting document content as this can create issues when displayed on non-visual platforms. Additionally, if tables are combined with images, users may need to scroll horizontally to view the content, especially on smaller screens. To address these concerns, it is recommended for authors to utilize style sheets for layout control instead of relying on tables.

Even if you're not utilizing tables for layout purposes, your issue could still stem from rendering and layout challenges. One simple fix would be to merge both tables into one (jsfiddle).

If you prefer organizing your data in multiple smaller tables rather than a single large table, make sure to specify column widths for better alignment.

Answer №4

While I do acknowledge that merging tables is usually the best and simplest solution, there are instances where having two separate tables with identical columns is necessary (one fixed and one scrollable).

To achieve this, I created two tables with the same number of columns, one with width rules using percentages and pixels, and one without.

Then, using JavaScript, I assigned the width of the ruled table to the free table:

document.getElementById("HeaderTable").style.width = document.getElementById("main").clientWidth ;
document.getElementById("tar1").style.width = document.getElementById("org1").clientWidth ;
document.getElementById("tar2").style.width = document.getElementById("org2").clientWidth ;
document.getElementById("tar3").style.width = document.getElementById("org3").clientWidth ;
document.getElementById("tar4").style.width = document.getElementById("org4").clientWidth ;

The first line fixes the table width, followed by setting each column's width individually. Using .clientWidth is crucial as .style.width may return a percentage if applied on the ruled column.

Initially, this method almost worked but had slight shifting in layout. To address this in Internet Explorer, I adjusted my code to use fixed values:

document.getElementById("tar1").style.width = document.getElementById("org1").clientWidth - 9;
document.getElementById("tar2").style.width = document.getElementById("org2").clientWidth ;
document.getElementById("tar3").style.width = document.getElementById("org3").clientWidth - 10;
document.getElementById("tar4").style.width = document.getElementById("org4").clientWidth - 10;

These value adjustments could vary based on the specific table. Surprisingly, this solution worked seamlessly across major browsers regardless of zoom level.

However, resizing windows caused alignment issues, necessitating the need to bind the function to window resize events. Additionally, this solution may not be effective for extremely large tables.

You can view a demonstration on jsfiddle, which includes CSS styles. At present, it does not work when zoomed in because my floating header fails to stick to right: 0px; the reason for this issue is currently unknown.

Answer №5

By utilizing this straightforward HTML snippet, you can achieve your goal. I personally used this method to generate PDFs from HTML successfully. Hopefully, it proves beneficial for someone else as well.

<table border=0>
  <tr>
    <td>
      <!--Insert table 1 -->
    </td>
    <td>
      <!--Insert table 2 -->
    </td>
  </tr>
</table>

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

Update the table contents smoothly using the html helper PagedListPager without having to reload the entire

I am struggling with a specific line of code that utilizes the html helper's PagedListPager: @Html.PagedListPager(Model.kyc_paged_list, page => Url.Action("ClientDetails", new { id = ViewBag.ID, kyc_page = page, transaction_page = Model. ...

Having trouble getting the Facebook like button to display on my website using an iframe in the markup

I gave it my all to try and get it to work, but unfortunately, I was unsuccessful. This is the approach I took. First, I followed the instructions provided on https://developers.facebook.com/docs/plugins/like-button. Next, I copied and pasted the iframe ...

Extract certain text from an element using a straightforward script

Is there a way to simplify this JavaScript code for splitting specific text from an element? Here is an example of the HTML element: <div id="ReviewBox"> <span>(By:) Hello</span> <div id="ReviewBox"> <span>By: Goodbye</ ...

Inserting multimedia (URL or picture) into Bootstrap 4 tooltip

I have implemented Bootstrap 4 in my project. Below is the code snippet I am using: <a class="btn popovers" href="#" data-toggle="popover" data-placement="top" data-content="test content <a href='&ap ...

Providing input through the URL bar in PHP and HTML

I have a simple form on my website's HTML page. <form action="post.php" method="post"> Message: <input type="text" name="message" /> &nbsp; <input type="submit" name="submit" value="send"> </form> After submitti ...

Arrange the child divs on top of the parent div

<div id="1"> <div id="2"> </div> </div> It is proving challenging to position div2 above div1 in the vertical dimension. Applying a negative top position does not have the desired effect, as it appears that the top border of the ...

The ellipsis menu pops up next to the final video in the series of HTML videos

Currently, I am working on rendering HTML video tags for playing videos. However, I am facing an issue where the three dots menu is not appearing near the button that triggered it, instead, it displays near the last video's three dots. To illustrate t ...

Transferring data from HTML label to TypeScript

Looking for some assistance with this code snippet: <sh-toggle label='ABCD' id = 'ABCD'> </sh-toggle> I am trying to extract the value of the label in my TS file. Can anyone provide guidance on how this can be achieved? Whe ...

What is the best way to reset the animationTransform using JavaScript?

Incorporating SMIL animations in Firefox is presenting a challenge for me. The animation starts automatically upon page load, but I need to find a way to trigger it dynamically through JavaScript. I suspect that the issue may lie with the begin attribute. ...

Is there a way to turn off alerts from Aspx files for HTML and CSS?

Dealing with annoying warnings in my aspx files has been a constant struggle. The "CSS Value is not defined" message pops up when I reference CSS files from different projects, causing unnecessary frustration. Even more frustrating are the warnings about i ...

"Transforming portfolio photos from black & white to vibrant color with the click of a filter button

I'm currently working on creating a portfolio that includes button filters to toggle between black and white images and colored images based on the category selected. While I have successfully implemented the functionality to change the images to bla ...

Video tag with centered image

For a current project, I am in need of rendering a centered image (a play button) at runtime on top of a video based on the UserAgent. If the userAgent is not Firefox, I want to display the image as Firefox has its own playEvent and button on top of the vi ...

How to prevent v-menu from overlapping a navbar in Vue.js

Exploring the examples on the main page of Vuetify, we come across the v-menu component showcased in detail. Check it out here: https://vuetifyjs.com/en/components/menus/#accessibility If you activate any of the buttons to open the v-menu and then scroll ...

I am experiencing an issue where links are not aligning correctly within table cells in Outlook

In the email preview from Email on Acid, I am seeing some centered text and links, but it is not displaying as expected. Most other email clients are showing it properly. https://i.sstatic.net/wCPVH.png Here is a snapshot of how it should look: https:// ...

:empty selector for targeting an empty tbody element

Here is the table provided for reference: <table id="incidents"> <thead> <tr> <th></th> </tr> </thead> <tbody> </tbody> </table> When using jQuery, th ...

Retrieve the maximum number of entries from the database and reduce it each time one is selected, using a combination of HTML,

My code is designed to distribute points from a pool into stats through a form. Here is the code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://mynindo.pl/test/css/style.css"> ...

CSS properties for SVG image borders not displaying correctly

I'm having trouble creating a border around a round SVG image with the class "lock feature". When I try to add the border in the CSS element ".lock feature", the text "feature" stays white and the border doesn't show up in the browser. However, i ...

What is the best way to align text to the center in a bootstrap table?

Is there a way to center-align the text in this table? The text-center class doesn't seem to be working. <!-- Bootstrap-5 --> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

Refresh the current page and log out the authenticated user

One issue I am facing is with a button on my website that is supposed to reload the page. However, when this button is pressed, it unexpectedly logs out the user and redirects them to the homepage. I am implementing passport-local for user authentication ...

Prevent using HTML escape characters when coding in PHP

When attempting to make an API call using PHP, one of the parameters required is 'currency'. The API call looks like this: <?php $call=".....&currency=USD&......."; $response = hash_call("Pay", $call); ?> However, when I print ...