Enable vertical scrolling within the table body to display entire rows

Encountering an issue with using overflow-y inside tbody. Attempting to set the maximum height of the tbody results in it shrinking into a single column, as shown in image 1. Seeking assistance in resolving this problem. Thank you.

Below is the HTML code:

.item-list .item-detail{
    margin: 0 0 2px 4px;
    font-size: 1.25em;
}

.table > :not(caption) > * > * {
    padding: 0.3rem 0.3rem !important;
    background-color: var(--vz-table-bg);
    border-bottom-width: 1px;
    box-shadow: inset 0 0 0 9999px var(--vz-table-accent-bg);
}
.color-display{
    display: inline-block;
    width: 16px;
    height: 16px;
}
.text-center {
    text-align: center !important;
}

/* The codes i added*/

.tbody-responsive{
    display: block;
    max-height: 200px;
    overflow-y: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71131e1e05020503100131445f425f43">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b383f6edf0edf1">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>

<div class="item-list py-4">
  <div class="item-header">

  </div>
  <div class="item-body">
    <div class="row">
      <div class="col-12">
        <table class="table">
          <thead class="">
            <th>
              <h4><b>DETAIL</b></h4>
            </th>
            <th class="text-center">
              <h4><b>STOCK (Client)</b></h4>
            </th>
            <th class="text-center">
              <h4><b>STOCK (XXX)</b></h4>
            </th>
          </thead>
          <tbody class="tbody-responsive w-100">

            <tr>
              <td class="item-detail"> Polo SE, Red <span class="color-display" style="background-color: red"></span>, XL</td>
              <td class="item-detail text-center">1</td>
              <td class="item-detail text-center">1</td>
            </tr>
            
             <tr>
              <td class="item-detail"> Polo SE, Red <span class="color-display" style="background-color: red"></span>, XL</td>
              <td class="item-detail text-center">1</td>
              <td class="item-detail text-center">1</td>
            </tr>
            
            (more table rows here)
            
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

The outcome of the code

Desiring the width to be as shown in the image. Attempts with width:100% or w-100 class did not yield the expected result.

The expected width

Answer №1

Tables don't have the capability to set maximum heights due to their display:table property.

To work around this limitation, you can create a container div around the table, assign a max-height and overflow value to it, and remove display:block; from the thead and tbody-responsive elements. Additionally, you can make the headers sticky during scrolling by applying position:sticky; and top:0; to them as demonstrated below.

.item-list .item-detail{
    margin: 0 0 2px 4px;
    font-size: 1.25em;
}

.table > :not(caption) > * > * {
    padding: 0.3rem 0.3rem !important;
    background-color: var(--vz-table-bg);
    border-bottom-width: 1px;
    box-shadow: inset 0 0 0 9999px var(--vz-table-accent-bg);
}
.color-display{
    display: inline-block;
    width: 16px;
    height: 16px;
}
.text-center {
    text-align: center !important;
}


.tbody-responsive{
    max-height: 200px;
    overflow-y: auto;
    width: 100%;
}
thead{
    position: sticky;
    top: 0;
    width: 100%;
}
.container{
    max-height: 300px;
    width: 100%;
    overflow: scroll; 
}
body{
    width: 100vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e4e9e9f2f5f2f4e7f6c6b3a8b5a8b4">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05676a6a71767177647545302b362b37">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>

<div class="item-list py-4">
  <div class="item-header">

  </div>
  <div class="item-body">
    <div class="row">
      <div class="col-12">
        <div class="container">
        <table class="table">
          <thead class="">
            <th>
              <h4><b>DETAIL</b></h4>
            </th>
            <th class="text-center">
              <h4><b>STOCK (Client)</b></h4>
            </th>
            <th class="text-center">
              <h4><b>STOCK (XXX)</b></h4>
            </th>
          </thead>
          <tbody class="tbody-responsive w-100">

            <tr>
              <td class="item-detail"> Polo SE, Red <span class="color-display" style="background-color: red"></span>, XL</td>
              <td class="item-detail text-center">1</td>
              <td class="item-detail text-center">1</td>
            </tr>
            
             <tr>
              <td class="item-detail"> Polo SE, Red <span class="color-display" style="background-color: red"></span>, XL</td>
              <td class="item-detail text-center">1</td>
              <td class="item-detail text-center">1</td>
            </tr>
            
            // Additional table rows...

          </tbody>
        </table>
        </div>
      </div>
    </div>
  </div>
</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

Insert a design layout into a text entry box

Currently developing an application with AngularJS and seeking a solution for adding a "template" to an HTML input field similar to a placeholder. Specifically, I have a date-field requiring user input in the format of dd/MM/yyyy or through a datepicker se ...

Having issues with column offsetting in Bootstrap v4.0.0-beta

While using Bootstrap 4 Beta (Bootstrap v4.0.0-beta), I encountered an issue with offsetting columns. Previously, I used offset-md-* and it worked fine. However, this feature has been removed in BS4 Beta as per the documentation. The new method suggested i ...

Encountering a problem while deploying Rails 6 on Heroku: ModuleNotFoundError - The module '@popperjs/core' cannot be found, resulting in an error message stating it can't be resolved in the directory '/tmp'

I've integrated Bootstrap into my Rails 6 blog-like app and encountered a compilation issue when trying to deploy on Heroku. The error message reads: ModuleNotFoundError: Module not found: Error: Can't resolve '@popperjs/core'.... If ...

Save user entries in a database array

I'm working on developing a platform for advertisements where users can create detailed profiles with images. To achieve this, I need to store the information in an array within a backend database for future retrieval. Below is an example of the backe ...

The functionality of Jquery AJAX is operational, however, the message being displayed appears to

Inside the file changename.php, there is a DIV element: <div id="resultDiv"></div> Within the same file, PHP code can be found: <?php include_once ('connect.php'); if(isset($_POST['name'])) { $postedname = $_POST[&ap ...

What is the best way to display cards next to each other on desktop and mobile devices while maximizing available space?

In the world of React components, there exists a card representation within a grid view that adapts based on available screen space; function EngineerCard(engineer: EngineerDetail) { return ( <div className='engineerCard'> <d ...

Half-extended Bootstrap 4 container reaching the edge of the page

I'm looking to create a layout with a .container that consists of two distinct halves. The left half should function as a standard .col-6, while the right half should extend all the way to the edge of the screen, similar to a .col-6 within a .contain ...

Transitions fail to appear correctly when the page first loads

I need to fill a gauge meter based on a variable sent to the html file. To achieve this, I have initially set the value to zero: transform:rotate(.0turn); transition: all 1.3s ease-in-out; However, when the HTML is first loaded or refreshed (with F5 ...

Undefined boolean when passing to AngularJS directive

I developed a custom directive that allows for the addition of a gradient background color in AngularJS: .directive('colouredTile', function () { return { restrict: 'A', controller: 'ColouredTileController&apos ...

Exclusive Pages in NextJS for Development Purposes

I want to set up a /storybook page specifically for development purposes. However, I do not want this page to be accessible in production mode. How can we make that happen? ...

Set the background image width to 75% of the screen size

I want to use this image https://i.sstatic.net/gt30c.jpg as a background for a section on my page. My preference is to have the white part cover only 75% of the screen, stopping before reaching the content in the left grid. Is it possible to achieve this ...

Accordion Element using HTML and CSS

Seeking assistance with a website project! I am in need of help in implementing the following feature: I would like the "About" link to expand into a panel when clicked, and retract back when the user clicks "hide" within the panel. Check out the attached ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

Failing to upload a file along with other form elements in Flask results in a 400 error

Encountering a 400 error when attempting to upload a file and send other form elements to Flask from HTML. Tried using AJAX, but it also gives me an error. Python: @app.route('/prod_diff_result', methods=['POST']) def prod_diff_result ...

Email Coding Problem

This email serves as a confirmation from a MailJet subscription widget. The issue at hand is that the body of the email appears too wide in Outlook, but the header is responsive and displays correctly. I have made several attempts such as setting fixed wid ...

What's the best way to use the keyboard's enter key to mark my to-do list

I'm looking to update my todo list functionality so that pressing enter adds a new todo item, instead of having to click the button. <h1 style="text-align:center">Todo List</h1> <div class="container"> ...

Developing SAPUI5: Construct a GenericTile featuring a centrally positioned icon

I'm having trouble creating a custom tile in SAP that inherits from sap.suite.ui.commons.GenericTile and only displays an icon centered in the middle of the tile. The standard aggregations and properties are causing issues as they position the icon on ...

What is the process for including a JavaScript file in an HTML document?

Greetings to all and thank you for taking the time! I have a straightforward query for you.. I have designed an html page featuring only a basemap (open street map). Moreover, I possess a javascript file which utilizes your coordinates to calculate a perce ...

Issue with if statement when checking element.checked

Hey everyone, I'm currently working on a calculator app and running into an issue. I have set up 3 radio buttons and I would like to check them using an 'if statement' in my JS file. However, the problem is that the 'main' element ...

What exactly does the <cbn-root> HTML element signify, and how can it be parsed using Java programming language?

I attempted to develop a Java program to monitor the availability of reserved spots on this website: However, when I examine the page source using Chrome or Edge, only <cbn-root></cbn-root> is displayed in the body section. Yet, utilizing Chro ...