Select a row on a table to reveal additional information in the row directly underneath

I am presenting the following table:

Live Demo

<table class="table">
  <thead>
    <tr>
      <th>Airport</th>
      <th width="150px">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td div class='action'>flight leaves on 13:20<br>Take the train from ABC</td> 
      <td>JFK</td> 
      <td>234</td>      
    </tr>
    <tr>
      <td>LAX</td>
      <td>104</td>
    </tr>
  </tbody>
</table>

along with some custom CSS styling

.action {
  display: none;
}

tr:hover .action {
  display: block;
}

Currently, when hovering over the airport name, a text appears inline like this: https://i.sstatic.net/5Ocze.png

The goal: When hovering over an airport name, detailed information should be displayed in a line below the airport taking up the entire space. For example, hovering over JFK should show:

Airport    Value
JFK        234

flight leaves on 13:20
Take the train from ABC

LAX        104

I initially tried using the display: block property, but it didn't align properly.

Answer №1

By using display: none/block, you can alter the layout of a table as the element's space is not allocated when it's not displayed among other tags. An alternative approach is to utilize visibility: collapse as suggested in the MDN documentation

The behavior of the collapse keyword varies for different elements:

For <table> rows, columns, column groups, and row groups, hiding the row(s) or column(s) eliminates their occupied space (similar to applying display: none). However, the dimensions of other rows and columns are recalculated as though the cells in the concealed row(s) or column(s) are present. This technique allows for quick elimination of a row or column without triggering a full table width and height recalculation.

Note the use of <td colspan="2"> ensures that the single td spans across both columns

tr.action {
visibility: collapse;
}
tr:hover + tr.action {
visibility: visible;
}
<table class="table">
  <thead>
    <tr>
      <th>Airport</th>
      <th width="150px">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>JFK</td> 
      <td>234</td>      
    </tr>
    <tr class="action">
      <td colspan="2">flight leaves on 13:20 Take the train from ABC</td>
    </tr>
    <tr>
      <td>LAX</td>
      <td>104</td>
    </tr>
  </tbody>
</table>

The transition between visibility: collapse/visible occurs instantly without any intermediate steps, making animation impossible. To achieve a smoother appearance upon hover, an alternate method could be to employ line-height: 0/1 and overflow: hidden instead

table {
  line-height: 1.2;
  border-collapse: collapse;
}
tr.action td {
  line-height: 0;
  overflow: hidden;
  padding-block: 0;
  transition: all 300ms;
}
tr:hover + tr.action td {
  padding-block: 1;
  line-height: 1.2; /*same as table line-height*/
}
<table>
    <tr>
      <td>hover me!</td>
    </tr>
    <tr class="action">
      <td>I'm showing up more smoothly</td>
    </tr>
    <tr>
      <td>next row</td>
    </tr>
  </table>

Answer №2

To start, include a "+" in your CSS code like this;

.activity {
  display: none;
}
tr:hover + .activity {
  display: block;
}

Next, update your HTML structure as shown below;

<table class="table">
  <thead>
    <tr>
      <th>City</th>
      <th width="150px">Population</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>New York City</td> 
      <td>8,398,748</td>      
    </tr>
    <tr class="activity">
      <td>Explore Central Park and visit museums</td>
    </tr>
    <tr>
      <td>Los Angeles</td>
      <td>3,990,456</td>
    </tr>
  </tbody>
</table>

Feel free to test it out on this demonstration.

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

"Mute printing of a PDF file that is integrated within the document

I’m working on a web page that includes an embedded PDF file. Here’s a snippet of my code: <embed type="application/pdf" src="path_to_pdf_document.pdf" id="pdfDocument" width="100%" height="100%"> </embed> To print the ...

Position the Facebook Like Button in alignment with the counts box

Is it possible to align the Facebook likes count box at the bottom of my Like Button on my website? I'm looking for help with this task. Here is a visual representation of how I want the fb button to be positioned or styled: ...

Having trouble with Bootstrap form-inline stacking issue, any tips on how to stop them from stacking?

I've created a simple test navbar with a search input box and search button using the Bootstrap class form-inline. However, the search button is appearing below the search input, even though there is enough space available. You can view the codepen he ...

Showing information retrieved from a database using PHP in a sequential manner

