Content in Table TD with rowspan attribute is concealed by setting the visibility of TR to "collapse"

I have created an HTML table where some rows are hidden using CSS with visibility:collapse

By default, only the first and last rows of the table are visible.

In one column on the right side, I've set rowspan to accommodate multiple lines of text.

The issue arises when the content in this column is taller than the combined height of the displayed default rows (first and last), as it appears truncated.

.hide {
  visibility: collapse
}

body {
  padding: 2rem;
}
<table border="1">
  <tr>
    <td>A1</td>
    <td>A2</td>
    <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
  </tr>
  <tr class="hide">
    <td>B1</td>
    <td>B2</td>
  </tr>
  <tr>
    <td>C1</td>
    <td>C2</td>
  </tr>
</table>

What modifications should be made in the CSS to display all lines of "Text" in the rowspan cell without truncating them? JavaScript cannot be utilized.

Answer №1

Personally, I find it more effective to utilize display:none; or visibility:hidden;. Alternatively, you can apply overflow:auto; on TD, but this may result in a scrollbar appearing. Check out this JSFiddle for an example.

For information on visibility:collapse;, refer to this post on CSS-Tricks.

.hide{

  /* Utilizing this will disrupt the layout */
  /* visibility:collapse; */
  
  /* Using this will hide the element */
  /* visibility:hidden; */
  
  /* display none is recommended */
  display: none;
}
<table border="1">
<tbody>
   <tr>
      <td>A1</td>
      <td>A2</td>
      <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
   </tr>
   <tr class="hide">
      <td>B1</td>
      <td>B2</td>
   </tr>
   <tr>
      <td>C1</td>
      <td>C2</td>
   </tr>
</tbody>
</table>

Answer №2

After delving into this issue, it became apparent that the problem description and the construction of the <td rowspan="3"> in the example were causing confusion for everyone, myself included...

The solution, after hours of frustration, turned out to be quite simple: the OP is attempting to fit 6 rows of text into a space meant for only 3 rows due to the presence of 5 <br> tags. The content exceeds the available space, which causes the cell to overflow. Instead of setting the rowspan to 3, it should actually be set to 6!

Once the spacing issue was identified, one possible solution involved adding more table rows to accommodate the 6 rows of text and implementing a scrollbar when needed, while also clipping the overflowing cell when there are less than 6 rows in the table.

On the page table: The Table element: Displaying large tables in small spaces, MDN defines a table as

table { display: block; overflow: auto }
and introduces tbody { white-space: nowrap } to enable scrolling of table content.

I provided some commented code demonstrating the original and solution 1 with easy toggles for the class .hide and additional rows.

Do I find this ideal? Not at all, but this seems to be how the workings of the table operate. It's not necessarily a bug, just something that requires a workaround...

UPDATE

In response to @MaxiGui's valid point below about me neglecting the OP's question on "What should I change in the CSS to get all the lines of "Text" of the rowspan cell displayed instead of being truncated", I included another solution.

The added solution 2 showcases the OP's original code, but simply switches the class .hide between display: none/table-row rather than using visibility: collapse/visible.

When all size properties of an element have to be set to 0 (zero) to mimic display: none, it is much simpler to use the display property instead of visibility to hide a table row.

It's worth noting that the OP didn't explicitly mention the need to retain visibility...

.

Answer №3

.hide {
  position: absolute;
  left: -999em;
}

body {
  padding: 2rem;
}
<table border="1">
  <tr>
    <td>A1</td>
    <td>A2</td>
    <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
  </tr>
  <tr class="hide">
    <td>B1</td>
    <td>B2</td>
  </tr>
  <tr>
    <td>C1</td>
    <td>C2</td>
  </tr>
</table>

display: none; Element is removed from the normal flow and hidden; the space it occupied is collapsed. Content is ignored by screen readers. So, to hide content but keep it accessible for screen readers, you can utilize the code snippet provided above in my opinion.


Answer №4

If you find it necessary, you have the option to include line-height: 0; within the .hide selector like so:

.hide {
  visibility: collapse;
  line-height: 0; 
}

This particular fix was suggested in this discussion: CSS: "visibility: collapse" still takes up space

It is also recommended to apply the visibility:collapse directly on the td, as this allows more room for the rowspan cell, which can be observed by comparing the two tables.

