The <th> element does not expand to 100% width

I'm currently working on implementing a scrollable <tbody> in my project, following the solution provided by gavroche. Here's what I have so far:

JS:

var tblcols = 7; // defining table columns
var tblClass = ".scroll"; // referencing table class

for(var i = 0; i < tblcols; i++) {
    var thWidth = $(tblClass).find("th:eq(" + i + ")").width();
    var tdWidth = $(tblClass).find("td:eq(" + i + ")").width();

    if(thWidth < tdWidth)
        $(tblClass).find("th:eq(" + i + ")").width(tdWidth);
    else
        $(tblClass).find("td:eq(" + i + ")").width(thWidth);
}

CSS:

.scroll table {
    margin: 0 auto;
    border-collapse: separate;
}

.scroll thead {
    display: block;
}

.scroll tbody {
    height: 30em;
    overflow-y: scroll;
    display: block;
}

HTML:

<table id="viewfaculty" class="scroll">
    <thead align="center">
        <tr>
            <th><a href="viewfaculty?orderBy=professor_id">ID</a></th>
            <th><a href="viewfaculty?orderBy=professor_last_name">L. Name</a></th>
            <th>F. Name</th>
            <th>Sex</th>
            <th>Email</th>
            <th><a href="viewfaculty?orderBy=professor_employment_status">Empl. Status</a></th>
            <th><a href="viewfaculty?orderBy=professor_department">Dept.</a></th>
        </tr>
    </thead>

    <tbody>
        <s:if test="#session.facultyList.isEmpty()">
            <tr align="center">
                <td colspan="7"><p class="norecords">NO RECORDS FOUND</p></td>
            </tr>
        </s:if>

        <c:forEach var="professor" items="${facultyList}">
            <tr>
                <td align="center">${professor.profId}</td>
                <td>${professor.profLastName}</td>
                <td>${professor.profFirstName}</td>
                <td align="center">${professor.profSex}</td>
                <td>${professor.profEmail}</td>
                <td align="center">${professor.profEmplStatus}</td>
                <td align="center">${professor.profDept}</td>
            </tr>
        </c:forEach>
    </tbody>
</table>

Output:

Any tips on how to make the header row extend across the entire table?

Answer №1

It seems like there might be an issue preventing your JavaScript code from running correctly. I tested your code on a fiddle and it worked fine. However, when I removed the JavaScript section, I encountered the same problem you described:

$(document).ready( function(){
    var tblcols = 7; // defining table columns
    var tblClass = ".scroll"; // setting table class
    
    for (var i = 0; i < tblcols; i++) {
        var thWidth = $(tblClass).find("th:eq(" + i + ")").width();
        var tdWidth = $(tblClass).find("td:eq(" + i + ")").width();
        
        if (thWidth < tdWidth)
            $(tblClass).find("th:eq(" + i +")").width(tdWidth);
        else
            $(tblClass).find("td:eq(" + i +")").width(thWidth);
    }
});
.scroll table {
    margin: 0 auto;
    border-collapse: separate;
}

.scroll thead {
    display: block;
}

