Locked down a row in the table

I need help fixing the position of the first <tr> in my <table>. Currently, it looks like this:

https://i.sstatic.net/PveD5.png

but I want it to look like this:

https://i.sstatic.net/NgIn5.png

This is what I have tried so far:

<table id="myHeader" class="table table-striped table-hover table-bordered table-responsive"><tbody>
            <tr  style="position: fixed;">
                <th>Sr.</th>
                <th>Question</th>
                <th class="mobiile">Your Answer</th>
                <th>Right Answer</th>
                <th>Marks</th>
              </tr>     

Answer №1

document.getElementById('theTable').addEventListener('scroll', scrollHeader);

function scrollHeader(e){
    
    var table = e.currentTarget,
        thead = table.getElementsByTagName('thead')[0];
    
    thead.scrollLeft = table.scrollLeft;
}
table {
font-family:Arial, Helvetica, sans-serif;
color:#666;
font-size:15px;
background:#eaebec;
border:#ccc 1px solid;
border-radius:3px;
border-collapse:collapse; border-spacing: 0; 
box-shadow: 0 1px 2px #d1d1d1;
    
    display: block;
    overflow: auto;
    max-height: 200px;
    width:425px;
}
table thead {
    z-index: 1; /* put on top of other rows */
display: block;
position: fixed;
    
    /* make sure this header width is same as table */
    max-width: 500px;
    overflow-y: auto; /* use auto so scrollbars only appear if they need to*/
    overflow-x: hidden;
}
thead tr td {
    background: #eaebec;
width: 65px;
min-width: 65px;
}
table tbody {
display: block;
padding-top: 40px;
}
table th {
padding:2px 2px 2px 2px;
border-top:0;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;
background: #ededed;
}
table tr {
text-align: center;
}
/* only using !important to override your styles below */
table td {
margin-left: 0 !important;
    width: 65px !important;
padding:2px 2px 2px 2px;
border-top:0;
border-bottom:1px solid #e0e0e0;
border-left: 1px solid #e0e0e0;

}

table tr:hover td {
background: #F7FE2E;
}
table tr, table td {
width: 65px;
min-width: 65px;
}
<div id='container'>
<div id='content'><h3><font color='blue'>Listview - MP</font></h3>
<div id='wrapper'>
<table id="theTable"><thead><tr><td>Julian Date</td>
<td>Records</td>
<td>Sales</td>
<td>CPO</td>
<td>2006 Records</td>
<td>2006 Deals</td>

</tr></thead>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
<tr><td>229</td><td>39784</td><td>0</td><td>10,542.76</td><td>0</td><td>39784</td></tr>
</table></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

Tips for relocating anchor elements to less desirable locations

My website has an issue with anchor elements appearing too frequently and not in the desired location. I need someone to help fix this so there are only two anchors displayed. After checking my code, it seems like there are not more than the specified num ...

Strategies for managing a situation where a class is called during runtime

Can anyone assist me with an Angular issue? I am trying to dynamically add the abc class when the show class is present, and remove the abc class when the show class is hidden. I am working with Angular 6 and my current conditions are as follows. Any help ...

Python: Remove tag and substitute u00a0 within a JSON document

I have a JSON file structured like this: [ { "content": ["<p style=\"text-align:justify;\"> This is content1</p>"], "title" : ["This is\u00a0title 1"] }, { "content": ["<p style=\"text-a ...

"Utilize jQuery's append method to dynamically add elements to the DOM and ensure

I am currently facing an issue with my AJAX function that appends HTML to a page using the jQuery append method. The HTML being appended is quite large and contains cells with colors set by CSS within the same document. The problem I'm encountering i ...

Adjust the Size of Bootstrap 4 Dropdown Caret Icon

Currently, I am using the Bootstrap v4 framework on my HTML page. I am looking to resize or increase the caret size in my dropdown list. Does anyone have any tips on how to accomplish this? https://i.sstatic.net/kCNPK.png Below is the code snippet: < ...

Ways to navigate to a new webpage in JavaScript without the need to click on a link

After going through various posts and trying different methods, I am still facing challenges. In a PHP page, I have an HTML form with an onClick command that triggers a JavaScript validation procedure when the user clicks a button. While everything funct ...

The updated version of Angular from 13 to 16 has implemented a new default value for primary colors

I recently upgraded my angular (+material-ui) system from version 13 to 16, and encountered an issue with the primary color of my custom theme. It seems that the value is no longer being recognized and defaults to blue. After updating the angular version, ...

Using JavaScript and PHP to dynamically update a field based on two input values within a row of a table

Currently in the process of developing a dynamic profit margin calculator for a personal project. Overview Data is retrieved from a database table using SQL and PHP After necessary validations, the data is dynamically displayed as rows in an HTML table ...

Jquery alters the border of a textbox with a single modification

I attempted to implement a validation using regular jQuery and managed to accomplish half of it successfully. When I try to submit an empty field, the textbox border turns red, but if I fill in the value and click the button again, the border doesn't ...

Creating a basic ruler using JavaScript: what you need to know

As I embark on my journey with PhoneGap, I am delving into creating a simple application - a Ruler. Despite my background in native Android development, I find myself confronting challenges in JavaScript as I attempt to figure out what's going wrong. ...

Place an element in relation to its initial position, ensuring that the x-coordinate is not set to 0

Is there a method to place an element in relation to its initial position, only excluding x=0 - meaning that the element is placed at its original vertical coordinate but sticks to the window's left edge? ...

What is the best way to navigate to Amazon URLs with BeautifulSoup4?

Problem: The list "linkElems" seems to be empty Suspected Cause of Issue: I believe the tags I am specifying to retrieve are incorrect Purpose of Program: Execute a search on Amazon.com via command line arguments and save the website data in the variab ...

How to stylishly showcase a list in a horizontal row using HTML and CSS`

Is there a way to modify this code so that the list is displayed in a horizontal line menu? Currently, the code displays the list vertically but I want it to be displayed in a horizontal line. How can I achieve this? <html><head><style> ...

What is the best way to convert this html code into php?

I’m trying to figure out how to incorporate all of this into my loop, but I’m not sure if it can be broken up. However, PHP needs to interpret it as HTML. $movieDetails = <header class="masthead" role="banner"> <div class="logo-con ...

Positioning a Bootstrap popover directly adjacent to the cursor's location (within the clicked element)

I've been struggling to get my bootstrap popover to show up inside the element I'm calling it on, next to my mouse cursor. Despite extensive research in the popper.js documentation and numerous attempts with various parameters, including the &apo ...

Master page does not recognize JQuery on all pages

Issue identified: The Request URL in the network tab is including an extra directory in the path on a page that is not functioning correctly due to the webform being nested within an additional directory in the solution structure. The home page does not ...

Styling Your PHP Output with Echo

Is there a way to create a compact text box with a scroll bar that can display recurring data generated by PHP activities on the server side? How can I format it in this way? ...

Guide on making a persistent sidebar using CSS and JavaScript

I'm in the process of developing a website that features a main content area and a sidebar, similar to what you see on Stack Overflow. The challenge I am facing is figuring out how to make the sidebar remain visible as a user scrolls down the page. T ...

Sending the value from a for loop through AJAX and populating it into a form field

Currently, I have implemented a piece of JavaScript code that captures user input, sends a request to an endpoint using AJAX, and appends a specific field from the returned results as an option within a datalist. This functionality is working perfectly fin ...

Issue with toggleClass() function on a div element

I've been developing a website and encountered an issue. In my FAQ section, I have a drop-down menu along with a submit button that directs to different parts of the FAQ page. Everything seems to be working fine so far - the submit button triggers a j ...