Create a static div and a separate scrolling div

I am looking to achieve a layout where the top div is fixed at the top of the screen and the second div is scrollable below it. Both divs should have the same column width.

I attempted to use position: fixed, but the columns are not aligned directly beneath each other as desired.

The current issue can be seen here:

https://i.sstatic.net/qHKLy.jpg

This is the desired outcome:

https://i.sstatic.net/XR2oh.jpg

Here is an example of the code:

td,
th {
  width: 4%;
  text-align: center;
  vertical-align: middle;
  word-break: break-word;
}

.mainContainer {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 0 1rem 0 1rem;
  background-color: blueviolet;
}

.fixed {
  position: fixed;
  height: 7rem;
  padding: 0 1rem 0 1rem;
  background-color: orange;
}

.scrollable {
  margin-top: 7rem;
  background-color: lightgreen;
}
<div class="mainContainer">
  <div class="fixed">
    <table style="table-layout: fixed;">
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
    </table>
  </div>
  <div class="scrollable">
    <table style="table-layout: fixed;">
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
    </table>
  </div>
</div>

Answer №1

This solution should suit your needs perfectly. What I did was tidy up everything and added scroll functionality to the mainContainer. Just make sure that any content you want to appear below the scrollable div remains within the mainContainer div.

td,
th {
  width: 4%;
  text-align: center;
  vertical-align: middle;
  word-break: break-word;
}

.mainContainer {
  display: flex;
  flex-direction: column;
  width: 100%;
  background-color: blueviolet;
  max-height: 170px;
  overflow-y: scroll;
}

.fixed {
  position: sticky;
  top: 0;
  height: 7rem;
  padding: 0 1rem 0 1rem;
  background-color: orange;
  max-width: 100%;
}

.scrollable {
  background-color: lightgreen;
  padding: 0 1rem 0 1rem;
  max-width: 100%;
}

body {
  overflow-y: hidden;
}
<div class="mainContainer">
  <div class="fixed">
    <table style="table-layout: fixed;">
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
    </table>
  </div>
  <div class="scrollable">
    <table style="table-layout: fixed;">
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
      <tr>
        <td>Company 1</td>
        <td>Contact 1</td>
        <td>Country 1</td>
      </tr>
    </table>
  </div>
  <p style="color: #fff; text-align: center;"> additional content here still nested in the main-container div </p>
</div>

Answer №2

Give this a shot:

.fixed {
    position: sticky;
    top: 0px; 
    height: 7rem;
    background-color: orange;
}

.scrollable {
   
    background-color: lightgreen;
}

Check out the code here!

UPDATE: Adjusted the top value from 1px to 0px.

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

Shift content from side to side with a click

I've been attempting to shift content left and right using an onclick event and I need it to stop at the margins on the left or right. The list-items are generated dynamically. Here's the code I've tried so far, but it's not quite worki ...

Why is it that aligning items in a row doesn't seem to function properly when I apply a custom class to a container-fluid in Bootstrap 4?

Recently, I started delving into the world of front end development. After mastering the basics of HTML5 and CSS3, I ventured into Bootstrap 4. My current project involves creating a replica of the Facebook login page. To achieve a full-width design, I uti ...

Design and styling with HTML5 and CSS

Having a small issue with my code as I venture back into coding after a long hiatus, resulting in me forgetting some basics. Currently, I am attempting to create a simple HTML layout. Upon inspecting the page, I noticed that it appears slightly longer tha ...

Generate a list of files and transfer them to an input file

I am currently working on creating a drag and drop area where I can retrieve dataTransfer items using webkitGetAsEntry, and then differentiate between directories and files. My goal is to convert these files into a FileList and transfer them to a file inp ...

Make the Angular Modal scrollable except for when a specific div is being click dragged

I am working on an Ionic app that uses AngularJS. One issue I encountered is with a modal popup that can sometimes extend beyond the visible area. To address this, we applied overflow: hidden to its CSS class. However, there is a specific functionality i ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

Creating an app using Ionic 1 that features scrollable tabs with the ability to handle overflow

My current code snippet is displayed below. On smaller mobile screens, all 7 tabs are not visible at once and instead appear in two rows which looks messy. I want to modify the display so that only 3 tabs are visible at a time and can be scrolled through. ...

What do I do when I encounter the error message "Provider not found for RadioControlRegistry" in Angular 2? Which provider should

Which provider should be included in the provider component? <div class="radio"> <input let match id="male" type="radio" name="gender" value="true" [(ngModel)]="isMatching" (click)="isMatching(match.value)" > Take a look at my console output ...

What is the best method to override the CSS transition effect with JavaScript?

I am trying to implement a CSS transition where I need to reset the width of my box in advance every time my function is called. Simply setting the width of the box to zero makes the element shrink with animation, but I want to reset it without any animati ...

Prevent the caching of the cache manifest file in IIS to avoid issues with browser caching

Looking for some advice on adding a cache manifest to my web app. I'll be deploying the app on IIS and need help making sure "that the cache manifest file is not cacheable by HTTP semantics" (source: ) Does anybody have tips on preventing a .appcache ...

How to align the navbar toggle button and list items to the right in Bootstrap 5

I'm struggling with a simple html page that has a fixed navbar at the top. Everything looks great when viewed in full-screen mode, with centered links. However, when the page size is reduced, it collapses to a toggle button on the left side. What I re ...

Does Blazor offer a GetElementById method for C#? Alternatively, can you help me understand why my Checkbox remains checked?

I need to modify the "checked" attribute of a checkbox input element based on a model's property. The challenge arises when this "checked" attribute is part of a random list of li-elements generated by a list of models representing todo elements. This ...

Identify when a div reaches the end of the screen using CSS

I've developed a custom dropdown feature using Vue 2 and TypeScript to meet our specific requirements (without relying on jQuery). The dropdown functions correctly, opening the options list downwards when the main box is clicked. One enhancement I a ...

Using jQuery to insert a div class with PHP referenced

My variable stores a color name. Here is an example: <?php $myvar = blueColour; ?> I want to apply this value to the body of an html page using jQuery: <script type="text/javascript"> jQuery(body).addClass("<?php echo $myvar; ?>"); < ...

Issue with Google Adsense - adsbygoogle.push() error: Pages can only support one AdSense head tag. The additional tag will be disregarded

After adding my script tag to the navbar component, I encountered an error on my site during the next js build: Google Adsense error -adsbygoogle.push() error: Only one AdSense head tag supported per page. The second tag is ignored <Head> ...

Creating a table from a PHP associative array

I've been attempting to organize an associative array in ascending order and then display it in an HTML table, but I've hit a roadblock with an error. I tried searching for solutions here on SO and followed the advice provided in some threads: P ...

history.js - failure to save the first page in browsing history

I have implemented the history.js library to enable backward and forward browser navigation on an AJAX products page triggered by clicking on product categories. While I have managed to get this functionality working smoothly, I am facing a particular iss ...

Using PHP to display dynamic data and add it to an HTML element

I have a unique situation where I am storing html elements, specifically <rect> svg elements, as strings in a database. In my PHP code, I retrieve and echo this data: $db = getConnection(); $statement = $db->prepare('SELECT `copy` FROM `dra ...

style for a bootstrap form input

Can the form-control from Bootstrap be made inline for input without relying on the grid system like col-md-2? This is the one aspect of Bootstrap that I find particularly frustrating. ...

changing the value of a text input based on the selected option in a dropdown using ajax

I attempted to update the text input value with data from the database after selecting an option from the dropdown list. However, I encountered an issue where the data is not populating the text input field. <?php //Including the database configuration ...