The fixed table header is misaligned with the body columns' width

I was looking for a way to add a vertical scrollbar to my table, and I ended up wrapping the entire table in a div. The result was that the table head was fixed in position. Is there a simpler method to include a scrollbar in a table without affecting the width of the table while keeping it centered on the page?

thead, tr, th, td, tbody{
border: 1px solid;
text-align: center;
padding: 3px;

}

th{
background-color:#99ccff;
height: 40px;
font-size: 20px;

}

tr{
width: 500%;
height: 20px;
font-size: 17px;
}

tr:nth-child(even) {
background-color: #CCFFFF;
}
tr:nth-child(odd) {
background-color: #fae8d1;
}

thead{
position: fixed;
width: 1200px;
}
.tbldiv{
width: 1200px;
height: 600px;
border: 2px solid;
overflow: auto;
}
<div class="tbldiv">
<table class="scroll">
<thead>
<tr>
<th class="col-md-2">Name</th>
<th class="col-md-2">Birthday</th>
<th class="col-md-2">Gender</th>
<th class="col-md-2">Marital</th>
<th class="col-md-2">Address</th>
<th class="col-md-2">Telephone</th>
<th class="col-md-2">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
</tbody>
</table>
</div>

Answer №1

Give this code a shot

Replace position with translate

$(".tbldiv").scroll(function(){
  var translate = "translate(0," + this.scrollTop + "px)";
  $('thead').css('transform',translate);
});
thead, tr, th, td, tbody{
border: 1px solid;
text-align: center;
padding: 3px;

}
table.scroll {
    table-layout: fixed;
}
th{
background-color:#99ccff;
height: 40px;
font-size: 20px;

}

tr{
width: 500%;
height: 20px;
font-size: 17px;
}

tr:nth-child(even) {
background-color: #CCFFFF;
}
tr:nth-child(odd) {
background-color: #fae8d1;
}

.tbldiv{
float:left;
height: 200px;
border: 2px solid;
overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div class="tbldiv">
<table class="scroll">
<thead>
<tr>
<th class="col-md-2">Name</th>
<th class="col-md-2">Birthday</th>
<th class="col-md-2">Gender</th>
<th class="col-md-2">Marital</th>
<th class="col-md-2">Address</th>
<th class="col-md-2">Telephone</th>
<th class="col-md-2">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
<td>Some Data</td>
</tr>
      <tr>
<td>Some Data</td>
<td>Some Data</td>
...
</tbody>
</table>
</div>

Answer №2

You have the ability to achieve this with CSS.

.table-container {
    height: 10em;
}
table {
    display: flex;
    flex-flow: column;
    height: 100%;
    width: 100%;
}
table thead {
    /* head takes the height it requires, 
    and it's not scaled when table is resized */
    flex: 0 0 auto;
    width: calc(100% - 0.9em);
}
table tbody {
    /* body takes all the remaining available space */
    flex: 1 1 auto;
    display: block;
    overflow-y: scroll;
}
table tbody tr {
    width: 100%;
}
table thead,
table tbody tr {
    display: table;
    table-layout: fixed;
}
/* decorations */
.table-container {
    border: 1px solid black;
    padding: 0.3em;
}
table {
    border: 1px solid lightgrey;
}
table td, table th {
    padding: 0.3em;
    border: 1px solid lightgrey;
}
table th {
    border: 1px solid grey;
}
<div class="table-container">
    <table>
        <thead>
            <tr>
                <th>head1</th>
                <th>head2</th>
                <th>head3</th>
                <th>head4</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</td>
            </tr>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</td>
            </tr>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</td>
            </tr>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</td>
            </tr>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</td>
            </tr>
            <tr>
                <td>content1</td>
                <td>content2</td>
                <td>content3</td>
                <td>content4</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

What is the method for executing a server-side script without it being transmitted to the browser in any way?

Although the title may be misleading, my goal is to have my website execute a php file on the server side using arguments extracted from the html code. For example: document.getElementById("example").value; I want to run this operation on the se ...

Step-by-step guide to selecting a specific point on an HTML5 canvas using Python's selenium webdriver

Looking to automate interactions with a simple HTML5 application on a website using Selenium webdriver in Python with Firefox on Linux. The challenge is clicking a button on an HTML5 canvas, then dragging one or two objects around the canvas post-button cl ...

Does the rendered ID of an ASPX control always appear the same in the source HTML code?

Let's say I have an aspx textbox with id="txtkms". In the HTML view source, it appears as ContentPlaceHolder1_Gridview1_txtkms_1. I'm curious if this control will always be rendered as ContentPlaceHolder1_Gridview1_txtkms_1 every time I run my as ...

XML, XTL, and HyperText Markup Language (HTML)

I am facing a challenge with transforming a list in an XML file into an HTML view using XSLT. The nesting of the lists is causing issues and the output is not meeting my requirements. XML: <book-part> <list id="ch4list2" list-type="simple"> & ...

Value of MySQL in a web form

Currently in my PHP code, I have the following: echo '<input type="email" name="email" value=''; I am looking to automatically populate the value with a data from a MySQL row (let's say ['name']), but I am encountering syn ...

