Tips for maintaining the fixed size of a table header and preventing it from resizing in width

My goal was to create a table using divs with fixed headers while scrolling vertically. However, I encountered an issue where the header width seemed to shrink and became misaligned with the rows. Even setting the width to 100% did not resolve this problem.

.grid-wrapper {
    position: relative;
}

.grid {
    background-color: #fff;
    overflow: auto;
    margin-top: 70px;
    height: 60%;
}

.gridHeader {
    display: table-header-group;
    position: absolute;
    top: -50px;
    z-index: 2;
}

.gridHeader .gridCell {
    font-weight: bold;
    color: #334D5C;
    text-decoration: none;
}

.gridBody {
    display: table-row-group;
}

.gridRow {
    display: table-row;
    background-color: #fff;
    min-height: 50px;
    line-height: 30px;
}

.gridRow:hover {
    color: #45B29D;
}

.gridCell {
    display: table-cell;
    width: calc(100% / 7);
    padding: 10px;
    text-align: center;
    word-break: break-all;
    border-width: 0px 0px 1px 0px;
    border-color: #d2d7dc;
    border-style: solid;
}

Answer №1

Consider setting a specific width for the header divs and also a maximum width for added security. Have you thought about using tables instead of divs? It might save you some time and effort.

.gridHeader {
   //display: flex;
   display: table-header-group;
   position: absolute;
   top: -50px;
   z-index: 2;
   width: 50px;
   max-width: 50px;
}

Answer №2

Apply the following styles to your header: display: table; width: 100% and position: fixed;

.grid-wrapper {
  position: relative;
}

.grid {
  background-color: #fff;
  overflow: auto;
  margin-top: 70px;
  height: 60%;
}

.gridHeader {
  display: table;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2;
  width: 100%;
}

.gridHeader .gridCell {
  font-weight: bold;
  color: #334D5C;
  text-decoration: none;
}

.gridBody {
  display: table-row-group;
}

.gridRow {
  //display: flex;
  display: table-row;
  background-color: #fff;
  min-height: 50px;
  line-height: 30px;
}

.gridRow:hover {
  color: #45B29D;
}

.gridCell {
  display: table-cell;
  width: calc(100% / 7);
  padding: 10px;
  text-align: center;
  word-break: break-all;
  border-width: 0px 0px 1px 0px;
  border-color: #d2d7dc;
  border-style: solid;
}
<div class="grid-wrapper">
  <div class="grid ui-sortable">
    <div class="gridHeader">
      <div id="inline-actions" class="gridCell"></div>
      <div id="title" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="title">title<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
      <div id="customer" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="customer">customer<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
      <div id="price" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="price">price<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
      <div id="calories" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="calories">calories<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
      <div id="eggless" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="eggless">eggless<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
      <div id="duedate" class="sortable gridCell ui-sortable-handle" title="Click on title to sort. Drag and drop to reorder." data-sort="nosort" data-header="duedate">duedate<i id="sort-icon" class="fa fa-sort"></i><i id="exchange-columns" class="fa fa-exchange"></i></div>
    </div>
    <div class="gridBody">
      <div class="gridRow" data-recordnumber="0">
        <div class="gridCell" data-recordnumber="0"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Chocolate Pancakes With Fruits</div>
        <div class="gridCell">Bilbo Baggins</div>
        <div class="gridCell">300</div>
        <div class="gridCell">1997.07</div>
        <div class="gridCell">true</div>
        <div class="gridCell">2017-07-04</div>
      </div>
      <div class="gridRow" data-recordnumber="1">
        <div class="gridCell" data-recordnumber="1"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Cinnamon Sugar Fried Apples</div>
        <div class="gridCell">Frodo Baggins</div>
        <div class="gridCell">200</div>
        <div class="gridCell">1997.07</div>
        <div class="gridCell">true</div>
        <div class="gridCell">2017-07-06</div>
      </div>
      <div class="gridRow" data-recordnumber="2">
        <div class="gridCell" data-recordnumber="2"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Cinnamon-Apple Bread</div>
        <div class="gridCell">Tony Stark</div>
        <div class="gridCell">250</div>
        <div class="gridCell">1500</div>
        <div class="gridCell">true</div>
        <div class="gridCell">2017-07-08</div>
      </div>
      <div class="gridRow" data-recordnumber="3">
        <div class="gridCell" data-recordnumber="3"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Pecan Tart</div>
        <div class="gridCell">Scrats Squirrel</div>
        <div class="gridCell">210</div>
        <div class="gridCell">1999</div>
        <div class="gridCell">true</div>
        <div class="gridCell">2017-07-08</div>
      </div>
      <div class="gridRow" data-recordnumber="4">
        <div class="gridCell" data-recordnumber="4"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Cinnamon, Apple, Oatmeal Cake</div>
        <div class="gridCell">Bruce Banner</div>
        <div class="gridCell">390</div>
        <div class="gridCell">2007.07</div>
        <div class="gridCell">false</div>
        <div class="gridCell">2017-07-09</div>
      </div>
      <div class="gridRow" data-recordnumber="5">
        <div class="gridCell" data-recordnumber="5"><i id="edit" class="actions fa fa-pencil"></i><i id="delete" class="actions fa fa-trash"></i></div>
        <div class="gridCell">Chocolate Pancakes With Fruits</div>
        <div class="gridCell">Bilbo Baggins</div>
        <div class="gridCell">300</div>
        <div class="gridCell">1997.07</div>
        <div class="gridCell">true</div>
        <div class="gridCell">2017-07-04</div>
      </div>


    </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

