Issues with TH elements not functioning properly in fixed headers of tables

I am working on creating a table with a fixed header. I have made some adjustments, but the header is not extending to full width as expected. Here is my code snippet and a demonstration:

To achieve the desired look, I want the table rows (tr) to fill up the entire 100% width and the table headers/cells (th/td) to cover all available space.

.fixed_headers {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: #ccc;
  display: block;
}
.fixed_headers td {
    border-bottom: 1px solid #f00;
}


.fixed_headers tr{
  display: flex !important;
  width: 100%;
  
}

.fixed_headers thead {
  background-color: #f00;
  color: #fdfdfd;
}

.fixed_headers td:nth-child(1),
.fixed_headers th:nth-child(1) {
  min-width: 20%;
}
.fixed_headers td:nth-child(2),
.fixed_headers th:nth-child(2) {
  min-width: 30%;
}
.fixed_headers td:nth-child(3),
.fixed_headers th:nth-child(3) {
  width: 20%;
}
.fixed_headers td:nth-child(4),
.fixed_headers th:nth-child(4) {
  width: 30%;
}
table.fixed_headers thead tr {
  display: block;
  position: relative;
}
table.fixed_headers tbody {
  display: block;
  overflow: auto;
  width: 100%;
  height: 300px;
}

.fixed_headers tbody::-webkit-scrollbar {
    width: 8px;
}

.fixed_headers tbody::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #f00;
}
<table class="fixed_headers">
  <thead>
    <tr>
      <th>DATA</th>
      <th>FULL NAME</th>
      <th>PHONE NUMBER</th>
      <th>PRIZE</th>
    </tr>
  </thead>
  <tbody>
     <!-- Repeat this row structure for demo purposes -->
  </tbody>
</table>

Answer №1

.fixed_headers {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: #ccc;
  display: block;
}
.fixed_headers td {
    border-bottom: 1px solid #f00;
}


.fixed_headers tr{
  display: flex !important;
  width: 100%;
  
}

.fixed_headers thead {
  background-color: #f00;
  color: #fdfdfd;
  width:100%;
  float:left;
}

.fixed_headers td:nth-child(1),
.fixed_headers th:nth-child(1) {
  min-width: 20%;
}
.fixed_headers td:nth-child(2),
.fixed_headers th:nth-child(2) {
  min-width: 30%;
}
.fixed_headers td:nth-child(3),
.fixed_headers th:nth-child(3) {
  width: 20%;
}
.fixed_headers td:nth-child(4),
.fixed_headers th:nth-child(4) {
  width: 30%;
}
table.fixed_headers thead tr {
  display: block;
  position: relative;
}
table.fixed_headers tbody {
  display: block;
  overflow: auto;
  width: 100%;
  height: 300px;
}

.fixed_headers tbody::-webkit-scrollbar {
    width: 8px;
}

.fixed_headers tbody::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background: #f00;
}
<table class="fixed_headers">
  <thead>
    <tr>
      <th>DATA</th>
      <th>NOME COMPLETO</th>
      <th>TELEFONE</th>
      <th>PRÊMIO</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>15/01</td>
      <td>Castello Branco</td>
      <td>(21) 99612-xxx</td>
      <td>1000 REAIS</td>
    </tr>
    <tr>
      <td>15/01</td>
      <td>Castello Branco</td>
      <td>(21) 99612-xxx</td>
      <td>1000 REAIS</td>
    </tr>

Answer №2

implement styling using css

.header-section {
    width: 100%;
    float: left;
}

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

"Error occurred: Unable to execute bgColor as a function" on HTML select onchange event

I am having an issue with my HTML switch that has an onchange tag triggering the JavaScript function bgColor with the argument this. However, every time I attempt to use this, I receive an error message: Uncaught TypeError: bgColor is not a function. Can a ...

An advanced coding tool that is able to transfer CSS code from the style attribute to the class definition

Is there an editor or IDE that can convert styles from CSS classes to inline styles? .my_class { color: black; } <span class="my_class" style="font-size: 200%;">test</span> to .my_class { color:black; font-size: 200%; } <span ...

How to harness the power of loops in JavaScript