Achieving a frosted glass effect for your background without relying on a background image

I am currently working on creating a modal window in React with a blurred backdrop. My goal is to blur the backdrop content while keeping the background image clear, as there is no image behind the modal window, just content. I have explored various soluti ...

Attempting to integrate the Angular2 module text-mask-addon into the project

Currently, I am in the process of integrating text-mask-addons into my Angular application. You can find more information at https://github.com/text-mask/text-mask/tree/master/addons Despite attempting to follow the npm links suggestion, I am encountering ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

Ways to halt a CSS animation when it reaches the screen boundary

I put together this demo: By clicking, a red box falls down. The issue arises when trying to determine the screen size using only CSS. In my demo, I set the box to fall for 1000px regardless of the actual screen height. Here is the keyframe code snippet ...

Partially Assessed Ajax Response

I am struggling with my ajax response as it seems to evaluate only some of the html, but not all of it. For instance, I have this code that replaces a div with the response from the request: eval(document.getElementById("test").innerHTML-xmlhttp.response ...

Converting user's birthdate input from HTML to their age using PHP

I am facing an issue with retrieving the correct date from a date-time-picker named "dob" and passing it to PHP. When I try to post the date into the database user_age column, it only displays '2017'. However, if I manually set $dob to '10/0 ...

Enhance the appearance of radio buttons using CSS and incorporate images to create a

Is there an easy and straightforward method to customize radio buttons using only CSS, allowing me to click on an image instead of a traditional bullet point? For example, like thumbs up/thumbs down. I have two radio buttons that need to track which one i ...

What is the method to make xsl:function return a string value containing HTML tags?

I am currently working on converting a Java function into an XSL:Function specification. The function is responsible for adding HTML tags around substrings. I have encountered some challenges in this process: while the Java code works perfectly with inline ...

One Background Image Serving Multiple Divs

Can you use one image (PNG or SVG) as the background for multiple divs? Take a look at the images below to see how it could work. And if the screen width gets smaller and the divs stack up vertically, is there a way to change the background accordingly? D ...

How can I modify the border color of a Material Ui TextField component?

I've been struggling to figure out how to customize the color of the TextField border. I want to change it from the default grey to black. Below is a simplified version of my UpdatePage.js file: const useStyles = makeStyles(() => ({ updatePage ...

Making rapid formatting changes by rearranging the positioning of words in Javascript

Struggling to untangle my complex code, I'm unable to make the necessary adjustments. Here's what I have: var stocks= [ ["Beef (80/20) raw","oz",115.4451262,3.293742347,72,"4.85 gallons","5.65 pounds","0 - ",2.142,19,20,"0.0001275510204"," ...

Limit selection choices in select element

Similar Question: Prevent select dropdown from opening in FireFox and Opera In my HTML file, I have a select tag that I want to use to open my own table when clicked. However, the window of the Select tag also opens, which is not desirable. Is there a ...

Tips for centering text in a dijitTextBox:

Ensure that the text is centered vertically with left padding and placeholder text included Check out the image link below: https://i.stack.imgur.com/PbHDa.jpg Snippet of my xPage code: <xe:djTextBox id="djTextBoxSearch" style="width:100%;height: ...

Exploring the fundamentals of Django with a touch of creativity in CSS

As a newcomer to Django, I am currently working on setting up a basic Django application. I have progressed to chapter 5 in the Django online book: Before delving into databases, my goal is to incorporate some simple CSS and JS directly into the applicat ...

Reducing image size using CSS can result in blurry images on multiple web browsers

Encountering challenges with downscaled images in multiple browsers... The images must be downscaled to adapt to the browser size. Here is the code snippet: #pic-holder img { -moz-transform: rotate(0deg); /*image-rendering:-webkit-optimize-contras ...