Tips for avoiding a situation where a table row with colspan disrupts the natural sizing of other columns due to lengthy content

I am dealing with a simple html table where rows can be clicked to show or hide the row below them, which contains additional details for the row above.

The "detail" row that is toggled on click has a colspan set so that one cell spans all columns in the table.

The table expands to take up 100% of the available horizontal space at all times.

However, I have encountered an issue that I cannot explain: when the content in the "detail" row overflows onto multiple lines, it causes all the columns to change their width!

<html>
   <head>
      <title>asdf</title>
   </head>
   <body>
   <table>
      <thead>
         <tr>
            <th>AAA</th>
            <th>BBB</th>
            <th>CCC</th>
            <th>DDD</th>
            <th>EEE</th>
         </tr>
      </thead>
      <tbody>
         <tr onclick="document.getElementById('detail').hidden = !document.getElementById('detail').hidden" style="background-color:red">
            <td>asdfa</td>
            <td>dhfdh dfhd hfdfdh  fdh dhdhd</td>
            <td></td>
            <td></td>
            <td></td>
         </tr>
         <tr id="detail">
            <td colspan="5">
                (content here)
            </td>
         </tr>
         <tr>
            <td>asdfasdf</td>
            <td>dgfhdh sdgdfdh</td>
            <td></td>
            <td></td>
            <td></td>
         </tr>
      </tbody>
   </table>
   </body>
</html>

https://jsfiddle.net/93dpowb5/ (click the red row to toggle the display of the "detail" row)

I have been able to replicate the issue in a jsfiddle. (tested on Firefox and Chrome)

I discovered that setting a max-width for the "detail" row resolves the issue (columns widths remain consistent), but I prefer not to do this to allow normal window resizing.

While I am aware of using table-layout: fixed, I want my columns to adjust dynamically. My goal is to keep them at the same width once the layout is calculated!

In essence, I am seeking a solution that maintains dynamic column sizing while preventing any sudden changes in column width when displaying the "detail" row...

Answer №1

Uncover/Conceal content in a table row:

body {
  overflow: scroll; /* To prevent page displacement */
}

table {
  width: 100%;
}

.hidden td {
  padding-top: 0;
  padding-bottom: 0;
}

.hidden div {
  height: 0;
  overflow: hidden;
}
<table>
  <thead>
    <tr>
      <th>AAA</th>
      <th>BBB</th>
      <th>CCC</th>
      <th>DDD</th>
      <th>EEE</th>
    </tr>
  </thead>
  <tbody>
    <tr onclick="detail.classList.toggle('hidden')" style="background-color:red">
      <td>asdfa</td>
      <td>dhfdh dfhd hfdfdh  fdh dhdhd</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr id="detail">
      <td colspan="5">
        <div>sssssss sssss</answer1>
<exanswer1><div class="answer accepted" i="77355401" l="4.0" c="1698170953" v="1" a="aW1odm9zdA==" ai="9267406">
<p>Show/Hide content within a table row:</p>
<div>
<pre class="snippet-code-css lang-css"><code>body {
  overflow: scroll; /* helps prevent page jumping */
}

table {
  width: 100%;
}

.hidden td {
  padding-top: 0;
  padding-bottom: 0;
}

