Preserving the position of the header and footer while utilizing a combination of grid layout, Bootstrap, and handling

How can I ensure that the page layout does not allow scrolling for the entire page height (100vh), but allows scrolling within a table if necessary? The header and footer should remain fixed at the top and bottom of the page respectively, unaffected by the content of the table. Even if the table content exceeds the available vertical space, the footer should not get pushed down but instead provide scrolling within the table.

The current setup consists of a root div with a Bootstrap4 navbar, followed by a page_container utilizing a grid layout for its internal sections: header, content, footer.

Here is a JSFiddle link to a layout with a table that stays within the page height: https://jsfiddle.net/kvdyg4q5/

And here is another JSFiddle link to a layout with a table that exceeds the page height: https://jsfiddle.net/1nv7hqg6/

In the second example, you can see that the table pushes the footer down, which is the issue that needs to be addressed.

CSS Styles Used:

<style type="text/css">
    #root {
      height: 100%;
      min-height: 100vh;
      max-height: 100vh;
    }

    .page_container {
      max-height: calc(100vh - 70px);
      height: calc(100vh - 70px);
      min-height: calc(100vh - 70px);
    }

    .table_container {
      display: grid;
      height: 100%;
      grid-template-rows: auto 1fr auto;
    }

    .table_body {
      display: block;
      height: 100%;
      overflow-y: scroll;
    }
</style>

HTML Structure:

<body>
<div id="root">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark"><a class="navbar-brand" href="#">Company</a>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar1</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar2</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar3</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar4</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar5</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar6</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar7</a></li>
                <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar8</a></li>

            </ul>
        </div>
    </nav>
    <div style="padding: 6px;">
        <div class="page_container">
            <div class="table_container">
                <div>
                    <h1>HEADER</h1>
                </div>
                <div>
                    <div>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th scope="col">Column 1</th>
                                    <th scope="col">Column 2</th>
                                    <th scope="col">Column 3</th>
                                    <th scope="col"> Column 4 </th>
                                    <th scope="col">Column 5</th>
                                </tr>
                            </thead>
                            <tbody class="table_body">
                                <tr>
                                    <td colspan="5">
                                        <h3>Row</h3>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="5">
                                        <h3>Row</h3>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="5">
                                        <h3>Row</h3>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="5">
                                        <h3>Row</h3>
                                    </td>
                                </tr>
                                <!-- more rows...-->

                            </tbody>
                        </table>
                    </div>
                </div>
                <div>
                    <h2>Footer</h2>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №1

You don't have to rely on CSS grid for this particular layout. Instead, you can utilize flexbox and set the content to flex:1 0 0 with overflow:auto

