Uncertain about how to create a table with a fixed header as demonstrated

Can someone assist me in identifying the key styles necessary to create this fixed header table? I have found this example useful, but I am struggling to determine which styles are essential for achieving this effect.

I am also curious about why a DIV and padding are required for th td to achieve this?

<section class="">
  <div class="container">
    <table>
      <thead>
        <tr class="header">
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        <tr>
          <td>bgcolor</td>
          <td>rgb(x,x,x), #xxxxxx, colorname</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
        </tr>
        <tr>
          <td>border</td>
          <td>1,""</td>
          <td>Specifies whether the table cells should have borders or not</td>
        </tr>
        <tr>
          <td>cellpadding</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
        </tr>
        <tr>
          <td>cellspacing</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between cells</td>
        </tr>
        <tr>
          <td>frame</td>
          <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
          <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
        </tr>
        <tr>
          <td>rules</td>
          <td>none, groups, rows, cols, all</td>
          <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
        </tr>
        <tr>
          <td>summary</td>
          <td>text</td>
          <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
        </tr>
        <tr>
          <td>width</td>
          <td>pixels, %</td>
          <td>Not supported in HTML5. Specifies the width of a table</td>
        </tr>
      </tbody>
    </table>
  </div>
</section>


<style>
    html, body{
      margin:0;
      padding:0;
      height:100%;
    }
    section {
      position: relative;
      border: 1px solid #000;
      padding-top: 37px;
      background: #500;
    }
    section.positioned {
      position: absolute;
      top:100px;
      left:100px;
      width:800px;
      box-shadow: 0 0 15px #333;
    }
    .container {
      overflow-y: auto;
      height: 200px;
    }
    table {
      border-spacing: 0;
      width:100%;
    }
    td + td {
      border-left:1px solid #eee;
    }
    td, th {
      border-bottom:1px solid #eee;
      background: #ddd;
      color: #000;
      padding: 10px 25px;
    }
    th {
      height: 0;
      line-height: 0;
      padding-top: 0;
      padding-bottom: 0;
      color: transparent;
      border: none;
      white-space: nowrap;
    }
    th div{
      position: absolute;
      background: transparent;
      color: #fff;
      padding: 9px 25px;
      top: 0;
      margin-left: -25px;
      line-height: normal;
      border-left: 1px solid #800;
    }
    th:first-child div{
      border: none;
    }
</style>

Answer №1

Your question is a bit unclear. The code snippet you provided does not feature a fixed header element using the position: fixed property. It appears to be a standard table with data in it. The unique aspect here is that the user has added a new header section that overlays the existing thead, as seen in the following code:

<th>
     Table attribute name
     <div>Table attribute name</div>
</th>

The text "Table attribute name" is hidden using th {color: transparent;}, while the content of the div is displayed instead.

The div class="container" creates a scrollable container using:

.container {
    overflow-y: auto;
    height: 200px;
}

It seems like the user combined the use of a table and div so they could have a header with scrollable data beneath, maintaining proper alignment.

If you prefer a simplified version without tables, you can opt for this alternative code:

HTML

<div class="wrap">
    <div class="top">Title here</div>
    <div class="scroll">
        <div class="text">All your base are belong to us</div>
    </div>
</div>

CSS

 html, body{
      margin:0;
      padding:0;
      height:100%;
}
.wrap {
    margin: 0 auto;
    width: 600px;
}
.top {
    border: 1px solid red;
    background: lightgray;
    line-height: 50px;
    text-align: center;
}
.scroll {
    background: pink;
    height: 200px;
    overflow-y: auto;
}
.text {
    height: 1000px;
}

Feel free to check out the JS.Fiddle link for further experimentation. Hope this explanation helps clarify things.

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

Incorporating HTML into a WordPress Plugin

I am currently working on my very first WordPress plugin. The main purpose of this plugin is to display a message on the plugin page whenever a user saves or edits pages. However, I'm experiencing some issues with printing out these messages. Here&ap ...

Arrange a row of fifteen child DIVs in the form of bullets at the bottom of their parent DIV

Currently, I am working on customizing a slider by adding bullets. My goal is to create a gradient background for the parent div containing these bullets. However, when I try to increase the height of the parent div, all the bullets align themselves at the ...

Steps for creating a Bootstrap 4 numeric keypad template that automatically submits after entry of 4 characters

Struggling with the limitations of using only one id="" per element is a common challenge. Despite the difficulties faced in JavaScript and Ajax, there is an attempt to utilize them effectively. JavaScript is used to capture the #code id for user sign-in ...