.hidden div {
  height: 0;
  overflow: hidden;
}
<table>
  <thead>
    <tr>
      <th>AAA</th>
      <th>BBB</th>
      <th>CCC</th>
      <th>DDD</th>
      <th>EEE</th>
    </tr>
  </thead>
  <tbody>
    <tr onclick="detail.classList.toggle('hidden')" style="background-color:red">
      <td>asdfa</td>
      <td>dhfdh dfhd hfdfdh  fdh dhdhd</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr id="detail">
      <td colspan="5">
        <div>ssssssss ssssssssssss ssssssssssssssssssss ssssssssssss sssssssssssss ssssssssssssss ssssssssssssssss sssssssssssssssssssss ssssssssss ssssssssssssssssss sssssssssssssssssssss sssssssssssssss sssssssss ssssssssssssss sssssssssss sssssssssssssss ss sssssssssssss SSSSJSJS SLSKSLDK FDFHJFHFUE WIENHEIOWJK JFLASDF WE KLFWO *(*&^*(& WDDL WW KWEKLFSLDM<SDDSLLSDKNF SD GGSN KWLEKM 

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

Loading an empty CSS file in Node.js

Just starting out with node.js, and I could really use some help with a small css and image issue that I'm facing. I've streamlined my html and .js for clarity. Despite trying everything, the css and image just won't load. My form and server ...

Create varying widths of lines on either side of a vector line using the HTML canvas

Currently, I am working on utilizing an html canvas to draw lines. The process is straightforward; however, I am faced with a new challenge. I want to draw two lines at the same coordinates but with different widths - one on the right side and one on the l ...

The 'SafeString' object does not have a 'get' attribute

I am having trouble rendering a string to a template. Here is the code I am currently working with: context = { 'order': order, 'ordered_items': ordered_items, 'change': change, ...

Gather information that is dynamic upon the selection of a "li" option using Python Selenium

I need to extract data from this website (disregard the Hebrew text). To begin, I must choose one of the options from the initial dropdown menu below: https://i.stack.imgur.com/qvIyN.png Next, click the designated button: https://i.stack.imgur.com/THb ...

Practicing the basics of a simple 3-column layout before delving into the world of

Currently, I am working on a three column layout that appears to be functioning well on the surface. However, upon inspecting the page using developer tools, it becomes apparent that the margin of the third middle column is overlapping with the neighboring ...

Difficulty in aligning Grid elements in Material UI version 5

Strange enough, the MUI5 Grid is displaying unexpected spacing behavior. Instead of providing proper spacing between items and containers, it seems to be causing them to overlap. To see a live example, visit: https://codesandbox.io/s/green-worker-z44vds? ...

What are the best ways to stop jQuery events from propagating to ancestor elements?

I have a collection of nested UL's that follow this structure: <ul class="categorySelect" id=""> <li class="selected">Root<span class='catID'>1</span> <ul class="" id=""> <li>First Cat<span ...

Organizing an angular expansion panel

I am currently dealing with an expansion panel that has a lot of content. The layout is quite messy and chaotic, as seen here: https://ibb.co/Y3z9gdf It doesn't look very appealing, so I'm wondering if there is a way to organize it better or ma ...

Is there a way to ensure a dialog box is positioned in the center of the window?

After adjusting the dimensions of my jQuery UI dialog box with the following code: height: $(window).height(), width: $(window).width(), I noticed that it is no longer centered on the window. Do you have any suggestions on how I can recenter it? ...

The combination of sass-loader and Webpack fails to produce CSS output

Need help with setting up sass-loader to compile SCSS into CSS and include it in an HTML file using express.js, alongside react-hot-loader. Check out my configuration file below: var webpack = require('webpack'); var ExtractTextPlugin = require ...

CSS - Combining underline, striikethrough, and overline effects with customized styles and colors within a single element

I am trying to achieve a unique design with only one <span> that has three different text decorations (underline, strikethrough, and overline) like this: (This is just an example, I need it to be changeable) https://i.stack.imgur.com/61ZnQ.png Ho ...

Display a series of numbers in a stylish Bootstrap button with uniform dimensions

I am trying to find a way to show the number of days in a specific month within a bootstrap button. However, the code I have been using does not evenly distribute the buttons in terms of height and width. I would like the display to look similar to the sc ...

What is the best way to ensure that when a div is opened, the information to the right does not get pushed down?

I have a functioning menu bar, but when it expands, the content below is pushed down even though it should not be affected. How can I adjust my code to prevent this issue? View my website here: <div id="menu_wrapper"> <div id="menu"> &l ...

Modify the image inside a div when hovering

How can I create a product card that displays thumbnails of images when hovered over, and changes the main image to the thumbnail image? My current challenge is getting this functionality to work individually for each card on an e-commerce site. Currently, ...

Creating a Basic Slideshow using Javascript

I've been attempting to create a slideshow using only JavaScript, but I've run into an issue. When I set the divs to change on click, it works perfectly fine. However, when I try to set it to change at specific intervals, it doesn't work. ...

Is it possible to implement AngularJS Routing for displaying HTML pages within a slideshow?

I have a vision for my website in mind - a stunning slideshow with a sleek navigation bar at the top. The idea is to have each slide represent a different category or concept, all uniquely customized with their own HTML page. However, I'm facing a cha ...

Using Selenium to choose an element based on the text of its sibling element

I've been working on a selenium script for: My goal is to select the "Add to Compare" element for "Sony Xperia," but it's not being located. I've tried using both cssSelector and xpath, but I can't seem to figure out what I'm doin ...

Stop dropdown from closing upon clicking menu items

I have been experimenting with Bootstrap 4 alpha6 and have encountered an issue with my dropdown menu. Here is the HTML code for my dropdown: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" type= ...

What could be causing one Div to animate at a quicker pace than the other?

Why does the div with an id of "Second" animate slightly before the "first" div when using keyframe animation? I expected them to move at the same speed by default. Any help would be greatly appreciated, thank you. body { background-color: black; color: w ...

Exploring the concept of clip-path

Recently, I have been delving into the world of HTML and CSS, exploring clip-paths after stumbling upon them on this captivating website. My interpretation is that utilizing clip-paths, particularly with the polygon attribute, enables one to control which ...