The alignment of tables is off when they are identical in HTML and CSS

In an attempt to align two tables, I created a simple demonstration with this reproducible code. The issue is that the first table's headers are slightly narrower than the second table's data cells, resulting in misalignment of the content.

#index_table,
                #index_table_header {
                    text-align: left;
                    margin: 20px;
                    margin: 0 auto;
                }

                #index_table,
                #index_table_header {
                    width: 800px;
                }

                #index_table_header th {
                    padding: 10px 8px;
                    width: 100px;
                    border: 1px solid black;
                }

                #index_table td {
                    padding: 10px 8px;
                    width: 100px;
                    border: 1px solid black;
                }

                #index_table td:nth-child(1),
                #index_table_header th:nth-child(1) {
                    width: 60px;
                }

                #index_table td:nth-child(3),
                #index_table_header th:nth-child(3) {
                    width: 70px;
                }

                #index_table td:nth-child(5),
                #index_table_header th:nth-child(5) {
                    width: 200px;
                }

                #index_table td:nth-child(2),
                #index_table_header th:nth-child(2) {
                    width: 250px;
                }
<table id="index_table_header">
                    <thead>
                        <tr>
                            <th scope="col">ID</th>
                            <th scope="col">Item</th>
                            <th scope="col">Amount</th>
                            <th scope="col">Added</th>
                            <th scope="col">Nutritional Value ID</th>
                            <th scope="col">Actions</th>
                        </tr>
                    </thead>
                </table>
            
                <table id="index_table">
                    <tbody>
                        <tr>
                            <td>395</td>
                            <td>chicken liver</td>
                            <td>0.37</td>
                            <td>2019-10-14</td>
                            <td>67</td>
                            <td>
                                <a href="/delete/395">Delete</a>
                                <br>
                                <a href="/update/395">Update</a>
                            </td>
                        </tr>
                    </tbody>
                </table>

Answer №1

To make your table adjust its text wrapping based on specified widths, include table-layout: fixed; in your table CSS rules.

table {
  table-layout: fixed;
}
#index_table,
#index_table_header {
  text-align: left;
  margin: 20px;
  margin: 0 auto;
}
#index_table,
#index_table_header {
  width: 800px;
}
#index_table_header th {
  padding: 10px 8px;
  width: 100px;
  border: 1px solid black;
}
#index_table td {
  padding: 10px 8px;
  width: 100px;
  border: 1px solid black;
}
#index_table td:nth-child(1),
#index_table_header th:nth-child(1) {
  width: 60px;
}
#index_table td:nth-child(3),
#index_table_header th:nth-child(3) {
  width: 70px;
}
#index_table td:nth-child(5),
#index_table_header th:nth-child(5) {
  width: 200px;
}
#index_table td:nth-child(2),
#index_table_header th:nth-child(2) {
  width: 250px;
}
<table id="index_table_header">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Item</th>
      <th scope="col">Amount</th>
      <th scope="col">Added</th>
      <th scope="col">Nutritional Value ID</th>
      <th scope="col">Actions</th>
    </tr>
  </thead>
</table>
<table id="index_table">
  <tbody>
    <tr>
      <td>395</td>
      <td>chicken liver</td>
      <td>0.37</td>
      <td>2019-10-14</td>
      <td>67</td>
      <td>
        <a href="/delete/395">Delete</a>
        <br>
        <a href="/update/395">Update</a>
      </td>
    </tr>
  </tbody>
</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

Using htaccess in combination with variables from _POST allows for powerful control over

I am encountering an issue with empty _POST variables after URL rewriting and redirection. Here is the HTML code snippet: <article class="span6"> <textarea id="msg" rows="3" cols="40" name="message" placehol ...

Incorporate dynamic interval transitions to the carousel rotation

I'm currently working on implementing a carousel with 'Next' and 'Previous' buttons that slide through images. However, I've been struggling to get the auto-slide feature to work at regular intervals. If anyone has any sugges ...

Unusual occurrence involving label span and the use of italic font styling

Currently following a Xamarin tutorial on creating a label view. You can check out the tutorial here. Encountered an issue where applying an italic font attribute to text within a span tag does not retain the text size set in the label tag. <StackLayo ...

Add distinctive formatting for the final element that is not the last child

Presented with a fascinating challenge, I find myself with an ever-changing number of .child.red children. I am in need of referencing the last .child.red element dynamically using styles. Although I have attempted to achieve this on my own, I am curious a ...

