CSS technique for adjustable column width in HTML tables

Important Note: The HTML to PDF rendering engine I am using disables JavaScript by default, so I am seeking solutions that utilize CSS only.

Within my report, there is a table utilized for accounting purposes featuring 11 columns:

  • The first column serves as a counter (e.g. 1., 2., up to n.) and may span up to 3 digits.
  • The second column houses a 12-digit reference number.
  • The third column displays the address corresponding to the reference number in the previous column; this field can be empty or contain at least 50 characters.
  • The remaining columns contain data related to amount, currency, or price, with values ranging from 0.00 to a maximum of 8 digits separated by commas.

Assuming the width of the table needs to be set to 1024px (subject to the requirements of the PDF rendering engine), my objective is to:

  • Dynamically adjust the widths of the first, second, and fourth to eleventh columns based on their content, resulting in varying column widths depending on the longest content present during generation.
  • For the third column, it should adapt to the remaining space within the original 1024px width, adding an ellipsis to indicate overflow if necessary (crucial).

I initially attempted to allocate fixed widths to each column totaling 1024px, successfully implementing ellipsis overflow through CSS. However, undesired white spaces were observed, prompting the need for optimization:

  • Specifically, I set static widths of 75px / 100px / 125px for the fourth to eleventh columns, irrespective of whether the content was 0.00 or not. Notably, certain columns such as the fifth, eighth, ninth, and tenth could potentially have their widths reduced by half. https://i.stack.imgur.com/wSsBK.png

Answer №1

If you want to ensure your columns add up to 100% of the container width, consider using percentage values for width along with a min-width in pixels as shown below:

//CSS
.col1{
  width:5%; 
  min-width:20px;   
}
.container{
  width: 1024px;
  margin: auto;
}
#MyTable{
  width: 100%;
}

Remember to apply the same principles to all other columns as well!

In the HTML code, make sure to wrap the table inside a container element like this:

<div class="container">
  <table id="MyTable">
    <caption>table title and/or explanatory text</caption>
        <thead >
            <tr>
                <th id="col1">nth</th>
                <th id="col2">ID number</th>
                <th>Address</th>
                <th>Price 1</th>
                <th>Price 2</th>
                <th>Price 3</th>
                <th>...</th>
                <th>...</th>
                <th>...</th>
                <th>...</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>100</td>
                <td>201532112009</td>
                <td>Avenida Primera/ SOme address</td>
                <td>14.153,00</td>
                <td>0.00</td>
                <td>9.000</td>
                <td>and</td>
                <td>others</td>
                <td>others</td>
                <td>others</td>
            </tr>
        </tbody>
  </table>
</div>

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

This video cannot be played on this platform due to its privacy settings

I'm facing a challenge with embedding my client's videos on our website. These videos have domain-level privacy settings, and I've been using the code provided by Vimeo. <div style="padding:28% 0 0 0;position:relative;"> ...

Unable to identify the Xpath selector for the "Account settings button" and "find my iPhone" features within iCloud

When trying to access my iCloud account settings and locate the "Find my iPhone" button on iCloud.com, I am encountering issues with my selectors. It seems like there are no frames on the page, but the Xpaths I'm using are not matching. Here are the ...

How about having a surprise image pop up when you click a button?

I've been working on coding a button that can change the background of my webpage to display a random image. Initially, my code seemed functional, but it was only displaying the last image listed in my series of if/else statements. I suspect that usin ...

Image remains fluid within a static div without resizing

Any assistance would be greatly appreciated. I am currently facing an issue with a fixed div that is floating at the bottom of the screen, serving as ad space for the mobile version of a website. The problem arises when attempting to resize the browser win ...

How to prevent page refresh when hitting enter in jQuery form?

Currently, my form does not refresh the page when I click the button to submit it. However, if I press Enter while a text input is selected, the page will refresh. Is there a way to make pressing Enter on the text input perform the same action as clicking ...

