Scrollable table body with a stationary table header

Is there a way to create a table with a fixed height and a scroll bar for the content, while still allowing the table width to adjust accordingly? I've tried using display: block, but it seems to freeze the width of the table. Any suggestions on how to make the table width full?

https://i.sstatic.net/FEsgh.jpg

HTML :

   <table class="table table-striped table-hover" id="item_table">
       <thead>
        <tr class="bg-primary">
         <th>#</th>
          <th>Product Name</th>
          <th>Quantity</th>
          <th>Unit Price</th>
          <th>Discount</th>
          <th>Amount</th>
          <th><i class="fa fa-close"></i></th>
         </tr>
   </thead>
   <tbody id="item_list">
     <tr>
       <td>1</td>
       <td>Mens Full Sleeve Shirts</td>
       <td>1</td>
       <td>2200.00</td>
       <td>200.00</td>
       <td>2000.00</td>
       <td><span><i class="fa fa-trash"></i></span></td>
     </tr>
   </tbody>
</table>

CSS :

#item_table{
    display: table-row;
    table-layout:fixed;
}

#item_list{
    width: 100%;
    display: block;
    table-layout:fixed;
    height: 170px;
    overflow-y: scroll;
}

Answer №1

Avoid applying display:block to a table as it can have detrimental effects on its structure.

Instead, enclose the table within a div and utilize overflow:auto on this particular element.

If working with bootstrap, consider adding the table-responsive class to the container, as demonstrated in the documentation here

Answer №2

Greetings! I suggest giving this a try:

tbody {
    height: 100px;       /* Specifically for the demo          */
    overflow-y: auto;    /* Initiates vertical scrolling    */
    overflow-x: hidden;  /* Conceals the horizontal scroll bar */
}

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

Issue with flash installation

I'm currently working on a WordPress website at www.studium.cl. However, I've encountered an issue when trying to open it in Internet Explorer. Each time I try to access the site, a window pops up prompting me to install Adobe Flash Player. Even ...

Ways to eliminate the final column border coded in php?

My code for generating the output is as follows: <?php function HailstoneNumbers($x){ echo '<tbody>'; static $c=0; echo '<tr style="border-bottom:1px solid ...

Display endless data within a set window size

I am looking to create a fixed-size window for displaying text from the component <Message/>. If the text is longer, I want it to be scrollable within the fixed window size. See screenshot below: Screenshot export default function AllMessages(){ ...

CSS to Vertically Display Input Values

Having an input field with type=number in a React application styled using styled-components, I am looking to have certain inputs display their values vertically. export const UnitValue = styled.input` ::-webkit-inner-spin-button, ::-webkit-outer-spin ...

Maintaining Portrait Lock for Viewport in HTML5/CSS3: A Guide

Can the orientation of a mobile device's view port be locked to portrait mode? I tried searching for a solution on Google, but was unable to find clear instructions on how to achieve this. ...

Creating an infinite loop of SVG rectangles pulled from a database

As I extract svg rectangles from a database using Python, I find myself hardcoding the process in order to change the colors of each rectangle in my CSS style sheet. With potential scalability issues in mind - if there were 100 rectangles - I am wondering ...

Flexbox causing overflow in parent container due to max-height set to 100%

In my HTML code, I have the following elements: .modal - represents a page modal with a height limit of 30vh. .wrapper - serves as a container that wraps the modal content, consisting of an input (in this case, a search input) and a list of items. ...

JavaScript button event listener failing to display information

I have been developing a Tennis Club Management sample application using javascript, html, css, and bootstrap. The application consists of a login page and a profile page. In the profile page, I have implemented a sidebar with anchor tag links that navigat ...

ReactJS incorporates multiple CSS files

I am currently working on developing a Single Page Application using ReactJS. However, I am facing an issue with styling. Currently, I have created 3 different pages (with the intention of adding more in the future), each having its own CSS file that is im ...

Is it possible to move the scrollbar of an entire page to the left side?

Is it feasible to shift a scrollbar of a div to the left side using simply direction:rtl? Why doesn't this same method work for the entire page? I attempted applying the same CSS rule to both html and body, but it had no effect as the main scrollbar ...

Creating Dynamic HTML/Javascript with a Click of a Button

I am facing a challenge with implementing a callback function on a web page that triggers when a deposit is made. The issue arises from the fact that users have the freedom to add various elements such as scripts, images, iframes, etc., in the backend. My ...

The Angular (click) event requires two clicks in order to trigger the associated Typescript function

I'm facing an issue with a Typescript function that I've linked to (click) in my HTML. Oddly, I have to click the button twice for the function to be executed. Interestingly, if I don't provide any parameters, the function works as expected ...

Display HTML element only if the class is not currently visible

Recently, I created a dynamic web page using jQuery which involves the hiding and showing of elements based on their classes. For example: <p class='lookup'>You can lookup all the values.</p> If a user is unable to perform a lookup ...

Is there a way to use JQuery to make one button trigger distinct actions in two different divs?

For instance: Press a button - one div flies off the screen while another div flies in. Press the button again - Same div flies out, other div returns. I'm completely new to Javascript/JQuery so any assistance would be highly appreciated! Thank you ...

Notification fails to display following POST request

Whenever the select menu displays "Please select your country" and I click the checkout button, an alert pops up. But if I select Canada, then go back to selecting "Please select your country" and press checkout, no alert appears. I need the alert to alway ...

Difficulty with setting a border for text spanning multiple lines

I am trying to style a header text with a border around it. The issue arises when the larger text spans over two lines, causing the border to break in between. In an attempt to fix this, I applied the CSS property box-decoration-break: clone;. However, thi ...

What is the method to retrieve an object by using a variable containing a string?

I am facing an issue with linking object names and anchor titles in my code. I want to store the title of an anchor element clicked by the user in a variable called sprite, which should then correspond to the name of an object. Here is the current version ...

Tips for incorporating transitions into CSS styling

After successfully adding a picture over an element when hovering over it, I decided to include a transition: 0.2s; property but unfortunately the transition effect is not working as expected. Why is this happening? My intention is for the picture to smoot ...

Having trouble with input functionality on iPad due to a CSS, LI, div, or clipping issue?

https://i.sstatic.net/muMSG.png One challenge I am facing is related to several inputs housed within an LI and div that are sortable using jQuery. The problem arises specifically on the iPad when attempting to click into the inputs to enter information - ...

Can Bootstrap 4 CSS be used to display a yellow triangle with text in the bottom right corner of a card?

I'm having difficulty modifying the CSS to display a yellow triangle containing text in the bottom right corner of a Bootstrap 4 card. Is there a way to achieve this effect using CSS? This is how the current CSS output looks in a Bootstrap 4 card: h ...