While I personally prefer using display:none;, the choice between methods would depend on the specific requirements of your project.

DEMO

#hide .hide {
  visibility: collapse;
  line-height: 0; 
}

body {
  padding: 2rem;
}
/* SECOND table - collapse on td */
#td-hide .hide > td{
  visibility: collapse;
  line-height: 0;
  height:0;
  border: 0;
  padding:0;
  margin:0;
}
.container{
  display:flex;
}
.container > div{
  margin:0 auto;
}
<div class="container">
  <div>
    <h3>visibility: collapse on tr</h3>
    <table id="hide" border="1">
      <tr>
        <td>A1</td>
        <td>A2</td>
        <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
      </tr>
      <tr class="hide">
        <td>B1</td>
        <td>B2</td>
      </tr>
      <tr>
        <td>C1</td>
        <td>C2</td>
      </tr>
    </table>
  </div>
    <!-- Second table with collapse on td -->
  <div>
    <h3>visibility: collapse on td</h3>
    <table id="td-hide" border="1">
      <tr>
        <td>A1</td>
        <td>A2</td>
        <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
      </tr>
      <tr class="hide">
        <td>B1</td>
        <td>B2</td>
      </tr>
      <tr>
        <td>C1</td>
        <td>C2</td>
      </tr>
    </table>
  </div>
</div>

Answer №5

To achieve the desired output, you can utilize the CSS properties position: absolute and top: -1000000px. Here is a code snippet that demonstrates how you can hide elements with this approach:

.hide {
  position: absolute;
  top: -100000px;
}

body {
  padding: 2rem;
}
<table border="1">
  <tr>
    <td>A1</td>
    <td>A2</td>
    <td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6<br>Text 7<br>Text 8<br>Text 9</td>
  </tr>
  <tr class="hide">
    <td>B1</td>
    <td>B2</td>
  </tr>
  <tr>
    <td>C1</td>
    <td>C2</td>
  </tr>
</table>

Hopefully, this solution addresses your concern effectively.

Answer №6

Hey there! To ensure your content is displayed properly, consider adding a specific class with the following properties: width: auto; overflow: auto; height: auto;. I recommend using the following CSS:

.hide {
  visibility: collapse;
}

body {
  padding: 2rem;
}

.meta {
  width: auto;
  overflow: auto;
  height: auto;
}
<table border="1">
  <tr>
    <td>A1</td>
    <td>A2</td>
    <td class="meta" rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
  </tr>
  <tr class="hide">
    <td>B1</td>
    <td>B2</td>
  </tr>
  <tr>
    <td>C1</td>
    <td>C2</td>
  </tr>
</table>

Consider whether this approach aligns with your specific needs and requirements.

Answer №7

If you want your table data to be scrollable, take a look at the code snippet below:

.hide {
  visibility: collapse
}

body {
  padding: 2rem;
}

.table-data {
  overflow-y: auto;
}
.table-data::-webkit-scrollbar {
    display: none;
}
<table border="1">
  <tr>
    <td>A1</td>
    <td>A2</td>
    <td rowspan="3" class="table-data">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
  </tr>
  <tr class="hide">
    <td>B1</td>
    <td>B2</td>
  </tr>
  <tr>
    <td>C1</td>
    <td>C2</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

A guide on choosing a custom button color and automatically reverting to its original color when another button is clicked

