Header and footer that stick in place while content scrolls automatically

To create a layout where the header and footer remain sticky while the content has a minimum height and shows a scrollbar when the viewport is too small, I found success with this codepen: http://codepen.io/DlafCreative/pen/wzdOEN inspired by another source.

However, I ran into issues with the HTML markup not fitting well with the Angular2 framework I am using. The <header> being on the same level as <main>, and the <footer> outside <prop-wrapper>.

In my application, the header needs to stay consistent across all pages, while the footer should be dynamic. Ideally, I would like the <header> before <prop-wrapper> and the <footer> after <main> as shown in this forked codepen: http://codepen.io/DlafCreative/pen/xEAXxy. This structure would make it easier to define the content.

I am currently struggling to achieve the same behavior with the new markup. Any help or suggestions would be greatly appreciated. Thank you!

Answer №1

Here is a helpful guide on how to achieve the desired layout:

  1. To start, remove the default margin of the browser by adding the following CSS:

    body {
      margin: 0;
    }
    
  2. Turn your prop-body into a flexbox with display: flex. Set flex-direction: column for a vertical layout and use height: 100vh to occupy the entire screen height:

    prop-body {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    
  3. Apply flex: 1 to prop-wrapper to fill the space between the header and footer.

    This shorthand implies:

    a. flex-grow is 1 b. flex-shrink is 1

    This ensures that the height of this flex item adjusts to fill any remaining space.

Refer to the meaning of flex: 1 here.

flex: Equivalent to flex: 1 0. This makes the flex item flexible and sets the flex basis to zero, allowing it to take up the specified proportion of free space in the flex container. When all items in the flex container follow this pattern, their sizes will be proportional based on the assigned flex factor.

  1. You can now visualize the layout easily as we utilize nested flexboxes for organization.

body {
  margin: 0;
}
prop-body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
prop-body header {
  background: grey;
  padding: 20px;
}
prop-body prop-wrapper {
  display: flex;
  flex-direction: column;
  background: lightblue;
  flex: 1;
}
prop-body prop-wrapper main {
  overflow-y: auto;
  flex: 1;
}
prop-body prop-wrapper main p {
  padding: 50px;
}
footer {
  display: flex;
  background: pink;
  min-height: 50px;
  height: 10vh;
}
footer .footer-content {
  flex: 1;
  background: plum;
}
<prop-body>
  <header>Header</header>
  <prop-wrapper>
    <main>
      <p>CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT
        CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT</p>
    </main>
    <footer>
      <div class="footer-content">
        Footer
      </div>
    </footer>
  </prop-wrapper>
</prop-body>

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 could be the reason for Chrome breaking up this straightforward bootstrap header into two lines?

Why is it that the code below displays correctly in one line in both FF and IE, but Chrome for some reason is showing it on two lines as if both span and button elements had "display:block;"? Even though the button has a computed display of "inline-block," ...

Maintaining the state of perspectives

I am currently working on an app that utilizes a form to filter objects from a database. Each change in the form triggers a request to the server for filtered objects, which are then displayed in another view. However, I have encountered an issue where the ...

Difficulty setting up AngularFire in Ionic application

Seeking assistance for the installation of AngularFire in a Ionic 6 project. Encountering an error message: Packages installation failed, see above. Below is the setup details: >> ionic info Ionic: Ionic CLI : 7.0.1 (C:&bsol ...

Is there a way for me to choose the item from the search dropdown so that it fills in the search input field automatically?

Upon typing a word into the search input field, a list of suggestions will appear below. https://i.sstatic.net/etOBI.png Is there a way for me to select a word from the list like Maine (ME) and have it automatically fill the search input field? What chan ...

Tips for optimizing loading speed by implementing pagination with HTML, jQuery, PHP, and MySQL on the go

I currently have a table with over 1400 rows, adding approximately 20 new rows every day, which is causing my page to slow down. My current approach involves using a while loop to generate table rows and columns, along with some JavaScript like the exampl ...

Compiling the project using ng build without requiring the download of node

Hey there! Is there a method to execute the ng cli command (ng build --prod) without having to download the npm packages repeatedly? The production build process is becoming very sluggish due to this issue. I am curious to know if there is a way to avoid ...

Cut off text in a dropdown button using Bootstrap 4

I'm encountering an issue with a dropdown button where the text is a combination of a fixed part and a variable part from a database. The variable text can be long, so I need to truncate it. In order to apply bootstrap's .text-truncate class (or ...

What is the best URL structure to use?

I am seeking feedback on the best approach for structuring a website. My website allows companies to register and have their own page at company.site.com. I am trying to decide where the administration should take place for each company. Here are the two o ...

Having difficulty incorporating a video into the background

After searching online and attempting two different methods, I am still unable to achieve the desired result. I want a video to cover the entire background of my webpage. Here is what I have tried: CSS - video#bgvid { position: fixed; top: 50%; ...

After applying 'all: unset;' to remove all styles, the CSS hyphens property does not work as expected in Chrome

After applying this CSS code to remove all styles: * { all: unset; display: revert; } The issue arises in Chrome browser where CSS Hyphens are not taking effect. This can be seen with the following example: div { font-size: 3rem; -webkit-hy ...

Tips for managing the outcome of an HTTP Post request

I am currently working with Angular 2 and Node.js, attempting to incorporate braintree into my project, which has proven to be quite challenging. My current approach involves utilizing an HTTP Post request to send back the result object from the transacti ...

When a media query is activated, the Flex item mysteriously vanishes from

UPDATE I followed the suggestion in Ezra Siton's answer and changed the media query display to block, which successfully resolved the issue! Within my html code, I have a parent column flexbox containing two children. The second child is itself anoth ...

Having issues with Angular 2/4 unable to read an object within the initForm() function

In the process of creating an edit form using Angular, I am facing a problem with understanding the lifecycle of the object retrieved from my backend server. After making a call to my service in ngOnInit(), I receive valid data. However, when I assign this ...

I'm experiencing issues with event.preventDefault() not functioning properly when used within a contenteditable div

I'm currently working with some basic Angular 7.x code that involves a contenteditable div. I'm attempting to prevent the default action when a user hits the [ENTER] key, but no matter what I do, it still moves the cursor to the next line. What a ...

Click to make the arrow come to life through animation!

I am brand new to coding and this is my inaugural code snippet: .container_menu { position: relative; top: 0px; left: 0px; width: 25px; height: 25px; } .container_menu .line-1 { background-color: black; width: 59%; height: 2px; positio ...

Ways to reach the standard look

Check out this follow-up to a previously posted issue. Click here to view it I am currently facing an issue with the code below, where I am unable to make the second button inherit the default border and padding styles. Any assistance would be greatly app ...

Refresh the CSS without having to reload the entire page

I am working on a web page that operates on a 60-second timer. The code snippet below is responsible for updating the content: protected void RefreshButton_Click(object sender, EventArgs e) { ReportRepeater.DataBind(); ReportUpdatePane ...

Remove a div using JavaScript

My approach involves dynamically adding div elements using JavaScript in the following manner: <div id="detail[0]"> <input name="detail.Index" type="hidden" value="0" /> <input name="detail[0].details_name" type="text" /> ...

Utilize PHP to generate a table from data

I successfully created a table using information retrieved from my database, and everything is displaying as intended. However, I am facing an issue where adding a background color applies it to every row in the table. How can I add a background color wit ...

A guide to building a versatile component using Ionic 3 and Angular 4

I decided to implement a reusable header for my app. Here's how I went about it: First, I created the component (app-header): app-header.ts: import { Component } from '@angular/core'; @Component({ selector: 'app-header', te ...