Having trouble getting this code to repeat a CSS transition properly. for (var i=0 ; i<4 ; i++){ setTimeout(function() { $('#top-left').css('margin', '45px 0 0 45px'); $('#top-mid' ...

Adjust the dimensions and position of the notification box using Twitter Bootstrap

I am struggling to center and adjust the size to 80% of an alert box, but the text-center class is not achieving this for me. <div class="text-center"> <div class="alert alert-info" style="width: 80%;" role="alert"> <i class="fa fa- ...

JavaScript Time Counter: Keeping Track of Time Dynamically

Currently, I am attempting to create a dynamic time counter/timer using JavaScript. Why dynamic? Well, my goal is to have the ability to display days/hours/minutes/seconds depending on the size of the timestamp provided. If the timestamp is less than a d ...

Design interactive horizontal buttons containing images and corresponding labels using HTML5

I want to create three horizontal buttons with images and labels underneath using HTML5. I attempted to follow this example: tutorial, but I need a different image for each button. When I give each button an ID and add #id {background-image:url} to the CS ...

button that stays in place on smaller screens

Hello everyone, I am currently facing an issue while trying to create a button with fixed positioning. The problem arises on smaller screens where the button completely disappears. Here is my CSS code: .sound_button{ background: #FFD700 url("Images/ ...

What is the best way to display an error message for an invalid input using JavaScript?

I am struggling to implement error messages for my form inputs using JavaScript. My attempts so far have not been successful and I need guidance on the correct approach. I want to show an error message and prevent the form from being submitted if any erro ...

The unresponsive sticky navigation bar on a Joomla website is causing issues

I recently launched a new website that can be found here. The site includes the following JavaScript code: $(document).ready(function(){ $(window).bind('scroll', function() { var navHeight = $( window ).height() - 70; ...

Ways to access the files attribute in an input tag in AngularJS without relying on getElementById

I am currently working on file uploads using AngularJS and I have a question regarding how to retrieve input files similar to regular JS. What I want to achieve: HTML: <input type="file" name="file" id="fileImg" accept="image/*"> JS: var file ...

Initiate activity when link is clicked

I am currently in the process of developing a website that includes 5 divs. <div class="one"></div> <div class="two"></div> <div class="three"></div> <div class="four"></div> <div class="five"></div ...

Guide to incorporating a Menu feature into your MVC 5 application

After creating an MVC 5 project, the decision was made to change the menu by incorporating Bootswatch. The desired menu to implement is displayed below: https://i.sstatic.net/2vqxn.png The initial step involved modifying the general style of the project ...

The Bootstrap 4 Toggler Dropdown lacks a background color

Struggling to style the toggle dropdown on smaller screens to match the regular navbar background? Can't seem to figure out how to target it. Is there a missing bootstrap class or an error in the code? Initially suspected the border might be causing i ...

How can I position the dropdown to the right-hand side of the card header in HTML?

How can I position a dropdown in the top right corner of my card header? I want to have a welcome message on the right side of the card header next to the title, similar to the image below: https://i.sstatic.net/G0Sq3.png ...

What are the steps for showcasing a personalized HTML tag on a web page

I need to capture user input text and display it correctly. This is what I have attempted: <div> <input type="text" ng-model="post.content" /> </div> <div> <div ng-bind-html="post.content| htmlize"></div> < ...

Does the a:hover:not() exception only apply to elements with the .active class?

I developed a navigation bar and split it into 3 distinct sections: 1. id="menu" 2. id="fake" (button to fill space) 3. id="social" My main goal is to exclude the .active page and the fake button from the hover effect, but it seems that using a:hover:not( ...

"Retrieve data from an Excel file and either present it in a textarea or store it as an array

Is it possible to use JavaScript to read an Excel file and convert a single column (identified by name or index) into an array in JavaScript? ...

What is the best way to verify if a value exists within a Flask session using Jinja?

Having an issue in my Flask application where I am unable to check if a specific value, such as the 'name' value, exists in a session. Here is the code snippet: Python: @app.route("/add/<string: name>") def add(name): session["list ...

Determining in Angular 8 whether a value has been altered by a user or by a method call

Within my select element, the value is currently being assigned through an ngOnInit call. Here is an example of the HTML code: <select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> <option *ngFor="l ...

maximum allowed size for downloading manifest files

Exploring Manifest files and pondering an interesting scenario. Imagine a situation where a website sets up a manifest file requiring users to cache 3MB of data. For someone like me with limited iPhone storage running on 3G, this could quickly eat up my da ...