Create a fixed navigation for multiple table header rows without using JavaScript, only CSS/bootstrap

My goal is to make multiple rows under the <thead> tag stick while allowing the rest of the table to remain scrollable.

I found an answer on SO that demonstrates how to make the header sticky using the position: sticky property, but it doesn't explain how to make multiple rows within <thead> sticky.

Simply using the CSS code mentioned in the link

thead th { position: sticky; top: 0; }
will only make the first row within <thead> sticky.

Any assistance would be greatly appreciated!

Answer №1

If the top values of both elements are identical, they will overlap each other. In this case, change the second header's top value to match the first header's height. See below for the code:

thead tr:nth-child(1) th { position: sticky; top: 0; }
thead tr:nth-child(2) th { position: sticky; top: 43px; }

Here, the value 43px represents the height of the first header.

Full Example
//HTML

<table id="customers">
<thead>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <th>Company 2</th>
    <th>Contact 2</th>
    <th>Country 2</th>
  </tr>
  // more table rows ...
</table>

//CSS

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}
thead tr:nth-child(1) th { position: sticky; top: 0; }
thead tr:nth-child(2) th { position: sticky; top: 43px; }

Click here for an example on jsfiddle

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

triangular shape at the top and rounded shape at the bottom combined with various colors

Is there a way to create a triangle with a rounded bottom section in a different color? #box { content:""; width: 0; height: 0; border-style: solid; border-width: 0 150px 150px 150px; border-color: transparent yellow red blue; ...

<a href> hyperlink with the web address as the anchor text

Can a hyperlink be generated using a variable instead of typing in static text like CLICK HERE, so that it displays the full URL of the page specified in the a href tag, regardless of the URL? < a href="http://sourceforge.net/projects/tcpick/,">CL ...

What is causing my 100% height to be excessively large?

I'm facing an issue with my background containers, "background-overlay" and "background-gradient", as they are extending the height of the document beyond the actual content displayed. I am unsure of what could be causing this problem. It might be som ...

Multiple columns contained within a span

I'm trying to create a panel that displays a list of values with corresponding labels. The requirement is for each label to be positioned above its respective value and aligned to the left. The layout should resemble the following: Label 1 Lab ...

The division of columns in the Bootstrap grid is malfunctioning

Recently, I embarked on my journey with Bootstrap and encountered an issue with the Grid Col System. I am aiming to center the logo and navigation bar on the page by dividing the Grid layout into 4 col-lg-4 (md-4). Subsequently, I inserted the image and na ...

Ensuring that an added row remains at the bottom of a table composed of rows generated dynamically by utilizing the .detach() method

I need to ensure that the "subtot" row is always appended as the last row in the table. When dynamically adding rows, the "subtot" row appears as the last row the first time, but does not stay at the bottom when more rows are added. Even though I want the ...

Ensure that the select boxes are aligned in a horizontal manner

I am currently attempting to adjust the alignment of select boxes to reduce the staggered appearance. While not all need to be perfectly aligned, it would be ideal to have those that are close enough in alignment. I prefer using HTML to accomplish this tas ...

Execute JavaScript code via URL injection

One interesting aspect of the HTML is that it has a feature where it opens a webpage. The specific webpage it opens is determined by the URL, for example: https://mywebsite.com/index.html&audio=disabled In addition to this, there is a useful JavaScri ...

CSS disappears after selecting menu in JSF application with PrimeFaces

Whenever I click on a p:menuitem and update the content of a layoutunit, the CSS style of the layoutunit disappears. Strangely, when I refresh the page, the style magically reappears. Can anyone explain why this happens? <p:layout id="plantillaPrincipa ...

Transfer an image to the top of a cell

I'm trying to position the image at the top of the cell, but I've hit a roadblock. Here's a preview of the web photo: https://i.sstatic.net/U3XK9.png Here is my CSS code: #animes { width: 130px; height: 100px; display: block; padd ...

Mouseover Magic: Text and Captions Come Alive with JQuery

I am trying to create a rollover effect that displays text when the user hovers over an image. Despite researching numerous tutorials, I have found limited resources on incorporating text or captions using jQuery. Most of the available tutorials focus on C ...

Concerning dilemma with a div element

I am using 2 different partial views in my main view: <div class="col-md-6"> @Html.Action("Chart", "Teams") </div> <div class="col-md-6"> @Html.Action("SeriesWinsChart", "Teams") </div> <div class="next-game"> & ...

best way to eliminate extra space from bootstrap container rows columns and div elements

I am currently working with Bootstrap and I am encountering some empty space on the left and right of my div. Instead of filling up the entire screen, it seems to only be taking up 99%. MY CODE: #social-links { background-color : #242424; /* border ...

Unexpected resizing of a div due to Vue animation

I'm currently working on a quiz and I'm trying to incorporate some animation effects when users navigate between questions. I've been experimenting with a fade in and out effect, but there seems to be a glitch. Whenever I click the button, t ...

Is there a way to verify if an <input/> element is flagged as invalid during testing?

Currently, I have a Selenium suite in place to test the (web) user interface. My main focus is on verifying that appropriate error messages are displayed when users enter invalid values. <input id="inputID" type="number" min="1" step="1" /> WebE ...

Include a clickable jQuery icon in an HTML document for testing purposes

Is there a way to include an icon [+] at the end of this statement, so that when clicked it triggers an alert? <div id="catalog" style="width: 518px;"> <ul id="catalog111" style="left: -37px;top: -140px;width=500%;"> < ...

"JavaScript that doesn't rely on a specific browser

Why does this code behave differently across browsers? What modifications are needed to ensure consistent behavior in all of them? In Firefox, it functions correctly, using the URL /some/path/?searchedText=sometext. However, in IE, nothing happens when cli ...

Exploring Google Go Templates for Creating Dynamic Websites using Multidimensional Arrays

In my struct example, I have a two-dimensional array: type Foo struct{ testArray[9][9] int } My goal is to access it using a template. For instance: tmpl.Execute(w, foo) //foo is a pointer to Foo struct and w represents the parsed html website How can ...

Adjust the value based on selection

I aim to display another div when either the Full Time or Part Time option is selected. Additionally, I would like to calculate a different value for each option; if 'Part Time' is chosen, PrcA should change to PrcB. Here is the code snippet tha ...

Is it advisable to blend Angular Material with Bootstrap?

I'm brand new to Angular Material and it seems to have its own intricate API. Coming from using Bootstrap, I'm accustomed to utilizing its grid system with classes like row, containers, cols, etc. I'm curious if it's considered a good o ...