There appears to be an issue with the dynamic functionality of RouterLink in Angular 6

user-dashboard.html <ul class="nav flex-column"> <li class="nav-item"> <a class="nav-link" routerLink='/dashboard'>User Dashboard</a> </li> <li class="nav-item" *ngFor="let cat of categories; let i = in ...

Transforming the table into a list by categorizing every 3 rows together

SharePoint tends to use nested tables excessively, causing issues when trying to implement a jQuery carousel on them. To resolve this, I discovered a piece of jQuery code that can convert a table into a list: var list = $("<ul/>"); $('#tab1&apo ...

Enable the modal window to be scrollable

I've been struggling with configuring my modal to allow scrolling when the content overflows. I attempted to add "overflow: auto" to the modal, but it didn't seem to do the trick. It's important to note that I am not utilizing Bootstrap. One ...

In PHP forms, ensure that only completed textboxes are submitted and empty textboxes are discarded

I've created a form that displays all available products from a database along with their prices. Users can input the quantity they want to purchase for each product, and upon submission, the total amount will be calculated. There are 5 products in th ...

jQuery Ajax Load() causing screen to refresh unintentionally upon clicking

I am currently developing a web application that includes side menus. However, I am facing an issue with the functionality of these menus. Whenever I click on certain options, the entire page refreshes. For example, under my main menu "Planning the Engagem ...

Employing conditional statements to adjust the size of an image or background identifier in JavaScript

As I dive into the world of Javascript, I find myself faced with the challenge of modifying existing code using if / else statements. The original code snippet in question is as follows: document.getElementById("button1").addEventListener("click", functio ...

Utilizing CSS3 transformations for a symbol

I'm currently attempting to implement a CSS transformation on an icon (glyph) within a :before pseudo element. My primary objective is to execute a simple "flip" animation on the icon using transform: scale(-1, 1), but I am encountering difficulties ...

Is it possible for me to modify the parameters in confirm(); function in JavaScript?

Can I customize the choices for the JavaScript confirm(); command? I would like to have the options displayed in red and green for a specific purpose. var colorChoice = confirm("Which color do you prefer, red or green?"); if (c ...

Selenium Python can be used to save a web page as a

Trying to print a webpage as a PDF with headers and footers to mimic the output from Google Chrome. Any suggestions? Experimented with PhantomJS but couldn't get it to include headers and footers. from selenium import webdriver import selenium def ...

Ensuring the HTML5 video poster matches the dimensions of the video content

Is there a way to adjust the HTML5 video poster so it perfectly fits the dimensions of the video itself? Check out this JSFiddle demonstrating the issue: http://jsfiddle.net/zPacg/7/ Here's the code snippet: HTML: <video controls width="100%" h ...

Images are not appearing correctly

My website is having a strange issue with user profile pictures. When the image dimensions are small, it looks funny like in the example below: https://i.sstatic.net/oRuqd.jpg I am not sure how to fix this problem. Is there a way to make the images appea ...

disable the option to call on your iPhone

Hello there, I am currently working on a project which has a mobile browser version. I have noticed that on iPhone, all numbers are callable by default. My goal is to identify and select all callable numbers while disabling the call option. For i ...

Tips for stopping a flex:1 div from expanding to accommodate its abundant content

I'm facing a situation where I need to create two columns, one fixed at 150px and the other filling up the remaining space with scroll functionality for overflow content. Despite trying to use flexbox for this layout, the second column keeps expanding ...

Learn the process of extracting HTML content from a webserver response and displaying a specific tag value in a combobox within a desktop application using VB.NET

I am currently attempting to retrieve data from a URL using HttpWebRequest/HttpWebResponse in VB.NET desktop application. The response I receive is displaying the entire HTML content in a message box. Now, my objective is to extract a specific tag (TD tag) ...

Creating a full-height application with CSS3 Flexbox and managing overflow

I'm currently working on an app that utilizes a traditional email layout similar to the one illustrated below. Using the new CSS3 flexbox model, everything was functioning perfectly until I added the feature for users to dynamically insert new items ...

In JavaScript, is it possible to dynamically alter and showcase the value of a select tag?

My code snippet in the HTML file contains Javascript: <script> $(document).ready(function(){ $("#sub").click(function(){ var user_issue = $("#issue").val(); ...