[root] {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

[page] {
  flex: 1;
  display: flex;
  flex-direction: column;
}

[content] {
  flex: 1 0 0;
  overflow: auto;
}


/* Sticky Header */
table {
  position: relative;
}

th {
  position: sticky;
  top: 0;
  background: black;
  color: white;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<div root>
  <div navbar>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark"><a class="navbar-brand" href="#">Company</a><button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar1</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar2</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar3</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar4</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar5</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar6</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar7</a></li>
          <li class="nav-item"><a id="vehicles_navbar" class="nav-link" href="#">navbar8</a></li>

        </ul>
      </div>
    </nav>
  </div>
  <div page>
    <div header>
      <h1>HEADER</h1>
    </div>
    <div content>
      <div>
        <table class="table">
          <thead>
            <tr>
              <th scope="col">Column 1</th>
              <th scope="col">Column 2</th>
              <th scope="col">Column 3</th>
              <th scope="col"> Column 4 </th>
              <th scope="col">Column 5</th>
            </tr>
          </thead>
          <tbody class="table_body">
            <tr>
              <td colspan="5">
                <h3>Row</h3>
              </td>
            </tr>
            <!-- more rows here -->
          </tbody>
        </table>
      </div>
    </div>
    <div footer>
      <h2>Footer</h2>
    </div>
  </div>
</div>

Answer №2

To optimize the layout, consider adding a class to the section housing the footer and setting its position to be fixed.

.footer-section {
  position: fixed;
  left: 0;
  bottom: 0;
}

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

What is the best way to switch a set of buttons on and off?

I have a set of primary buttons, each with a group of nested secondary buttons. When a primary button is clicked, I want only its corresponding secondary buttons to be displayed. If the same primary button is clicked again, all its secondary buttons shoul ...

Localhost is fine, but Github pages are just not cooperating with Bootstrap

I am currently in the process of building a portfolio website using Bootstrap 4, animate.css, typed.js and my own custom css. When testing the site on localhost through VS Code, everything appears to be functioning correctly. However, upon viewing the sit ...

When floating a left and right div block, they do not necessarily align on the same line

I am looking to create a nested menu where some of the menu items have key shortcuts that I want to align to the right side on the same line. I attempted to use float left/right, but encountered an issue where the shortcuts were shifted to the next line. H ...

Prevent access to website during execution of background tasks

Whenever a user visits the homepage, I need to verify that default values on the database are in place (if this is not the correct location for the check, please advise). Here's what needs to happen: Determine if the database is empty (if not, skip ...

I'm puzzled as to why there is a blank space in the false element statements

Hey there! I'm noticing a blank space on the page, and when I inspect it, I see this bindings={ "ng-reflect-ng-if": "false" }. It seems like I am getting some blank cards displayed. Here is an image showing what I mean: https://i. ...

Ensuring Bootstrap Table Width Covers Entire Screen with Thymeleaf and Spring Boot

Trying to implement Bootstrap in my HTML Thymeleaf template with the following code: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85434c4c404747584945685f484c5f1c535f4748554c5f ...

Is it true that calc() functions exclusively with width and not height?

Recently, I attempted to vertically center a div on a page with a height of 100vh. The div has a height of 84px, so I tried using the following CSS: margin-top: calc(50% - 42px); Unfortunately, it appears that the browser always considers the width of t ...

To add a <figure> tag before and after the <img> tag using PHP, simply include the opening <

My goal is to surround the image tag with a figure tag at the beginning and end. This is my current approach. <?php $v['content_en'] = '<p><br /><img alt="" src="https://66.media.tumblr.com/9dc27741114bd854f29fc65dc33 ...

Pressing a button triggers the highlighting of a tab in HTML through the use of Javascript

In the corner of my webpage, I have three tabs: info, question, order. When I click on one tab header, only that tab should highlight. The info section includes two buttons that link to the question and order tabs. When these buttons are pressed, the respe ...

Tips for resolving the issue of a Bootstrap dropdown menu overlapping content in its vicinity

I am trying to position a Bootstrap dropdown button on the right-hand side of the page. The dropdown should cover 100% of the page's width and overlay any content when activated. However, there is a large logo on the left-hand side that I want users t ...

Why did the creators choose to integrate a dropdown menu into Twitter Bootstrap?

While exploring Twitter's Bootstrap framework, I couldn't help but be impressed. However, one feature that has left me a bit puzzled is the dropdown navigation menu. First of all, to view child links, you need to click on the parent. I understan ...

The transitionend event fails to trigger upon losing focus

When I attach a transitionend event listener to element using the code below, everything works smoothly: element.addEventListener('transitionend', transitionEnd); function transitionEnd() { console.log('transitionEnd fired') ...

Adjustable Text Size with HTML5 and CSS3

Looking to enhance the accessibility of a website by implementing text size adjustment buttons ' A A A ' using just HTML5 and CSS3. Avoiding the use of jQuery or Javascript for this task. Inspiration drawn from this link. Appreciate any assistan ...

Obtain the value for every span class and insert the corresponding string into the input field

I have a series of tags labeled as 'tag-label' in spans. My goal is to combine the values of these tags and insert them into an input field, separating each value with commas. To demonstrate this, I've created a JSFiddle that showcases the ...

Select every p element that comes after an h2 tag using CSS

Is there a way to select all paragraph elements that come after a specific heading? <h2 class = 'history'>History</h2> <p>this is paragraph 1</p> <p>this is paragraph 2</p> <p>this is paragraph 3</p&g ...

Switch between the table data elements in an .hta document

The response provided by Dr.Molle in a previous post here was accurate, but it only functioned with <div>. I am required to utilize <table>. Though I found a script that works flawlessly outside of my VBScript, it does not work when embedded in ...

Refresh the HTML content as soon as a modification is detected in the MySQL Database

Apologies if this sounds like a basic question, but I've hit a roadblock in my search for answers on the vast expanse of the internet... I am in the process of constructing a website that requires certain data on the page to be updated automatically. ...

The CSS content property within a style element prevents the dragging of elements

Is it possible to make an element with content draggable in HTML while changing the image through replacing the style of the element? I am trying to insert a picture using the content style tag, but it seems to make the element lose its draggable ability. ...

Missing information in input field using JQUERY

I've been attempting to extract a value from an input tag, but all I keep getting is an empty string. Upon inspecting the frame source, it appears as follows: <input type="hidden" name="" class="code_item" value="00-00000159" /> In order to re ...

Navigate between links by using the Tab key, while excluding certain links from the sequence

On my webpage, I have a main menu, various links in the main content area, and a left menu. The challenge is to make the website accessible for people who are blind... When using the tab button to navigate between links, the order currently goes as follow ...