Detecting whether a browser is capable of supporting dark mode

One method to determine if dark mode is active is by using prefers-color-scheme: dark: const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; Is there a way to detect if a browser supports dark mode as well? (By "supports ...

"Utilize AJAX to submit the value of the text box input from a jQuery slider when the Submit Button

Whenever I adjust the sliders, the value is shown in an input textbox. However, when I move the slider and check the values echoed from the textboxes on another PHP page, they are always displaying as 0. Even after clicking the submit button, it still echo ...

Keep the table header in place while scrolling

I am currently working with Bootstrap V4 alpha 6 and Angular 5 to develop a table that includes a fixed header while scrolling. Unfortunately, I have been facing challenges in getting this functionality to work effectively. Note: The navbar is set to fixe ...

The onclick event for a Bootstrap .btn-group checkbox functions correctly in JSFiddle, but encounters issues when tested

The toggle button group in the form below utilizes Bootstrap and checkboxes. I am looking to track click events on the checkbox inputs. <html> <head> <title>Example: Bootstrap toggle button group</title> <meta charset="UTF-8 ...

What is the best way to add a character within a YouTube JSON link?

Fetching json data from a link, the youtube format in the link is http:\/\/www.youtube.com\/watch?v=UVKsd8z6scw. However, I need to add the letter "v" in between this link to display it in an iframe. So, the modified link should appear as ht ...

How can I retrieve XML through a URL using HTML5 and JavaScript?

Currently, I am looking to access an XML file through a URL link. My main goal is to retrieve the file automatically and extract its contents for further processing. However, I am facing difficulties in finding the appropriate method to obtain and read t ...

Move items between unordered lists with automatic saving

As someone who is new to javascripting and php, I am looking to create multiple lists where I can easily drag and drop items between them. The specific order of the items within each list is not crucial as I will be able to adjust it using SORT BY. While ...

Find elements that are not contained within an element with a specific class

Imagine having this HTML snippet: <div class="test"> <div class="class1"> <input type="text" data-required="true"/> </div> <input type="text" data-required="true"/> </div> I'm looking to select ...

Show the precise selection from one dropdown menu based on the chosen value in another dropdown menu

How can I retrieve the scheduleID value from a specific MovieID value within a comboBox? Movie Selection Box: <?php $query = "select * from ref_movies ORDER BY MovieID"; $result = mysql_query($query) or die (mysql_error()); echo '<s ...

What sets apart the HTML tags for embed, object, and video elements?

Can you explain the technical distinctions among these three tags and provide reasons why one might be chosen over another? Additionally, why did the embed tag lose popularity in HTML 4 and get replaced by object, only to see a resurgence in HTML 5 while ...

Empty screen appears when "npm run serve" command is executed following the build process

I am currently utilizing Material-ui. Following the project build with npm run build, I encounter a blank page when running npm run serve. I attempted to set homepage: "./" in the package.json as suggested here, however, it still displays a blank ...

The v-on:click event handler is not functioning as expected in Vue.js

I am currently learning vue.js and facing some challenges. Below is the code snippet from my HTML page: <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net ...

Issue with template updating when utilizing ui-router and ion-tabs in Ionic framework

CODE http://codepen.io/hawkphil/pen/LEBNVB I have set up two pages (link1 and link2) accessible from a side menu. Each page contains 2 tabs: link1: tab 1.1 and tab 1.2 link2: tab 2.1 and tab 2.2 To organize the tabs, I am utilizing ion-tabs for each p ...

Headers with a 3 pixel stroke applied

I have a design on my website that includes a 3px stroke around the header text to maintain consistency. I don't want to use images for this due to issues with maintenance and site overhead. While I know about the text-stroke property, browser suppor ...

What is the process for adjusting the width of an element using JavaScript?

I have a unique bar with one half red and the other green. I am trying to subtract 1vw from the width of the red section. Unfortunately, using style.width is not yielding the desired result. See below for the code snippet I am currently using: //FIGHT do ...

Applying the 'overflow: scroll' property creates a scroll bar that remains fixed and cannot be scrolled

Looking to showcase the seating arrangement in a cinema hall. If the seats occupy more than 50% of the page, I want a scroll bar to appear. Attempted using "overflow-scroll" without success. View result image <div class="w-50"> <div ...

Having trouble with playing an mp4 video from Amazon S3 in an HTML video tag?

I have uploaded a video to S3 from my Android device, but it is not playing properly in HTML on Chrome. Here is the link to the video: ...

Customizing a responsive background image within a div using Bootstrap 3

Is there a way to style the background image of my header div like the one featured on Mr. Bill Gates' website at ? I noticed that the height of Mr. Bill Gates' header background image remains consistent, even on smaller screens. I've atte ...

Issue with QuickSand Script not displaying properly on Internet Explorer 7

My website features an edited version of Quicksand that displays correctly on Chrome, Opera, and Firefox, but runs into CSS issues on IE. Oddly enough, when I was using the static version, the CSS displayed properly even on IE until I added the Quicksand e ...

Turn off Chrome 69's autofill functionality

I've recently encountered an issue with Chrome's password autofill feature that has been troubling me for a few days now. This problem began when I was using Chrome version 69. After much trial and error, I found a solution by removing the id an ...