Maintaining uniform cell width in tables even when some data is missing

Hello, I have a webpage that dynamically displays various tables with different data sets. The issue I'm facing is that some columns do not have data, causing the tables to display at varying widths. This inconsistency in width is aesthetically unpleasing and I would like all tables to be the same width. Is there a way to achieve this using CSS without having to resort to setting pixel values for widths? Below is my current CSS code:

table.motionEventsTable {
  margin: 0 auto;
  width:90%;
  word-wrap:break-word;
}
table.motionEventsTable td:nth-child(1){width:18%;} /* first column */ 
table.motionEventsTable td:nth-child(2){width:15%;} /* second column */
table.motionEventsTable td:nth-child(3){max-width:15%; word-wrap:break-word;} /* third column */ 
table.motionEventsTable td:nth-child(4){width:22%;} /* fourth column  */ 
table.motionEventsTable td:nth-child(5){width:30%;} /* fifth column */ 
<table id="motionsTable" summary="This table is an index of all papers">
  <thead>
    <tr>
      <!--<th>&nbsp;</th> -->
      <th scope="col">Task </th>
      <th scope="col">Event</th>
      <th scope="col">Date</th>
      <th scope="col">Person</th>
      <th scope="col">Result</th>

    </tr>
  </thead>
  <tbody>
    <tr>
      <td>lkdjf dpfj dlkfkhg pldfjk;l</td>
      <td>ojkdfh dsfkjh</td>
      <td>August 29, 2013</td>
      <td>
        August 29, 2013 jlkfdj</td>
      <td>James Bradley gfhf;lmk fglkjm ;ldf;ljgk ;dlfjkgd</td>
    </tr>
    <tr>
      <td>fgh</td>
      <td>fghfg</td>
      <td>August 29, 20136 787899 990 000</td>
      <td>
        August 29, 2013
        &nbsp;
      </td>
      <td>James Bradley</td>
    </tr>
    <tr>
      <td>fghgfn</td>
      <td>fghfg</td>
      <td>August 29, 2013</td>
      <td>
        August 29, 2013
        &nbsp;
      </td>
      <td>James Bradley</td>
    </tr>
    <tr>
      <td>August 29, 2013</td>
      <td>fghfg</td>
      <td>August 22, 2013</td>
      <td>
        James Bradley
      </td>
      <td>n/a</td>
    </tr>
    <tr>
      <td>fghfgh</td>
      <td>gfhfh</td>
      <td>August 29, 2013</td>
      <td>
        James Bradley
      </td>
      <td>n/a</td>
    </tr>

  </tbody>
</table>

Answer №1

It appears that by implementing table-layout: fixed; within your table's CSS, you will achieve the desired outcome as per your inquiry.

This action ensures uniform width for all columns, unless individual widths are defined for certain columns. In such cases, any remaining space is evenly distributed among unspecified columns.

Answer №2

If you want all columns in a table to have the same width, you can use the CSS property table-layout with the value fixed. Simply add table-layout: fixed; in your CSS and every column will be of equal width.

To see this in action, click here: Try It Yourself - CSS table-layout

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

An effective method for utilizing a multiple choice input (presented as buttons) for entering data into a database using html, css, and js

I'm trying to figure out the coding process using buttons and input It's giving me a result, but only allows for a single choice Although I have limited experience with this, my goal is to create a multiple choice selection for days of the week ...

Encountered an error while trying to click the cookie button using Selenium: "object[] does not have a size or

I've been struggling to interact with a button inside a pop-up using Selenium, but I keep encountering this error: object [HTMLDivElement] has no size and location I initially tried to simply click the button since it is visible on the page and I wai ...

Animating slides with CSS Keyframes and React, incorporating toggle slide fade out and slide fade (back) in

I am seeking a solution for toggling a box (div) with slide-out animation and then sliding back in animation (in reverse) upon button press. My requirement is to have no animations during the initial page render. While I can successfully slide the box to ...

Tips for deleting a duplicate identifier within an li element that possesses a particular class through javascript

