Issue with table not scrolling vertically

My goal was to create a table with a vertical scroll bar that only displays the first 5 data entries, using CSS for styling. However, despite declaring the scroll in the CSS code, it doesn't seem to be working properly. I attempted to implement a fixed header table with a scrolling tbody section; when additional rows are added to the table, only the tbody should scroll. To address different row lengths and ensure proper alignment of data, I utilized percentage-based widths for each column. Here is an example code snippet:

<!DOCTYPE html>
<html lang="en>"
   <head>
      <meta charset="utf-8">
      <title></title>
      <meta name "viewport" content="width=device-width initial-scale=1">
      <link href"http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css rel="stylesheet">
      <!-- Theme CSS -->
      <link href"css/style.css" rel="stylesheet">
      <!-- Favicon -->
      <link rel="shortcut icon" href="img/favicon.ico ">
      <script
         src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
      <script
         src"https//ajax.googleapis.com/ajax/libs/jQuery/3.3.1/jquery.min.js "></script>
      <scriPTextStyle type"text/css ">'
      
        table {
            width: 800px;
            position: relative;
        }
        
        thead { 
            display: block;   
            overflow-y: auto;
            height: 30px;
            max-width: 100%;
            top: 0;
            font-size: 12px;
        }
        
        tbody {
            max-width: 100%;
            position: absolute;
            top: 40px;
            height: 2em;
            overflow-y: scroll;
            display: block;
            font-size: 10px;
        }
        
        </style>"
      </style>
    </head>
    
    <div class="container">
        <table class="scroll table table-bordered ">
            <thead>
                <tr>
                    <th style="width: 2.5%; font-size: 12px;">Sl No</th>
                    <th style="width: 3%; font-size: 12px;">First Name</th>
                    <th style="width: 10%; font-size: 12px;">Middle Name</th>
                    <th style="width: 10%; font-size: 12px;">Last Name</th>
                    <th style="width: 10%; font-size: 12px;">Email</th>
                    <th style="width: 10%; font-size: 12px;">Department</th>
                </tr>
            </thead>

            <tbody>
                <!-- List of data entries here -->
            </tbody>
        </table>
    </div>
   </body>
</html>

Answer №1

Below is a code snippet that will add a vertical scroll to your content:

