What's the issue with HTML Table column width when using fixed-layout and 100% width?

Why does the table ignore column width and make them all even when using fixed-layout with a 100% width setting?

Here is how the table looks without the 100% width:

https://i.sstatic.net/MNrjZ.png

However, setting the table width to 100% causes all columns to become evenly sized:

https://i.sstatic.net/oS63q.png

How can this be fixed? I need to keep the 'fixed-layout' and 'merged th', while still making the table 100% wide.

(I could detect when the table uses 100% width and replace 'merged th' with div before the table, but I am looking for a better solution.)

The code snippet:

<style>
  th, td { border: 1px solid black; }
  table {
    width: 100%;
    table-layout: fixed;
  }
</style>

<table>
  <tr>
    <th colspan="2" width="100%">100% united th</th>
  </tr>
  <tr>
    <th style="width: 20%;">20% th</th>
    <th style="width: 80%;">80% th</th>
  </tr>
  <tr>
    <td>a</td>
    <td>b</td>
  </tr>
</table>

Answer №1

It appears that using the <colgroup> element can help resolve this issue, you can refer to a related question for more information on why it occurs.

<style>
  th, td { border: 1px solid black; }
  table {
    width: 100%;
    table-layout: fixed;
  }
</style>

<table>
  <colgroup>
    <col style="width: 20%;">
    <col style="width: 80%;">
  </colgroup>
  <tr>
    <th colspan="2" width="100%">100% united th</th>
  </tr>
  <tr>
    <th>20% th</th>
    <th>80% th</th>
  </tr>
  <tr>
    <td>a</td>
    <td>b</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

Display lengthy column text in a tooltip in an Angular view

On my webpage, I have a table with columns that are generated using AngularJs. If the text in a column exceeds 8 characters, I would like to display it within a tooltip. This is an example of one of my columns: <td> <i class="column3"> { ...

What is the method for distributing additional width in a table among its columns?

Have you ever wondered how excess space in an HTML table is distributed among the columns? I too was curious about this, but couldn't find a definitive answer even after extensive searching. Therefore, I decided to create a simple test page to investi ...

CSS selector targeting the value inside a textbox

I am working in an environment that only supports CSS and Bootstrap 3.2, with no JavaScript functionality. My current task involves creating a list filtering solution using pure CSS selectors and HTML. Imagine I have 2 items within an HTML list element. T ...

Exploring the power of pseudo elements within Angular5

What could be causing the issue with regular pseudo-classes from CSS not functioning as expected in Angular5? One workaround is to replicate their behavior using Angular event handlers such as (click)="function1()" or (mouseenter)="function2()". Why is it ...

How to turn off automatic password suggestions in Chrome and Firefox

Currently, I have integrated a 'change password' feature which includes fields for 'old password', 'new password', and 'retype password'. However, the autocomplete feature is suggesting passwords from other user acco ...

I wish for the form to automatically shut down whenever I click on the (X) icon

I included a close button in my form, but it doesn't seem to be working. Do I need to use JavaScript for this? <button type="button" class="close" data-dismiss="form" aria-label="Close"><span aria-hidden="true">&times;</span>& ...

The percentage of occurrences for each result with the specific id

In my database, I have a table that looks like this: Currently, in my PHP code, I am performing a SUM operation like this: function calculate_percentage() { $sql = "SELECT (sum(distance))/(25000)*(100) as totaldistance1 FROM axle WHERE train_id = ...

Is there a way to extract all the aria-label attributes from elements that share a common class?

I am currently working on a program that tallies the number of reactions from Facebook posts, with each reaction stored in an aria label containing detailed information. I am encountering difficulty retrieving the values of these unique aria labels due to ...

The scrollbar remains visible on mobile devices

I'm attempting to remove the scrollbar from all elements in my Vue + Vite application. I do not want to disable scrolling, just hide the scrollbar itself. To achieve this, I have employed the following code snippet. *::-webkit-scrollbar { display: ...

What are some ways to expand the width and increase the spacing of bootstrap cards?

On my website, I have created Bootstrap cards that are displayed on two different pages. One set of cards is shown on the landing page, where I am looking to increase the spacing between them. The other set appears on the books page, and I want to make t ...

The poker table designed with CSS does not resize effectively when displayed in smaller dimensions

I have put together a CSS poker table design that I am quite happy with at the moment: https://jsfiddle.net/78fcs01m/3/ CSS: .poker-container { display: grid; width: 100vw; height: 100vh; .poker-table { justify-self: cent ...

iOS application becomes unresponsive when attempting to load a video within a webview, but does not

I developed an iPad app using a 3rd party tool called OpenPlug which converts AS3 to C++ and then exports to iOS. This app displays pictures and videos in a slideshow. The videos are shown using a WebView loading an HTML page with the video source set to a ...

Utilizing CSS for multiple carousels while applying unique styles to each one individually

I am currently working on modifying the code below so that it will only execute when a specific class is linked to the carousel. There are multiple carousels present on the same page. @media (min-width: 576px) and (max-width: 768px) { /* Display the 3r ...

Clicking on text within a DIV using jQuery

How can I use jQuery to show an "alert" when text inside a DIV is clicked, excluding images and other tags? The texts are contained within various DIVs along with images and tags. Is there a way to achieve this using jQuery? Check out the jsFiddle example ...

Using only HTML, I have created a collapsible accordion in Angular without the need for JS code. However, when I click the button, nothing happens. Can you help me resolve

I am attempting to add an Accordion button to my website using the HTML code provided below. I am currently incorporating this in a very basic manner, without any utilization of javascript code, within this example of accordion snippet. When the button i ...

"Is there a way to verify the existence of checkbox array values within a database using PHP Laravel

In order to display checkboxes for each country that exists in the database, I need to verify if the countries in the database match with those in the list. Since the values are retrieved directly from the database and can't be set as input values due ...

Angular reduces image width to zero

Looking to include an image in an Angular template, where the width attribute is equal to a variable width that sometimes holds the value null. In cases where width = null, I aim to have width = "" (or no width attribute at all). However, Angular assigns ...

Struggling to align an element in HTML when used in columns?

<div class="row"> <div class="col-lg-3 " style="border: groove;"> <p class="circle col-xs-1 center-block">01</p> <h3>trending courses</h3> ...

Tips for storing the order of sortable elements in MVC .Net?

Is there a way to retrieve all the item indexes, update them, and save them after changing their positions? <ul id="sortable"> <li class="ui-state-default">Item 1</li> <li class="ui-state-default">Item 2</li> <li clas ...

CSS: dynamic font-size based on text length

Can the font size be adjusted based on the length of the text without using JavaScript? I have a box with a width and height of 140px. The content inside should be 100px wide and high (with a 20px padding). The text should utilize this 100px width effecti ...