Footer at the bottom and a sticky navigation bar

I have experimented with various CSS guides in order to place the footer at the bottom of the page, and I have found the example below to be the simplest method.

html,
body,
#root {
  height: 100%;
}

#root {
  display: flex;
  flex-direction: column;
}

nav {
  position: sticky;
  top: 0;
}

section {
  flex: 1;
}

section>div {
  height: 1000px;
}
<div id="root">
  <header>Header</header>
  <nav>Navigation</nav>
  <section>
    <div>Test</div>
    <div>Test</div>
    <div>Test</div>
  </section>
  <footer>Footer</footer>
</div>

This method successfully places the footer at the bottom of the page, but sometimes the navigation bar does not scroll properly.

The issue may be due to setting the height of the #root element to 100%.

While I can speculate on "why" this problem is occurring, I am unsure of "how" to resolve it while keeping the nav element at the top of the page.

Any assistance would be greatly appreciated.

Answer №1

I believe you are very close to the solution. Simply swap out height:100%; with min-height: 100vh; for the #root element. Additionally, add margin-top: auto; to the footer element.

html,
body {
  height: 100%;
  margin: 0; /*Resetting the default browser margin*/
}

#root {
  display: flex;
  flex-direction: column;
  min-height: 100vh; /*Ensuring the height covers the entire view port*/
}

nav {
  position: sticky;
  top: 0;
}

section {
  flex: 1;
}

footer {
  margin-top: auto; /*Pushing the footer to the bottom of the page*/
}
<div id="root">
  <header>Header</header>
  <nav>Navigation</nav>
  <section>
    <div>Test</div>
    <div>Test</div>
    <div>Test</div>
  </section>
  <footer>Footer</footer>
</div>

Answer №2

Implement a JavaScript function to dynamically add the class sticky to the navbar when the user scrolls down the window. This class will give the navbar the following CSS properties:

  • position: fixed to fix the navbar in place;
  • top: 0 to keep it at the top of the page;
  • width: 100% to span the entire width of the screen.

var navbarElement = document.getElementById("navbar");
var navbarScroll = navbarElement.offsetTop;

window.onscroll = function() {
  addClassOnScroll()
};

function addClassOnScroll() {
  if (window.pageYOffset >= navbarScroll) {
    navbarElement.classList.add("sticky")
  } else {
    navbarElement.classList.remove("sticky");
  }
}
html,
body,
#root {
  height: 100%;
  margin: 0;
}

#root {
  display: flex;
  flex-direction: column;
}

header {
  padding: 15px
}

nav {
  top: 0;
  background-color: yellow;
}

section {
  flex: 1;
}

section>div {
  height: 1000px;
}

footer {
  padding: 15px;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}
<div id="root">
  <header>Header</header>
  <nav id="navbar">Navigation</nav>
  <section>
    <div>Test</div>
    <div>Test</div>
    <div>Test</div>
  </section>
  <footer>Footer</footer>
</div>

Answer №3

Based on the structure of your HTML, it is recommended not to apply any additional styling to the HTML and body elements. Instead, focus on resetting your CSS styles if necessary.

An easy way to achieve this without using JavaScript is by setting the #root element to have a fixed position with a height and width of 100vh and 100vw, respectively. Additionally, ensure there is an overflow property set to auto.

* {
    scroll-behavior: smooth;
    margin: 0;
    padding: 0;
    text-decoration-line: none;
    border: 1px dashed;
}

#root {
    height: 100vh;
    width: 100vw;
    position: fixed;
    background: lightblue;
    display: flex;
    flex-direction: column;
    overflow: auto;
}

nav {
    position: sticky;
    top: 0;
}

section {
    flex: 1;
}

section > div {
    height: 400px;
}
<div id="root">
    <header>Header</header>
    <nav>Navigation</nav>
    <section>
        <div>Test</div>
        <div>Test</div>
        <div>Test</div>
    </section>
    <footer>Footer</footer>
</div>

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

Steps to activate highlighting for the initial value in a quarterly datepicker

Concerning the quarterPicker feature in the Bootstrap datePicker (SO: how-to-change-bootstrap-datepicker-month-view-to-display-quarters, jsfiddle: jsFiddle): Is there a way to highlight an initial value upon startup? I have tried setting a value in win ...

HTML5 canvas processing causing web worker to run out of memory