.scroll {
  height: 100px;
  overflow-y: auto;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
  <!-- Theme CSS -->
  <link href="css/style.css" rel="stylesheet">
  <!-- Favicon -->
  <link rel="shortcut icon " href="img/favicon.ico ">
  <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js "></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js "></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js "></script>
  <style type="text/css ">
    table {
      width: 800px;
      position: relative;
    }
    
    thead {
      display: block;
      overflow-y: auto;
      height: 30px;
      max-width: 100%;
      top: 0;
      font-size: 12px;
    }
    
    tbody {
      max-width: 100%;
      position: absolute;
      top: 40px;
      height: 2em;
      overflow-y: scroll;
      display: block;
      font-size: 10px;
    }
    
    table {
      width: 100%;
      /* Optional */
      /* border-collapse: collapse; */
      border-spacing: 0;
      border: 2px solid black;
    }
    
    table tbody,
    table thead {
      display: block;
    }
  </style>
</head>
<div class="container ">
  <div class="scroll">
    <table class=" table table-bordered  ">
      <thead>
        <tr>
          <th style="width: 2.5%;font-size: 12px; ">Sl No</th>
          <th style="width: 3%;font-size: 12px; ">First Name</th>
          <th style="width: 10%;font-size: 12px; ">Middle Name</th>
          <th style="width: 10%;font-size: 12px; ">Last Name</th>
          <th style="width: 10%;font-size: 12px; ">Email</th>
          <th style="width: 10%;font-size: 12px; ">Department</th>
        </tr>
      </thead>
      <tbody>
        <!-- Table Body Content Here -->
      </tbody>
    </table>
  </div>
</div>
</body>

</html>

Answer №2

In my opinion, the best approach would be to utilize two tables, as demonstrated in this example.

 <!-- Table for header -->
 <table class="table table-bordered mb-0">
   <thead>
     ~
   </thead>
 </table>

 <!-- Division for scroll -->
 <div class="scroll-area">
   <!-- Table for data -->
   <table class="table table-bordered">
     <tbody>
       ~
     </tbody>
   </table>
 </div>

This method offers great flexibility, but may not be ideal if you need to accommodate a scrollbar.

For a simpler layout, consider using position: sticky.

  position: sticky;
  top:0;

I hope these suggestions prove helpful to you.

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

Center-aligned Bootstrap column with a width of 50% and centered vertically

I am looking to have images and videos displayed side by side on my webpage. The code below functions properly, however there is an issue with the alignment. While the horizontal center alignment is correct, the video and images are not centered vertically ...

Issue with Jquery text visibility on Internet Explorer version 7.0

When trying to display text on my modal window, I encountered an issue. The text is not visible initially, but appears when I highlight it with the mouse. Strangely, clicking anywhere on the screen makes the text invisible. This problem is specific to IE 7 ...

.load() not triggering for images that are not in the cache - jquery

In Safari and Opera, the callback function of .load doesn't wait for uncached images to be fully loaded. With cached images, it works perfectly fine. The code may seem a little complicated, but I'll do my best to simplify it... So, here is my J ...

Interactive image map with hover effects and external image swapping placed beyond the container

I've encountered a problem with a responsive image map and rollovers. Using Matt Stow's responsive image jQuery plugin ensures that coordinates are responsive, and clicking on an area brings up Lightview as expected. However, the issue arises whe ...

Utilizing JavaScript Classes to Implement Multiple CKEditor Instances

I have been attempting to utilize CKEDITOR for editing the content on my website. However, I am unsure about how to handle multiple instances of CKEDITOR. I have encapsulated all the JavaScript functions in a Class, but I need assistance in determining whi ...

Delete an item from an array based on its index within the props

I am attempting to remove a specific value by its index in the props array that was passed from another component. const updatedData = [...this.props.data].splice([...this.props.data].indexOf(oldData), 1); const {tableData, ...application} = oldData; this ...

Asynchronously parsing CSV files in NodeJs using the `await` keyword within the `on('data')`

I have a specific code snippet that is designed to read lines from a .csv file one by one and then store each processed row into a database const csv = require('csv-parse') const errors = [] csv.parse(content, {}) .on('data', async ...

Control the movement of the mouse pointer using javascript

For my University presentation on click-jacking, I came across an intriguing example that I wanted to replicate but didn't know where to start. The whole concept seemed very complex to me. To get a better idea, please take a look at this video: https ...

Is it possible to drag and drop content *FROM* a web browser?

While dragging items into a browser is a well-documented process, HTML5 has made it even easier. However, my curiosity lies in the possibility of dragging and dropping out of a browser. In this hypothetical situation, there would be a webpage element (su ...

Filtering data attributes in jQuery for multiple elements

I have a variety of cars listed for sale on my website, along with five different filters: Make Model Year Mileage Price There are multiple options under each filter that users can select to refine their search. When users choose one or more options wit ...

The AJAX validation process fails to run prior to the execution of the login PHP script

My attempt to implement AJAX for form validation is not successful and I'm unsure why. Despite my efforts, the form still redirects to login_action.php instead of performing the AJAX validation as intended. I have designed a modal login form and wish ...

What is the best way to solve the problem of Chrome auto-complete overlapping with labels in Vuetify?

When attempting to make a login form using outlined text fields in Vutify, there is an issue with Chrome autocomplete overlapping the labels. <v-text-field v-model="email" label="e-mail" name="email" outlined prep ...

When using VueJS checkboxes with object values bound to an array property, they do not get removed from the array when unchecked

Within my VueJS component, I am utilizing an array named selectedJobs to track checked checkboxes in an HTML table. The data displayed in the table is sourced from an array of objects called replenJobsList. /* My Component */ <template> ...

Acquire a personalized WCF entity using Json representation

[SOLVED] I recently created a test project to help me understand the process of working with WCF objects and Json. If you have any valuable resources or tutorials on this topic, please share them with me. My current challenge involves retrieving a custom ...

Issues have arisen with the functionality of IE and jQuery posts

When using jQuery ajax to post to my backend, everything works smoothly in browsers except for Internet Explorer (IE 11). Is there something I am missing specifically for IE? var example = "example/1234"; jQuery.post('#{test_path}', { uri: ...

After a period of 10 minutes with no activity, proceed to the next page

I am in the process of developing a custom local website that will be displayed on a large touch screen at my current workplace. Only one user can interact with it at a time. My client has requested a screensaver feature to appear after 10 minutes of no i ...

JavaScript can extract a portion of an array

Is it possible to generate a new array consisting of all elements ranging from the nth to the (n+k)th positions within an existing array? ...

The HTML footer elegantly hovers just above the bottom of the page, at specific resolutions

Preparing my website for a college interview, but struggling to figure out why the footer is floating above the bottom of the page when the window size is above a certain point. Below are the HTML and CSS snippets used: html,body{ width: 100%; h ...

What is the best way to incorporate multiple conditions within a React component?

When working in React, I have the ability to conditionally render any div using the following code snippet: {hasContent && <span>{value}</span> } Recently, I attempted to include two conditions as follows: {hasContent || hasDesc &am ...

Issue with dropdown functionality in Hartl 3rd edition chapter 8, section 2.3

When I click on the "Account" link in the navbar, the dropdown menu is not appearing. Instead, a # is being added to the end of the URL, turning users/1 into users/1#. A similar question regarding the dropdown issue has been posted here: ruby on rails cha ...