Creating a scrollable HTML table

I'm having trouble getting my table to scroll vertically. Can someone provide assistance? Here is the HTML code I am using:

<div class = scroll>
            <table border="1">
             <tbody>
                <tr>
                    <th>Number</th>
                    <th>Name</th>

                </tr>
                <tr>
                    <td>
                    5
                      </td>
                    <td>
                    Hello
                     </td>
                </tbody>
            </table>
            </div> 

Additionally, here is my CSS code:

.scroll{
    overflow:scroll;

}

Answer №1

Adjust the height property within your CSS stylesheet

#table-wrapper {
  position: relative;
}

#table-scroll {
  height: 120px;
  overflow: auto;
  margin-top: 20px;
}

#table-wrapper table {
  width: 100%;
}

#table-wrapper table * {
  background: yellow;
  color: black;
}

#table-wrapper table thead th .text {
  position: absolute;
  top: -20px;
  z-index: 2;
  height: 20px;
  width: 35%;
  border: 1px solid red;
}
<div id="table-wrapper">
  <div id="table-scroll">
    <table>
      <thead>
        <tr>
          <th><span class="text">A</span></th>
          <th><span class="text">B</span></th>
          <th><span class="text">C</span></th>
        </tr>
      </thead>
      <tbody>
        <!-- More table rows here -->
      </tbody>
    </table>
  </div>
</div>

Answer №2

To enable vertical scrolling for a table, you need to specify a height for the container with the "scroll" class. Here's an example:

.scroll{
    overflow-y:scroll;
    max-height: 100px;
    max-width: 200px;
}
table {
  width: 100%;
}
<div class = scroll>
  <table border="1">
    <tbody>
      <tr>
        <th>Number</th>
        <th>Name</th>
      </tr>
      <tr>
        <td>5</td>
        <td>Hello</td>
      </tr>
      <tr>
        <td>5</td>
        <td>Hello</td>
      </tr>
      <tr>
        <td>5</td>
        <td>Hello</td>
      </tr>
      <tr>
        <td>5</td>
        <td>Hello</td>
      </tr>
      <tr>
        <td>5</td>
        <td>Hello</td>
      </tr>
    </tbody>
  </table>
</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

node js returning undefined value

I'm currently working on configuring my node.js to retrieve and display the value submitted through an HTML form. Here's a snippet of the node.js file: const app = express(); var path = require('path'); var fs = require('fs' ...

Displaying a base64 image in a new tab with JavaScript on iOS devices

Currently, I am receiving base64 data for an image and would like to open it in a new tab. To achieve this, my code looks something like this: var window = new window(); window.open("<iframe src="+base64Url+"); Interestingly, this method works perfec ...

As the page is resized or zoomed in and out, the div elements start shifting in different directions

When I apply position:absolute to prevent the elements from moving when zooming in and out of the page, they all end up congregating in one place. Can anyone help me with this issue? None of the solutions I've found have worked so far. I'm develo ...

Guide to displaying a loading image when selecting an item from a dropdown menu using JavaScript

When using three drop-down lists and performing an AJAX call on each dropdown list's onclick function, I encountered a delay in loading the data. To address this issue, I attempted to display a loading image while processing the AJAX call. <form ...

Aligning content vertically with a floating element in a design that adapts to different

I am facing a challenge with my component box layout, where I need the image to be aligned on the left or right with the content vertically centered. Additionally, it should be responsive for mobile, with the image on top and content at the bottom. Check o ...

Ensuring the scroll bar is consistently positioned at the bottom

I'm currently working on a chat application using Vue.js and I'm facing an issue with the scroll bar. Despite using overflow: auto; and customizing the scroll bar, it doesn't automatically scroll to the bottom whenever a new message is sent. ...

A Guide to Mapping JSON Status for Handling Stale Elements in Selenium with Java

I encountered a stale element error while running a script that I wrote. Although I understand the problem, I am unsure about how to fix it. My script aims to execute the following tasks on FireFox: 1) Launch google.com, 2) type "pluralsight", 3) submit, ...

Issue: Java error - unrecognized flag: -release

I utilize Java : Java(TM) SE Runtime Environment (build 9-ea+143) Java HotSpot(TM) 64- Bit Server VM (build 9-ea+143, mixed mode) and IntelliJ IDEA: IntelliJ IDEA 2016.2.5 Build #IU-162.2228.15, built on October 14, 2016 Error encountered during compil ...

Unable to find the Java image file location

Currently utilizing Eclipse, here is the code snippet I have: package arrowRecog; import org.sikuli.api.*; import java.io.File; import arrowRecog.res.*; public class FocusTrain { public static void main(String[] args) throws Exception { ...

The progress bar on my browser disappears when I click a link in my Next.js (React) application

I'm currently working on an app that uses Strapi as the backend and Next.js as the frontend. In my frontend, I have multiple links that are styled in a certain way. However, when I click on these links, there is a delay of about 2 seconds before the ...

Place element H1 below H2 on the webpage, but keep it positioned above H2 in the underlying

I have a snippet of HTML Code: <html> <head> <title>css test</title> <style type="text/css"> .box{width:100%;float:left;background:red} </style> </head> <body> <h1>Title To Appear Below Div on Page< ...

Determining if a division results in an integer or a float in Java

Struggling to come up with a suitable heading. Here is the issue I am facing: I have an "int i," which can be any numerical value. My objective is to convert this "int i" into the nearest number that is divisible by 16. For instance, let's say i = 33 ...

Can the HTML of a link's target be altered after the link has been clicked without relying on a browser extension?

I am seeking a way to click this link and have it automatically change the text in all the buttons to "Click me!". The following code accomplishes this using jQuery 2.1.1, but despite my online search efforts, I have not found a method to achieve this afte ...

Unleash the power of CSS3 to style this button

I received an image of a button that I need to replicate on a website, but unfortunately, only the image was provided without any information about the font-family or size. Despite not being a designer or CSS expert, I attempted to create a CSS style that ...

Is there a way to modify the text color within the thumb-label of the Vuetify v-slider component?

Lately, I've been facing some challenges and my objective is to change the color of the thumb label on my v-slider to a custom one that is defined in the component's design. Can anyone provide guidance on how to achieve this? Regards, Joost ...

The anchor hover state will persist even after clicking the link

What if I need to create navigation top menu buttons with anchor tags, each having a specified href? I've already styled the hover effect for each button, but when I click on a link, the hover effect disappears. How can I ensure that the hover effect ...

Updating checkbox Icon in Yii2

Is there a way to customize the checkbox icon when it is checked in Yii2? Currently, I am adding a checkbox inside a div along with an icon: <i class="fa fa-check-circle icon-check-circle"></i>. However, the checkbox shows a default check symbo ...

Determine the frequency of a specific value occurring in a two-dimensional array and then record that count in a separate one-dimensional array

Despite my extensive search on Stack Overflow and Google, I have been unable to find a solution that fits my specific needs. I am looking to create a simple for loop that can cycle through the rows of a 2D array filled from a file and identify instances wh ...

Incorporate JSON data to display SVG images

As a beginner in web development, I have been honing my skills with the AngularJS framework due to its user-friendly nature. Currently, I'm working on pulling JSON data from an API using $http.get method. One of the fields contains an image source i ...

Is it possible to dynamically change an ngModel value directly from the component?

I'm currently immersed in an Angular project and my initial file setup was like this: dog.ts: export interface Dog { name: string; age: number; breed: string; } dog.component.ts: import { Dog } from '../dog'; @Component({ //setup ...