What is the best way to implement a fixed header within a table?

Greetings everyone! I am looking to keep the thead header fixed at the top and allow only the tbody values to scroll within the table. Currently, when scrolling the page, everything in the table scrolls, but I aim to have only the tbody values scroll. Check out my Demo.

Markup

<tr ng-repeat="sryarnorder in sryarnorder.colorshades">
        <td>{{$index+1}}</td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">
                   <div style="text-align:center;" ng-repeat="dyedyarnreferencenumber in dyedyarnreferencenumbers | filter:sryarnorder.color">
            <p>{{dyedyarnreferencenumber.shade}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">
               <div style="text-align:center;" ng-repeat="dyedyarnreferencenumber in dyedyarnreferencenumbers | filter:sryarnorder.color">
            <p>{{dyedyarnreferencenumber.buyers_reference}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">
                   <div style="text-align:center;" ng-repeat="dyedyarnreferencenumber in dyedyarnreferencenumbers | filter:sryarnorder.color">
            <p>{{dyedyarnreferencenumber.approved_supplier_ref}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">
                   <div style="text-align:center;" ng-repeat="dyedyarnreferencenumber in dyedyarnreferencenumbers | filter:sryarnorder.color">
            <p>{{dyedyarnreferencenumber.category}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">

            <p>{{sryarnorder.order_quantity}} {{sryarnorder.order_quantity_unit}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">

            <p>{{sryarnorder.price_per_kg_currency}} {{sryarnorder.price_per_kg}}</p>
        </div> </td>
            <td> <input type="text" ng-model="sryarnorder.color" style="display:none;">

            <p>{{sryarnorder.order_quantity * sryarnorder.price_per_kg}}</p>
        </div> </td>
    </tr>

Styling

table tbody, table thead
{
    display: block!important;
}

table tbody 
{
   overflow: auto!important;
   height: 100px!important;
}

table {
    width: 100%!important;
}
th
{
    width: auto!important;
}

Data Example

$scope.sryarnorders = [{"_id":"573d7fa0760ba711126d7de5","user":{"_id":"5721a378d33c0d6b0bb30416","displayName":"ms ms"},"__v":1,"colorshades":[{"_id":"573d7fc3760ba711126d7de6","price_per_kg_currency":"Inr","price_per_kg":"2","order_quantity_unit":"kg","order_quantity":"23","color":"56ffc46dab97a73d1066b792","quality":"Home Textiles","count":"yarn count"}],"created":"2016-05-19T08:56:00.997Z","remarks":"approved","actual_delivery_date":"2016-05-19","ex_india_date":"2016-05-19","ex_factory_date":"2016-05-19","lc_details_date":"2016-05-19","lc_details":"tooo much","po_delivery_date":"2016-05-19","sales_contract_date":"2016-05-19","sales_contract":"bioler","purchase_order_no_date":"2016-05-19","purchase_order_no":"1234","supplier_name":"Fsa","buyer_name":"e21"},{"_id":"56ffc6d5ab97a73d1066b798","user":{"_id":"56ff7bece2b9a1d10cd074a3","displayName":"saravana kumar"},"__v":1,"colorshades":[{"_id":"56ffc723ab97a73d1066b799","price_per_kg_currency":"Inr","price_per_kg":"120","order_quantity_unit":"kg","order_quantity":"25","color":"56ffc46dab97a73d1066b792","quality":"Home Textiles","count":"yarn count"}],"created":"2016-04-02T13:19:17.746Z","remarks":"pending","actual_delivery_date":"2016-04-02","ex_india_date":"2016-04-04","ex_factory_date":"2016-04-02","lc_details_date":"2016-04-02","lc_details":"lc","po_delivery_date":"2016-04-02","sales_contract_date":"2016-04-02","sales_contract":"required","purchase_order_no_date":"2016-04-02","purchase_order_no":"125","supplier_name":"Fsa","buyer_name":"Binary hand"},{"_id":"56ffc5e0ab97a73d1066b796","user":{"_id":"56ff7bece2b9a1d10cd074a3","displayName":"saravana kumar"},"__v":1,"colorshades":[{"_id":"56ffc608ab97a73d1066b797","price_per_kg_currency":"usd","price_per_kg":"5","order_quantity_unit":"kg","order_quantity":"20","color":"56ffc46dab97a73d1066b792","quality":"yarn quality","count":"yarn count"}],"created":"2016-04-02T13:15:12.876Z","remarks":"clear","actual_delivery_date":"2016-04-02","ex_india_date":"2016-04-02","ex_factory_date":"2016-04-02","lc_details_date":"2016-04-02","lc_details":"free","po_delivery_date":"2016-04-02","sales_contract_date":"2016-04-02","sales_contract":"required","purchase_order_no_date":"2016-04-02","purchase_order_no":"12345","supplier_name":"Fsa","buyer_name":"e21"}];
});

Answer №1

Give this a shot: https://codepen.io/tjvantoll/pen/JEKIu

(Ensure you specify fixed column widths)

HTML:

<table class="fixed_headers">
    <thead>
        <tr>
            <th>Name</th>
            <th>Color</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Apple</td>
            <td>Red</td>
            <td>These are red.</td>
        </tr>
        <!-- more content -->
    </tbody>
</table>

CSS:

.fixed_headers {
  width: 750px;
  table-layout: fixed;
  border-collapse: collapse;
}
.fixed_headers th {
  text-decoration: underline;
}
.fixed_headers th,
.fixed_headers td {
  padding: 5px;
  text-align: left;
}
.fixed_headers td:nth-child(1),
.fixed_headers th:nth-child(1) {
  min-width: 200px;
}
.fixed_headers td:nth-child(2),
.fixed_headers th:nth-child(2) {
  min-width: 200px;
}
.fixed_headers td:nth-child(3),
.fixed_headers th:nth-child(3) {
  width: 350px;
}
.fixed_headers thead {
  background-color: #333;
  color: #FDFDFD;
}
.fixed_headers thead tr {
  display: block;
  position: relative;
}
.fixed_headers tbody {
  display: block;
  overflow: auto;
  width: 100%;
  height: 300px;
}
.fixed_headers tbody tr:nth-child(even) {
  background-color: #DDD;
}
.old_ie_wrapper {
  height: 300px;
  width: 750px;
  overflow-x: hidden;
  overflow-y: auto;
}
.old_ie_wrapper tbody {
  height: auto;
}

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

jQuery event triggers upon completion of execution

A custom code has been integrated into my project using jQuery. Check out the code snippet below: <script> $("#startProgressTimer").click(function() { $("#progressTimer").progressTimer({ timeLimit: $("#restTime").val(), onFinish ...

Is there a way to create an interactive animation where an image responds to the movement of the mouse, but does not directly follow it

Looking to create an interactive experience where images on the webpage move in response to mouse movement. For example, if an image is at coordinates (0,0) and the mouse moves from (32,45) to (32,47), the image should shift vertically by 2 pixels. I atte ...

Modification in background when PNG image is hovered upon

Having an issue in Chrome that doesn't occur in Firefox. When hovering over a PNG within a carousel plugin or dragging an element with Jquery, the hover image briefly distorts the background. Looking for a solution to improve user experience. Let me k ...

"Having trouble getting `justify-between` to work in a tailwind CSS on an absolute

I'm currently using Tailwind CSS and I'm trying to position my navbar on top of an image. The image is set in a relative position and the navbar is set in an absolute position. Surprisingly, when the navbar is not set in the absolute position, I ...

Adding a linear gradient with a fade effect to Highcharts sparkline chart: Step by step guide

I am working with a Highcharts sparkline chart, and the design in my Figma file resembles the image provided below. I would like to customize the chart by adding transparency and a linear-gradient effect. Can anyone provide guidance on how to achieve this? ...

The section cannot be shown in a flex layout within a grid container

My goal is to showcase a grid layout containing multiple sections within a div, but I'm encountering an issue where the sections are only displaying as blocks. I am seeking assistance in making the second portion of the data appear flexed. Currently, ...

Tips for properly showcasing images on a webpage after ensuring all other content has loaded

Is there a way to have an image load last on a webpage after all other content has been loaded? I have an image that is retrieved from a database when a button is pressed, but I would prefer for the entire page to load first and then display the image. C ...

Analyzing the status of HTML resources in Google Chrome's DOM analyzer, comparing the cache version to the "not

When using Google Chrome, I navigate to a URL of an ASP.NET website after activating the DOM inspector (F12). By selecting the Network tab, I am able to view the requested resources along with their status, timeline, and more. Upon subsequent requests, som ...

From gathering user input on an HTML form, processing it using PHP, storing the data in a MySQL database, retrieving it with PHP, converting it to JSON

Description of issue: I am attempting to retrieve a value from an HTML input using PHP and execute a query on a MySQL database: SELECT * FROM WHERE (value = ID in the HTML input). MY APPROACH: I have created an HTML input: <input id="akt_djubrenje" ...

Troubleshooting: Issue with fixed positioning in React (Next.js) causing problems with modal window functionality

I'm currently working on implementing a modal window in React, but I've encountered an issue where setting position: fixed results in the modal window's position being relative to the page rather than the browser window. For privacy reasons, ...

conducting user verification in a REST-based application

As I explored methods for authentication in applications that primarily operate using RESTful APIs, particularly MEAN stack apps, where the backend (Node + express) and front-end (Angular) are completely separate with no connections between them. The meth ...

What events occur before they are detectable in JavaScript?

Below is a code snippet for handling events related to an image: HTML <!DOCTYPE html> <html> <head> <title>Image Event Handling</title> </head> <body> <img src="someImagePath.jpg" class="img-1"/> &l ...

Error message "Module 'ngFacebook' not found! Please check ngFacebook service" occurred when attempting to use ngFacebook service

Greetings, I am currently working on integrating Facebook with my Angular JS web application. To achieve this, I have employed the ngFacebook service in Angular JS. However, upon running my application, I encountered an error: "Uncaught Error: [$injector: ...

Aligning the text in the middle of a button

Currently delving into Front-End development and in the process of coding my maiden site using bootstrap. Encountered a roadblock on something seemingly simple. How can I center text within a button? Here's the button, the default one utilizing an " ...

What is the best way to retrieve a value from $http or $resource using this filter?

Within my HTML, I have a structure that looks like {{ i.userId | userName }}. Essentially, I am attempting to convert this into a name by utilizing a filter. However, I am encountering numerous challenges with the asynchronous function required to retrieve ...

Angular.js Service for Blocking and Initializing Data

My applications rely heavily on various web services within the intranet, with URLs that are dependent on the server environment. The apps are hosted on IIS, which adds an HTTP response header indicating the environment they are running in, such as Enviro ...

Sending a parameter to an ionic angular service

Currently, I am facing an issue while trying to create a button for display in Ionic that is based on a certain condition that needs evaluation within a service. To achieve this, I need to inform the service about the caller by sending a parameter. However ...

What strategies work best for managing a MySql database within a single-page application (SPA)?

How can I efficiently manage a complex MySql database in a single-page-app? Currently, I rely on jQuery ajax to retrieve data from the database, but this method appears to be sluggish when loading data into the browser. This issue arises particularly whe ...

My redirect is being disrupted by passing a text string with new lines through GET requests

My issue involves submitting a form that utilizes JavaScript to pass a value through GET. Upon reaching the submission page, the function runs successfully, but when redirecting via the header, it ends up on a blank page. The header code for redirection lo ...

A pair of large pillars perfectly aligned at the center in both horizontal and vertical position

Can anyone suggest an efficient way to design the following layout with responsiveness in mind? I attempted to divide the elements into sections and applied the transform property, but I encountered difficulties when trying to style the boxes on either si ...