Within the Main thread: The source image array is obtained using the getImageData method. It is represented as a uint8ClampedArray to store the image data. Below is the code executed in a web worker: (This operation generates a high-resolution image, but ...

Looking to add a dynamic divider between two columns that can be adjusted in width by moving the mouse left and right?

If you're looking for an example of two columns adjusting their width based on mouse movement, check out this page from W3Schools. I'm trying to implement this feature in my React app, but I'm unsure of how to proceed. Below is the JSX code ...

The application of CSS transition fails in the context where top property is set as auto

I have been exploring an online tutorial that almost met my requirements. However, I encountered a challenge with the CSS 'transitions' effects. Basically, I need the text to be positioned at a specific distance from the top because the title wi ...

Creating designs on the canvas

With this code snippet, I have the ability to create lines using mouse points on a canvas. My goal is to identify when these lines connect to form a shape and then fill that shape with color. <script language="javascript" src="//ajax.googleapis.com/a ...

What is the best method for ensuring three inside divs have equal heights?

I am working with a container div#content that holds three inner divs. I want to ensure that all three divs have the same height, but also expand based on their content. Here is an example of what I have tried: <!doctype html public "-//w3c//dtd html 4 ...

Incorrect data for minimal values within Google charts

Currently, I am utilizing a Google chart found at the following link: In my data set, I have significantly high values for the line chart and relatively low values for the bar chart. In order to make both types of data more easily readable, I would like t ...

Monitor the traffic on my website by implementing .htaccess restrictions

I am maintaining a website with restricted access to specific IP addresses listed in the .htaccess file. I am looking to implement a logging system that tracks unauthorized attempts to access the site. Is it feasible to record each attempt in a database? ...

The Electronjs application encountered an error while loading with the message "ReferenceError: require is not defined."

Having trouble with an electron application as I encounter an error when running npm start: $ npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c087966786678">[email protected]</a> start C:& ...

switch the visibility of the p tag based on its content

It seems like solving this shouldn't be too challenging, but I'm still learning when it comes to Javascript or JQuery. HTML: <p><span id="AddLine1Summary"></span>,</p> <p><span id="AddLine2Summary"></span& ...

Is there a way to potentially utilize window.open() to open a specific section of a webpage?

My HTML page consists of 2 div blocks and 2 links. I want to open the content of the first div in a new window when the first link is clicked, and the content of the second div in another new window when the second link is clicked. You can find the code h ...

Combine all input values into one using jQuery

I have four text boxes and I want to append the value of each box to a certain text box with a comma on KeyUp function, in addition to the default value. The issue is that it keeps overwriting the previous value. I am looking for an output like this: 0 ...

CSS Trick: Applying first-of-type selector to specific instances of a div

I'm currently grappling with how to specify that a 'first-of-type' should only be present on specific instances of a div. For instance, suppose I have a class called ".maintext" and in some cases I want the first paragraph to feature a dropc ...

Adding elements to a roster

Can anyone assist me with appending data from a csv file to an unordered HTML list? The csv file I have contains both single and grouped data for the list, as seen below: Test Group Name A,Test Link Name 1,Test URL 1 Test Group Name A,Test Link Name 2,Tes ...

What are some effective methods for dynamically styling elements during iteration?

Need assistance with styling dynamic elements during iteration Located on the test.phtml page <?php foreach($tests => $test) { ?> <li class="<?php echo $test['class'] ?>"> <a href="#" title=""> Test In ...

When I try to add more content to my page, it doesn't extend beyond a single page and instead gets compacted together

As a beginner in the world of coding, my goal is to create a time management website specifically tailored for school use. Despite copying this code multiple times, I have encountered an issue where it does not continue down the page, and I am unsure of th ...

Interacting with Watir and Cucumber, unfortunately the link is not functional

Being new to the world of Watir and Cucumber, I am currently in the process of running an automation script to create Live IDs. The link that I want to click on the webpage is labeled "New" and it should take me to a form where I can add a new contact for ...

Is the Packery image grid only functional when the background image is specified in CSS and not in JavaScript? Perhaps we need to look into using Await/Sem

I've successfully implemented a packery image grid that is responsive and functional when the background image in the .item-content section is defined in the CSS file: http://codepen.io/anon/pen/eJdapq .item-content { width: 100%; height: 100%; ...

CSS guidelines specifically for when two classes are required to exist simultaneously

Is there a specific effect that can only be achieved when two classes are present simultaneously? For example: .positive{ color:#46a546; } .positive:after{ content: "\25b2"; } .negative{ color:#F00; } .arrow:after{ content: "\25bc"; ...

Mysterious attributes of angular 6's <table mat-table> tag

This particular question regarding the angular material table has not been duplicated in any other discussions. Other similar questions pertain to angular versions 2-5, not version 6 The issue I am encountering is as follows: Can't bind to 'dat ...