Error encountered: index.html:17 - An InvalidStateError was thrown when attempting to execute the 'send' function on the XMLHttpRequest object. The object's state must be OPENED in order to send the Ajax request

When attempting to run index.html on a local host with xampp, an error is encountered: index.html:17 Uncaught InvalidStateError: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.sendAjax @ index.html: ...

Is there a way to show text as symbols in Primeng components?

Check out this helpful example that demonstrates what I am attempting to achieve. I have implemented p-menu from primeng. Is there a method to convert text into symbols like &trade to ™ and &#8482 to ®? ...

Incorporating Anchors for Seamless Navigation in Menus

My website has various sections on the homepage and I want to add an anchor link to the navigation. This will allow users to scroll down to a specific section, like "About Us," when clicking on the appropriate nav link. I also need the anchor link to work ...

Using querySelector to Target Elements by their InnerHTML Content

Is it possible to target an element by its innerHTML directly without the use of loops? Is there a way to achieve this with something like document.querySelector('div[innerHTML="Sometext"]') or document.querySelector('div[textcontent="Som ...

Using Javascript to select a radio button in a form depending on the value entered in a text box

I have a form that interacts with a Google Sheet to insert and retrieve data. For instance, the form contains two radio buttons: <input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" value="1" onchange="RadioValInsert ()"/> < ...

Vertical spacing on elements utilizing a position of absolute

Looking to create vertical space for elements with position:absolute and changing heights using just CSS. Any clever techniques involving containers or display properties that could be helpful? ...

Prevent content from occupying unnecessary space below a sticky div

When the "link" on the sticky header is clicked in this scenario, how can I ensure that the linked content item (#mypara) appears below the sticky div rather than directly underneath it where it may be hidden? $(document).ready(function() { $(window ...

The transparent background causes the vertical menu to malfunction on IE8

This is the first website I am working on: www.olgoya.com The issue I am facing is that all major browsers, except IE8, display the submenu even when the mouse hovers over the 'portfolio' item. Upon investigation, I found that the problem lies w ...

Implementing rowspan in PHP to display array data in a table

Seeking assistance with updating a table on a webpage to incorporate merged rows. Below is the php code responsible for processing data from a mysql database: ######## DISPLAYING TABLE WITH YEARS AND OFFICES ALONG WITH NAMES IN CELLS ######## for ...

Image tinting color picker tool (using HTML, CSS, and web technologies)

I'm currently exploring ways to apply a filter or tint on top of an image using CSS/HTML or some lightweight solution. For example, Ideally, my goal is to only have one image displayed on the page and overlay it with a transparent filter based on a ...

Animating colors with jQuery and shifting SVG shapes to create dynamic and

I am currently working on an svg animation that involves changing the color of the svg to a different color, creating a running light effect. Rather than fading the fill color of the entire svg as commonly seen in examples, I aim to achieve a dynamic trans ...

jQuery can be used to automatically close a subnav when another subnav is opened

Currently, my code is almost perfect, but there is a slight issue with allowing multiple sub-navs to be open at the same time. I want to ensure that when one sub-nav is opened, any others automatically close. Essentially, in the jQuery code below, I need ...

What is the reason behind the malfunction of the float?

Having an issue with the "1" form not extending to 100% width next to the "Tickets". Here is a visual representation of the problem: View how it currently looks here. html: <section id="main_content"> <div class="left inner"> ...

I am looking to automatically add a line of HTML text to the notifications list without requiring the user to manually refresh the page

Having trouble articulating the correct term or language I need, making it difficult to search. Q: How can I push a line of text from the server side to the user? I am developing an MVC 4 application using asp.net and c# in .net4. One page will feature a ...

Adaptable Image Maintains Integrity

My code is generating the following output: While I want the images to resize when dragged in, one of the right-hand images does not move below the main image as intended. Here is my code: Can anyone help me resolve this issue? Check out my CSS and HTML ...