I have a collection of 24 buttons, all in a dark grey (#333333) shade. Whenever I click on one of the buttons, it changes to a vibrant blue color (#0099ff), which is functioning correctly. However, when I proceed to click on another button, the previous ...

Why is the image cut in half on mobile devices but displaying correctly on computer screens? Could it be an issue with

There seems to be an issue on mobile screens that does not occur on computer screens. When the user clicks on the image, it disappears, and when they click another button, it reappears. However, there is a problem with how the image appears - it is cut off ...

Position the AdMob banner at the bottom of the page

I am attempting to place an AdMob banner at the bottom of my page, but the Ad is not appearing. There seems to be space allocated for it, but the AdMob banner is not displaying. Include and reference the Google Play services library. - CONFIRMED Add a me ...

Adjust the pagination page size based on the value in the Angular scope dynamically

My goal is to provide the user with the ability to adjust the number of rows displayed in a table. This feature is crucial for our web app, as it needs to be accessible on various devices and should allow users to customize their viewing experience to redu ...

The intricate scripting nestled within the jQuery function

When using jQuery, I am looking to include more codes within the .html() function. However, I also want to incorporate PHP codes, which can make the writing style quite complex and difficult to read. Is it possible to load an external PHP/HTML script? fu ...

Display mobile app components directly inside HTML tags within a webview

Is it possible to integrate our native Android components within regular HTML content and display them on a webview? For instance, instead of a standard button in a basic HTML page, can we replace it with a Native Android Button? How would this be accomp ...

The issue of border-radius and shadow box inconsistency

I'm trying to style a div with the following properties: .example { margin: 200px; width: 200px; height: 200px; border-radius: 20px; box-shadow: white 0px 0px 0pt 8pt, #033354 0px 0px 0pt 11pt; } <div class="example"> </div> ...

Is there a way to enlarge images when hovered using canvas?

I came across a fascinating example at the following link: , where the images expand when hovered over. I am aware that this can be achieved using jquery, but I have concerns about its speed and reliability. I would like to experiment with d3.js, although ...

Can you explain the significance of "q" and "+" within the CSS properties of the HTML body tag?

This solution for question #2 on CSSBattle is considered one of the best. However, the use of the ""+"" sign and ""q"" in this single line of code is perplexing. <body bgcolor=62375 style=margin:0+50;border:dashed+53q#fdc57b;clip-pat ...

Unusual Blank Space Found Beneath a Displayed Block Item

I've noticed some odd whitespace appearing under all my display: block elements within a meganavigation. Despite adding the code snippet below (which also determines column widths), I can't seem to pinpoint what's causing this issue. Wheneve ...

JavaScript Radio Buttons

Below are the different radiobuttons: Apple <input type="radio" id="one" name="apple" data-price="10" value="light"/> Light <input type="radio" id="two" name="apple" data-price="20" value="dark" /> Dark <input type="text" id="appleqty" name ...

Implementing a dependent <select> within an HTML table is a useful feature to enhance user experience and organization of

I have set up a table with editable columns where values can be selected from a drop-down menu. I achieved this by using HTML select. The options in the 'Category tier 2' column are based on the selection made in the 'Category tier 1' c ...

Large padding in the main container of Bootstrap 4

Hey there, this is my third and hopefully final question. I'm having some trouble with my page layout that was supposed to look like this initially: [navbar] [1][2][3] I was aiming to achieve the following effect when res ...

Maximizing Efficiency: Utilizing a Single jQuery Function to Retrieve Specific Values

I have a webpage with select menus for choosing user type, minimum age, and maximum age. I want to select options and send values as an object array to ajax. Initially, the getAjax function is working when the page loads. However, it stops working when I c ...

When clicked, elevate the element to the top of the window with an offset

Is there a way to click on this button, which is located within an accordion section header, and have it automatically move to the top of the page based on the window size? It seems like it should be a simple task, but sometimes after a long day things ca ...

Struggling to locate the element using xpath on a JavaScript enabled website in Selenium with Chrome

I have been attempting to extract data from a website that utilizes Javascript. Despite researching similar inquiries on xpath in Selenium, I have not found a solution. I initially tried using requests, but as the JavaScript doesn't load completely, I ...

The outcome of the Javascript Calculator is showing as "Undefined"

I've been attempting to create a calculator using JavaScript, but I'm facing an issue where the 'operate' function keeps returning Undefined, and I can't seem to figure out why. I suspect that the problem lies with the switch state ...

Expanding list item with jQuery

I am facing an issue with the dropdown functionality of my <li> elements. The problem occurs when clicking on the 4th option, as the expander appears on top instead of dropping down beneath the list item. If you check out this jsFiddle, you can see ...

Bootstrap container with added page border

Allow me to explain the issue to the best of my ability: I have a design that needs to be implemented using bootstrap. I prefer to use only CSS and avoid JS/jquery. The content is contained within a bootstrap container that has two columns. The left gre ...

How can we transfer parameters in JavaScript?

My vision may be a bit vague, but I'll try to explain it as best as I can. I want to implement multiple buttons that can toggle the visibility of a div (I have this functionality working already). Each button should carry two values (a number and a l ...