Ensure that the div and table header remain in a fixed position when scrolling

I have a container with a dropdown at the top and a table at the bottom. I am implementing ngx-infinite-scrolling feature, and I want the container and the table header to remain fixed when scrolling down as more data is loaded.

You can view the jsfiddle example here.

Here is the HTML code snippet:

 <div class="topTable row ">

  <div class="col-md-4 col-lg-3 col-sm-4 col-6">
  <form class="form-inline">
      <div class="form-group" style="width:100%;max-width:200px;">
        <div class="input-group" style="width:100%">
            <select class="form-control"  >
                <option >All</option>
                <option >Option1</option>
                 <option >Option1</option>
                  <option >Option1</option>
              </select>

        </div>
   </div>


 <div class="container-fluid">
   <div class="col-md-12">
  <div class="col-md-12 tableDiv"  >
  <div class="constrained"  >
  <table class="table table-striped col-md-12 table-condensed cf" >
  <thead class="table-bordered cf">
    <tr>
      <th>Column1</th>
      <th>Column2</th>
      <th>Column3</th>
      <th>Column4</th>
      <th>Column5</th>
      <th>Column6</th>

    </tr>
  </thead>
  <tbody >
    <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>
        <tr>
      <td > asdf</td>
      <td >asdfa</td>
      <td >fsdf</td>
      <td>sdfsfd</td>
      <td>sdfs</td>
      <td>fsdfsdf</td>
    </tr>

   </tbody>
  </table>
 </div>
  <div>
  </div>
 </div>
</div> 
</div>

CSS:

.topTable{
 height:70px;
background:red;
margin-top:-20px;
margin-bottom:20px;     
display: flex;
padding-left: 32px;
border:1px solid rgba(0,0,0,.1)
}

.tableDiv{
margin-top:50px
}

I attempted using position:static on the container, but it caused the table to overlap when scrolled down. Any suggestions on how to make both the container and its table header fixed are welcome. Thank you.

Answer №1

To maintain a stable layout, it is important to separate the top-block from the content-block.

By making the first block fixed with a width of 100% and a z-index greater than the content block (default is 0), you can achieve the desired effect.

position:fixed;
width:100%;
z-index: 1;

I've made changes to your fiddle: http://jsfiddle.net/n958b7q3/1/ where necessary adjustments have been noted.

Here are some tips:

html

  • Avoid excessive use of divs; only create new ones when absolutely required
  • Ensure all tags, such as forms, have proper closing tags like </form>

css:

  • Remember to include a ; after border:1px solid rgba(0,0,0,.1)

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

The functionality of CSS scroll snap does not seem to be compatible with the CSS Grid

When utilizing CSS scroll snap with Flexbox, the snapping functionality works adequately: * { box-sizing: border-box; margin: 0; padding: 0; } .slider { font-family: sans-serif; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vw ...

Is there a way to incorporate multiple dynamic classes in CSS modules react?

I have integrated CSS Modules import style from "./styles/question.module.css"; // status[0] can range from 0 to 4 <p className={style[`difficulty_${status[0]}`]}>{difficulty}</p>, The code above is functional, however the following ...

Is the ID selector the quickest method in jQuery and CSS?

Which is the optimal choice in jQuery/javascript for speed? $('#myID .myClass') or $('.myClass') What is the preferred option to utilize in CSS? #myID .myClass{} or .myClass{} In hindsight, I realize my explanation was insuffici ...

How to Alter Button Color upon Click in React with Bootstrap?

I'm working on implementing a thumbs up and thumbs down feature similar to Reddit's upvote and downvote system. The goal is to change the color of the object to green when the thumbs up is clicked and to red when the thumbs down is clicked. How ...

CSS almost there!

Below is the HTML code I am using: <div class="ObjectList" id="ObjectList"> <p class="ObjectListTitle" id="Element">TEST</p> </div> The CSS is applying properly to the ObjectList class but is not working for the ObjectListTitl ...

Tips on managing scrollbar behavior in Angular UI Grid

In my current project, I am utilizing Angular UI Grid. The grid automatically resizes itself so that all columns fit within a specific div horizontally. While this feature works well initially, it becomes problematic when there are more rows than can fit ...

Trouble ensues with integrating Magic CSS3 animations via jQuery due to malfunctioning

I've been experimenting with some magical CSS classes from a source I found online (http://www.minimamente.com/magic-css3-animations/) and trying to apply them using jQuery on mouseenter and mouseleave events. Unfortunately, it's just not working ...

What is the process to modify the font color on a webpage using a button?

I need some help figuring out how to change the font color on my website when someone clicks a button. I already have the background functionality working, but I can't seem to get the text color to change. This is the code I have so far: <div cl ...

Adding color to characters, digits in an HTML file

Is it possible to individually style each letter, number, and symbol in an HTML document with a unique color? I am interested in creating a text editor that applies specific colors to each glyph for those who have grapheme-color synesthesia. While there ar ...

The paragraph tag closes automatically once opened

In the process of establishing a news section on my website, I encountered an issue while using the following code: <p id="news-container"> <h1> . . . </h1> <strong>big</strong> . . . </p> Upon inspecting, it appea ...

What could be the reason for the absence of the CSS destination folder being generated by

My colleague has the exact same code and can run gulp without any issues, but I'm struggling to compile the css files using Gulp. Gulp isn't creating the css destination file or the css files themselves. However, it works perfectly fine for build ...

CSS inset box-shadow creates a gap between collapsed borders

I need help with a strange issue I'm facing. I have a table where border-collapse is set to collapse, and there's an unusual gap between the box-shadow inset and the border. The size of this gap increases as the border width gets larger. How can ...

Maintain the flow of a floated or absolutely positioned element using CSS

Is there a way in CSS to ensure that floated or absolutely positioned elements remain within the flow? I find it frustrating that CSS lacks something like flow:on and flow:off to control this. The main issue I am facing is with having a div element of var ...

The child block element in Internet Explorer 7 expands independently of the parent's padding, ignoring

I'm having trouble understanding this situation. There seems to be an issue with a block element inside another block element. The child element is expanding its height to 100% and overlapping the bottom padding of the parent, ignoring it completely. ...

What is the best way to trigger a function with a bootstrap toggle?

A toggle switch has been implemented using bootstrap with the following code snippet: <label > Automatic Refresh: </label> <input class="pull-right" data-size="mini" type="checkbox" data- toggle="toggle"> When the toggle button is in ...

Showing tags with varying sizes

https://i.sstatic.net/oo3MP.png Is there a better way to organize and display my content? I have an array of elements that I want to display like the example above. I attempted using lists, but it didn't work out as expected. Here's what I&apo ...

The height of a DIV element can vary based on

Looking at this div structure: DIV3 has a fixed height. The nested DIV5 will be receiving content from Ajax, causing its height to change. Additionally, there are some DHTML elements inside it that also affect the height. DIV5 has a fixed min-height set. ...

Changing the background color of a React styled-component when hovered over

Is there a way to create a custom div with an interactive hover effect that changes the background color? I am using styled components and passing in the color as props, but for some reason, the hover effect is not functioning correctly. const ColorBlock = ...

Scale up the font size for all text on the website uniformly

Recently, I decided to switch a website's font to a non-web typeface. However, I quickly realized that the font was extremely thin and difficult to read. I attempted a simple CSS fix using * { font-size:125% }, but unfortunately, it did not have the d ...

Resolving odd SVG anomalies

I recently exported three SVGs from Sketch to include in HTML, but one of them (specifically the Twitter icon) is presenting a mystery with its appearance. All three SVGs were created in the same manner, using #999999 as the stroke color and having a stro ...