I need help removing duplicate IDs from list items with a specific class. For example, in the following list, there are duplicates IDs (small, medium) and I want to remove the IDs with the class "size". How can I achieve this? <ul id="bulk"> ...

What could be causing my textarea-filling function to only be successful on the initial attempt?

Programming: function updateText() { $("body").on("click", ".update_text", function(event) { $(this).closest("form").find("textarea").text("updated text"); event.preventDefault(); }); } $(document).ready(function() { updateText(); }); H ...

The :empty selector is failing to function properly in @media print queries

Currently, I am working in Twig on Symphony and have encountered an issue with an empty div that needs to be hidden in print mode. <div class="form-fields__list"> </div> Surprisingly, the :empty selector works perfectly fine on the screen, bu ...

Displaying several column headers while filtering data in a table

Is there a way to prevent my table from printing multiple column headers each time I filter my results? For instance, after filtering, the same column headers are repeated. Here is an example of the issue: Below you can find the code snippet that demonstr ...

Scroll horizontally in a div with a minimum width while it is contained within a column using Bootstrap

I am seeking to create a bootstrap v4 grid with 3 columns, where certain content within the columns require a minimum width. This necessitates a horizontal scroll (overflow-x), but I am encountering some overlap where the scrollbar and content extend beyon ...

Displaying a list of values in Angular using data retrieved from an Entity Framework query

Is there a way to populate a list (ul li) with data from an array that is populated through Entity Framework code? Currently, I am able to populate an option dropdown successfully using the following code: <select class="form-control" name="Search" ...

Error encountered during GSTIN validation in the Polymer JS console

I'm currently working on a Polymer JS code that validates GSTIN: <dom-module id="gst"> <template> <div> <label for="gstid" class="gstlabel">GSTIN</label> <input type="te ...

Determining the container height for every two-image row

I am currently working on a project for this website. My focus right now is on resizing vertical images using the following script: function Gallery(selector) { this.add_module = function (type, image) { var portrait_text = image.next('.portr ...

Tips for hiding one sub-navigation menu when hovering over another sub-navigation menu

Setting up the main navigation menu: <ul class="menu12"> <li><a href="/">Home</a></li> <li><a href="/">About</a> <ul> <li><a href="/">History</a></li> <li ...

Error: The function modal() is undefined in the jQuery library

I am trying to open a modal on page load, but I encountered the following error: jquery.min.js:2 Uncaught TypeError: $(...).modal is not a function at HTMLDocument.<anonymous> ((index):491) at j (jquery.min.js:2) at k (jquery.min.js:2) Below is th ...

Is it possible to upload multiple files using JavaScript?

My JavaScript project can be found on GitHub. You can check out a live demo of the project here. The main goal for my HTML Button with id='Multiple-Files' is to enable users to upload multiple files, have them displayed in the console, and then ...

Fixed-top navbar in Bootstrap obscuring webpage contents

I have tried various solutions to the issue at hand, but nothing seems to be working. I applied padding-top to the container of my main content, but when I add the following CSS: @media (max-width: 768px) { body{ padding: 0; } } it overrides the padd ...

Can minification of JS be achieved in a Jekyll environment?

Currently, I am in the process of developing a project with Jekyll and one of the requirements is to minify HTML, CSS, and JS. I was wondering if Jekyll has built-in features for JS minification. It may seem like a simple question, but since I am new to ...

Excessive Blank Space Issue on Mobile Devices with MDBootstrap

I have been working on creating a replica of Watch2Gether, and it looks great on medium and large screens. However, when viewed on a phone, everything seems perfect until you scroll to the left and notice a large white space on the right side. I've ex ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

Modifying text size using JavaScript string manipulation

I'm currently experimenting with a countdown script, but I'm struggling to change the size of the numbers displayed. Can anyone help me find where I can adjust the font and font size in this script? var eventdate = new Date("February 20, 2014 11 ...

What is the best way to arrange images in a horizontal orientation?

How can I change the order of images from vertical to horizontal in one row using fancybox3 and boostrap4? Here is the code I am using: <?php $sayfa = 'Ürünler'; include('inc/vt.php'); include('inc/head.php'); include(&a ...