I'm currently working on developing a webpage that features 6 rows of images, with each row containing 4 images accompanied by captions right below them. The structure I have in mind is as follows: IMAGE IMAGE IMAGE IMAGE TEXT TEXT TEXT TEXT IMAGE ...

Exploring Angular 4: A deep dive into routing and ensuring responsiveness

I set up an ubuntu server on AWS and most things are running smoothly, but there are a couple of issues that have me stumped. When visiting my web app, you can only interact with the buttons in the menu. Trying to access a link like 'mypage/items&ap ...

Changing the color of your device's background

I am experiencing difficulty in altering the background color of my popular posts widget on my website. Upon investigation, I discovered that the issue is associated with my sticky navigation menu. The background of my sticky navigation menu is black, and ...

Element with absolute position does not expand along with scrollable content (over 100%)

In my popup window, the fade element is supposed to stretch to the full width of the screen. However, when the content exceeds 100% of the browser window height, the element does not extend to the full page height. If I set html, body { height: 100%; over ...

List items that are not in any particular order spilling over onto the next

Having an issue with an unordered list where the list items aren't all on the same line. The 5th element is dropping to the next line even though each item is set to 20% of the container. The math adds up, so not sure what's causing the problem. ...

What is the reason for the additional space and irregular alignment of my divs when utilizing the 960 grid system?

One issue I'm facing is that elements within divs are aligning randomly without responding to any alignment tags. Additionally, some divs are creating extra space above or below their elements. I am using the 960 grid system and have not made any chan ...

Steps to display the datatable footer on the printed page

I am having trouble understanding the solutions provided for my table query. The current table setup is as follows: <table class="table table-bordered make_datatable"> <thead> <tr> <th>SL No</th> ...

How can I extract information from an HTML element using its ID in JavaScript?

Can anyone please help me retrieve the date using JavaScript from the following div: <div id="popupDateFieldMin" class="dateField">2017-08-02</div> Below is the code snippet I am currently using: var cal = document.getElementById( 'popu ...

The directive for Content Security Policy in Django framework

I am having trouble importing font-awesome into my application. I've tried the following code: <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> However, this is causing an err ...

JavaScript and CSS animations out of sync in terms of timing

I've been encountering some issues. I have an array containing 5 lines of text that change with a timer, but it seems that the css timer animation might not be synced properly or my @keyframes slidesdown setup could be incorrect. The delay between te ...

Enable a button or class to dynamically alter its color

Hi there! I'm new to coding and just started learning HTML and CSS. My question is: How can I change the color of buttons once they are clicked? <div class="icon-bar"> <a href="#"><i class="fa fa-search" ...

Activating an HTML button based on a specific condition

One of the challenges I faced was with two HTML buttons that I have set up: <button id="alarm-log-submit" class="btn btn-primary alarm-log-btn" >Generate Log</button> <button id="alarm-log-download" class="btn btn-primary alarm-log-second-b ...

Can CSS content be used to showcase an .html document or .html fragment?

Referencing the MDN documentation for css content: /* <uri> value */ content: url(http://www.example.com/test.html); Inquiry: Can an image be displayed using the url() value of the content property in a css html element? .content { conte ...

Concealing and revealing content using JQuery based on user input in

I'm attempting to create a feature where the visibility of a password field is determined by the value of another input element. For instance, when the username is set to "admin", the password field should be hidden. If any other value is entered in ...

Difficulty arises when trying to merge a dropdown menu with a horizontally scrolling menu

I am attempting to include a dropdown menu within a horizontally long menu. In order to achieve this, I have merged the script for displaying a scrollable menu with that of a dropdown menu. However, in doing so, the dropdown menu does not pop out from the ...

The text and links should appear transparent against a gradient background; however, they are displaying in white

I am currently facing a small issue while working on my new website, www.dvpwebdesign.com. The problem arises in Internet Explorer (IE) when the intro page displays a repeated gradient background with text and links overlaid on it. While the layout works ...

Submitting a particular entry in a form that has been dynamically generated through iteration

This code snippet pertains to the admin panel of a particular website. The page displays appointments requested by customers, allowing the admin to make changes based on availability. <?php while($row = mysqli_fetch_assoc($result)){ ?> <form ...