Issue with the final transition of the last image in the Bootstrap carousel

I'm currently in the process of creating my own portfolio website. I've integrated a Carousel feature inspired by Bootstrap's example. However, I've noticed some glitches in the transitions between the first or second slides and the thi ...

What are the benefits of keeping JavaScript and HTML separate in web development?

Recently, I came across the concept of Unobtrusive JavaScript while researching best practices in the realm of JavaScript. One particular point that grabbed my attention was the idea of separating functionality (the "behavior layer") from a web page's ...

What is the process for loading this image?

While browsing through this stunning website, I came across a feature that caught my eye. Can someone please explain how the designer displayed the "The magic is loading" effect: The image vanishes once the site has finished loading completely. ...

How can I clear all jQuery toggled classes that have been added across the entire webpage in an Angular project?

Upon clicking a link within an element, the following CSS is applied: <a class="vehicleinfo avai-vehicle-info-anc-tag" ng-click="vehicleInfo($event)">Vehicle Info <span class="s-icon red-down-arrow"> </span> </a> $scope.vehic ...

An absolutely positioned element will always move within its relative container

After extensive searching on Google and Stack Overflow, I finally found what seemed like the perfect solution. The logic behind it made sense and I understood it perfectly. But when I implemented it, my absolute positioned divs kept moving whenever I resiz ...

How to center a div horizontally within another div using CSS

I'm attempting to center a block element horizontally on my webpage. Here's an illustration of what I'm aiming for: I can't use fixed div dimensions as the design needs to be responsive. Click here for the demo. Below is the HTML co ...

Does the Spanned Textview HTML formatting only work on the last item in the loop?

My current challenge involves looping through all elements and setting them to a TextView, while also formatting numbers to be subscript. Despite my attempts at using a loop, only the last number is correctly formatted. For instance, if I input Fe2Zn7Ag4, ...

What is the best way to pass a variable in PHP and create an HTML link to a specific element ID simultaneously?

Formulating the question may be challenging, but the concept is quite straightforward: I'd like to pass the variable 'count' using GET and simultaneously direct to the media section. On the page http://page.org.mx/comunicados#media I&apo ...

Navigate files on a Windows server remotely using a Linux Apache web server by clicking on a browser link

After setting up an apache webserver on Linux Debian and successfully creating an intranet, I encountered a challenge. The intranet is designed to display tables with data from sql queries using php and mysql. However, I wanted to include a hyperlink withi ...

What is the best way to eliminate vertical scrolling from a data table in Bootstrap?

My issue arises when fetching data using a while loop from the database and having a lot of entries in my data table, resulting in an unwanted scrollbar showing up. You can view a screenshot here. I have attempted the following to remove the scrollbar: ...

Awesome C# PDF Printing Solution [closed]

This question does not comply with our Q&A standards and guidelines. To maintain the integrity of our platform, we require answers to be factual, supported by references or expertise. However, this question may provoke debates, arguments, polls, or pro ...

jQuery Alert for Double-Checking Email Entry

Is it possible to create a simple alert pop-up using jQuery if the user does not enter the correct email address in the second email input form? Below is my current code: <div id="reservation-request"> <form method="post" action="test.php ...

JavaScript is unable to retrieve values from a dropdown menu with selectable options

After searching on Stack Overflow (jquery select change event get selected option), I attempted to implement the provided code but did not make any progress. As the options are changed, the values concatenate to create a variable that points to a specific ...

What could be causing the appearance of a mysterious grey box hovering in the upper left portion of my image and why is the code only adjusting the size of the grey box instead of the entire

I've been working on embedding some JavaScript into my Showit website to create a drag-and-drop feature that displays a collage/mood board effect. Everything is functioning correctly, but I'm running into issues with resizing the images. There&a ...

How can I use React Native to align two text tags differently within the same row?

In this particular scenario, I attempted to showcase two distinct elements within a list item. However, I encountered a challenge in figuring out how to display the "item.date" on the left side of the line, and the string "Click to show.&qu ...

Create a design where the logo seems to be suspended from the navigation bar using bootstrap

My goal is to achieve a navigation bar similar to the one shown in this image: Using Bootstrap 3, the code below is what I have implemented for my navigation: <nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <di ...

Issue with Angular 2 - Basic form validation no longer functioning

The Angular 2 application I've been working on includes a simple form with input fields and basic HTML validation. Here's an example: <form (onSubmit)="submit()"> <input type="email" /> <input type="submit" value="save" /> ...