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

Creating an atom without any boundaries: A step-by-step guide

While browsing the Atom forum, I stumbled upon a post discussing scripts for enhancing the UI of Atom. I'm curious about where to place these scripts. Should they go in the Atom init script? Any guidance would be highly valued. Visit this link for m ...

When using Angular with mdbootstrap, the mdb-tabs directive will move to the end if the ngIf condition is true

Currently facing a challenge with a significant amount of code here. It is referenced as follows: "ng-uikit-pro-standard": "file:ng-uikit-pro-standard-8.3.0.tgz", I am attempting to display a tab between 1 and 3 if a certain condition ...

Implementing overflow-x: hidden in React JS for enhancing mobile user experience

In my current React project, I encountered an issue where setting overflow-x: hidden in the index.css file for the body tag worked fine on desktop screens but not on mobile. I read that adding it to the parent element would be a better solution, however, ...

There seems to be an issue with the on() function in jQuery when using mouseover

I am trying to implement a small popup box that appears when I hover over some text, but for some reason it's not working as expected. Can someone please help me troubleshoot this issue? My goal is to display the content from the "data-popup" attribut ...

How can I ensure that a bigger sprite image fits inside a smaller div?

I have a unique situation where I am working with a large image sprite that is 1030px by 510px and contains two smaller images, each being 515px by 515px. My goal is to extract the two small images from the sprite and set them as backgrounds for two <im ...

Having trouble getting PostCSS nesting to work with CSS variables in Tailwind CSS and Next.js?

I've been attempting to utilize PostCSS nesting alongside CSS variables, but unfortunately the CSS variables are not being converted at all. Instead, I am seeing Invalid property value in the DOM for CSS Variables. The tailwind.css file I have inclu ...

The td data is appearing fragmented and not continuous in one line

Why is the td element on my webpage not appearing on a single line? The weekly report for XYZ is displaying on two lines, but I want it to be on one line. Why is the label content showing up on the next line? Output: Weekly Report for Testing project ...

Implement a counter in a JavaScript table, initializing it to zero

I have successfully written my code, but there is one issue. The first row is starting with the number one instead of zero. I'm looking for suggestions on how to start from zero. Any help would be greatly appreciated. Thanks! <script> var tabl ...

Blurring the edges of a div container

I have successfully faded the top of a div, but I am struggling to achieve the same effect for the bottom. I attempted to reverse the CSS properties used for fading the top, but it does not seem to be working as expected. HTML: <div class="container-c ...

Why does the flex-start and flex-end values correspond to the top and bottom positions when using the flex-direction property set

I am attempting to organize text and buttons into a row, with the buttons aligned on the right side and the text on the left side. My approach involves creating a flexbox layout and assigning items the appropriate classes for alignment. html, body { widt ...

Finding elements based on their class can be accomplished by using XPath

Can you show me how to effectively utilize the :not selector to choose elements in a scenario like this: <div class="parent"> <div class="a b c"/> <div class="a"/> </div> I am trying to select the div with only the class & ...

What is the best way to ensure that JavaScript is loaded in a specific sequential order?

I'm having trouble ensuring that JS files load in the correct sequence, despite my efforts to research async and defer on various platforms. This is the structure of my code: <script src="lang.js"></script> <!--loading either ...

When scrolling, the text or logo inside the navbar seems to dance with a slight wiggling or

I recently implemented a code to create a logo animation inside my navigation bar that expands and contracts as I scroll, inspired by the functionality of the Wall Street Journal website. However, this implementation has caused some unintended side effects ...

Align the button to the right within the form

Having difficulty aligning a button to the right using float: right; No matter what I try, the button with the "advanced-search-button" class refuses to move to the right. This issue is occurring in Bootstrap 4. HTML <link href="https://maxcdn. ...

Chrome failing to recalculate the width of an element as required

When attempting to size an element's width based on its height, I discovered what seemed like a clever solution using the transparent img trick: http://jsfiddle.net/6yc8a2yj/ The pink div displays correctly at a 1:2 ratio, but it fails to resize whe ...

Navigate through each element with a specific class name in a block of HTML code using

Currently, I am dealing with an HTML string that is being retrieved through AJAX. Let's assume it looks like this: var htmlString = '<div class="post"></div><div class="post"></div>'; I want to find a way to iterat ...

Incorporating A Full-Fledged Office Word Editor Into My Web Application

In our web project, we are looking to integrate a feature that allows users to create, edit, and save their word documents for reporting purposes. While Microsoft Office offers an online option here, it seems limited to working within the Microsoft OneDriv ...

Can the styling and variables of Bootstrap4 be altered dynamically?

I have recently started working on a project that utilizes Bootstrap4 for design and layout. However, there is now a need for the ability to change certain core styles (such as font-face, primary color, and background color) dynamically at runtime. Curren ...

Incorporating text sections into a div container and adjusting the width

Currently facing an issue with the canvas element on my project. <div id="app-container"> <div id="canvas-container"> <div id="canvas"></div> </div> </div> In the CSS stylesheet, the following styles ar ...

Is the in-app browser of WeChat able to support self-signed HTTPS URLs?

My local WAMP server hosts a web application with self-signed SSL enabled, resulting in the following app URL: https://myipaddress:port/demoapp/index.html However, when I send this URL via WeChat chat and click on it, only a blank page is displayed. Stra ...