.scroll tbody {
    height: 30em;
    overflow-y: scroll;
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="viewfaculty" class="scroll">
    <thead>
        <tr align="center">
            <th><a href="viewfaculty?orderBy=professor_id">ID</a></th>
            <th><a href="viewfaculty?orderBy=professor_last_name">Last Name</a></th>
            <th>First Name</th>
            <th>Sex</th>
            <th>Email</th>
            <th><a href="viewfaculty?orderBy=professor_employment_status">Employment Status</a></th>
            <th><a href="viewfaculty?orderBy=professor_department">Department</a></th>
        </tr>
    </thead>

    <tbody>
        [... Repeat this block as needed for each row ...]
        <tr>
            <td align="center">${professor.profId}</td>
            <td>${professor.profLastName}</td>
            <td>${professor.profFirstName}</td>
            <td align="center">${professor.profSex}</td>
            <td>${professor.profEmail}</td>
            <td align="center">${professor.profEmplStatus}</td>
            <td align="center">${professor.profDept}</td>
        </tr>
         [... Add more rows here ...]
    </tbody>
</table>

Answer №2

After some investigation, I was able to identify the issue. The CSS code included in the premade/open-source template was causing problems with how the tables were being displayed by default. To resolve this, I decided to remove any CSS related to tables.

In addition, I found a solution that does not rely on JavaScript.

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

Discover the sleek and modern concept of transparency in Microsoft's

I'm interested in incorporating Microsoft Fluent Design transparency into my webpage. Are there any CSS or other tools that can help achieve this design language? I want to enhance the look of my site with these elements. ...

Safari's iOS appears to be causing a striped effect on Bootstrap fonts

I have developed a straightforward application utilizing Bootstrap version 3.3.5 and included a button with a dropdown feature: <!-- Large button group --> <div class="btn-group"> <button class="btn btn-default btn-lg dropdown-toggle" typ ...

Is it possible for a draggable position:absolute div to shrink once it reaches the edge of a position:relative div

I am facing an issue with draggable divs that have position:absolute set inside a position:relative parent div. The problem occurs when I drag the divs to the edge of the parent container, causing them to shrink in size. I need the draggable divs to mainta ...

What's the Best Approach: Conditional Processing or Separate Pages?

I am in the process of creating a resource section on a website that will showcase various types of information, such as guidebooks, blogs, and corporate documents. Each type of information has unique requirements when it comes to layout and presentation. ...

Populate an HTML select with data as users click the create button

Encountering an issue when loading data into an HTML select after users press or click a button. The situation is as follows: A button is available to create another button dynamically Upon clicking the created button, a form is displayed Once the form i ...

What is the best way to apply custom css to a Google Plus share button within an iframe?

I am having an issue with the CSS on my share button. The Google style is causing my CSS to reset. Here is the iframe code I am using: <iframe src="https://apis.google.com/u/0/_/+1/fastbutton?usegapi=1&amp;size=tall&amp;origin=http%3A%2F%2F ...

overflowing bootstrap cards are causing disruptions in row layouts and impacting the margins of surrounding elements

As a beginner in the world of programming, I'm reaching out for help with my first post. Please bear with any errors in my code or confusion in my terminology. Experimenting with bootstrap cards on a blog template, I am facing an issue where the heig ...

Scaling the ion-spinner to fit your specific needs

In my Ionic application, I am utilizing the ion-spinner. However, I have encountered an issue with resizing the spinner. While custom CSS works well with default spinners, it does not function properly with Bubbles, Circles, and Dots spinners. CSS .spin ...

Creating an HTML Menu in PHP by Parsing JSON

Managing a website involves constant updates and improvements. Recently, I started using DataTables, but faced timeout issues with large HTML tables. To resolve this, I decided to switch to server-side processing using JSON. Despite being new to JSON, I&ap ...

Issue with Website Rendering: Safari 4 exhibits display glitch showing temporary content before showing a blank white screen

Currently, I'm in the process of developing a specialized rails application. However, there seems to be an unusual rendering error that has been reported by Safari 4 users. It's quite peculiar because the page appears briefly but quickly disappea ...

What is the best way to assign a background color to each tr element using JavaScript?

I have a list of table rows with different background colors like <tr bgcolor="#OC6110"> <tr bgcolor="#000000"> <tr bgcolor="#FFFFFF"> Is there a way to assign unique background colors to each tr element without specifying its id value ...

center the selection text in a Bootstrap dropdown

I'm currently working on a bootstrap 4.3.1 dropdown select box, but I'm having trouble centering the text ('nothing selected') in the select box. $(function() { $('.selectpicker').selectpicker(); }); <link href=" ...

Align text divs vertically and ensure font size is responsive within a container div

Within this designated div, I have included: https://i.stack.imgur.com/j099H.png In an attempt to ensure that the font size of the text adjusts responsively, I implemented the following code: font-size:calc(xx + 1.5vw); However, upon resizing the scree ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Transmit data with a post request from an HTML form to a Node.js application

I'm encountering an issue with a form I created that isn't correctly sending data to my node js app. Interestingly, when using tools like Postman to send requests, everything works fine (the files sent are saved in the desired folder). However, s ...

Scaling Images using HTML and CSS

Hey there, I'm currently learning web development and running into a bit of trouble with creating responsive images. Can anyone give me some guidance on what changes I should make in the code below? Here's the HTML code: <div class="caro ...

Can a specific element be chosen based on its background color?

Is it possible to change the background color of a child element within a div that has a specific background color using CSS? See my explanation below. For example: .container[background-color=some color] .content { background-color: some other color ...

The functionality of Bootstrap Cards is malfunctioning when used in conjunction with columns

When viewing on smaller devices, I want to show "Cards" in a single line -> with the image on the left and the name and description on the right. For larger devices, I prefer to have multiple cards displayed side by side -> with the picture on top an ...

Achieving overlapping div layouts with CSS styling

Displayed above are 4 different divs: red, green, and blue. I am seeking to have the blue div positioned directly below the green div, which contains the text "JACKSON". Additionally, I do not want the green div to expand beyond its content, nor do I want ...

Unable to locate template in Flask while attempting to integrate Flask-Bootstrap

I am in the process of working on a project and I have decided to integrate bootstrap with flask. Following the guidelines provided in the book FLASK WEB DEVELOPMENT (OREILLY), I have copied the exact code from the book's github repository. Upon impor ...