Create a div element set to occupy 100% of the height and styled with the

Currently, I'm struggling with creating a container div that consists of a header, content area, and footer. The goal is to have the footer always stuck to the bottom while the content area fills the remaining space. Unfortunately, I haven't been able to achieve this yet.

This is my current setup:

.container {
  display: grid;
  /*position: relative;*/
  grid-template-columns: 1fr;
  border: 1px solid black;
}

.header {
  background-color: yellow;
  display: grid;
  grid-template-columns: 3fr 3fr 3fr 1fr;
  padding: 10px 0;
}

.content {
  background-color: teal;
  position: relative;
  display: grid;
  grid-template-columns: 1fr 7fr 1fr;
  padding: 15px 20px 20px 0;
  min-height: 100%;
}

.footer {
  background-color: maroon;
  position: absolute;
  width: 100%;
  bottom: 0;
}
<div class="container">
  <div class="header">This is a header.</div>
  <div class="content">This is a content area.</div>
  <div class="footer">This is a footer.</div>
</div>

It's evident that the content section doesn't stretch all the way to the footer section. What could be missing in my implementation?

Appreciate any help you can provide!

Answer №1

Implement

grid-template-rows: auto 1fr auto;
to allow footer and header to occupy only the necessary space, while content can expand into the remaining space. Also, remember to eliminate position: absolute; from footer to integrate it as a grid-item.

body {
  margin: 0;
}
* {
  box-sizing: border-box;
}
.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr auto;
  border: 1px solid black;
  min-height: 100vh;
}

.header {
  background-color: yellow;
  padding: 10px 0;
}

.content {
  background-color: teal;
  padding: 15px 20px 20px 0;
  min-height: 100%;
}

.footer {
  background-color: maroon;
  width: 100%;
}
<div class="container">
  <div class="header">This is a header.</div>
  <div class="content">This is a content area.</div>
  <div class="footer">This is a footer.</div>
</div>

Answer №2

<script>        
$(document).ready(function(e) {
    var windowHeight = $( window ).height();
    $('.container').css('max-height', windowHeight - 220 + 'px');
});         
</script>

This code adjusts the height of the container based on a footer height of 220px. Feel free to modify the height according to your requirements.

<style>
    .footer{ bottom:0; position:fixed; }
</style>

Use this CSS code snippet to keep the footer fixed at the bottom of the page.

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

Personalized Navigation Bar Toggle (transitioning from dropdown to sliding from the left)

On larger screens, I would like the navigation/navbar to be positioned at the top. However, on mobile view or when collapsed, I want the toggle button to reveal the navigation menu by sliding from the left side, similar to this example: (source by David B ...

Unlocking the potential of Bootstrap 4 pop ups and modals within an Angular 6 project

Angular.json "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css", "src/styles.css" ], ...

Leveraging bootstrap for achieving vertical and horizontal centering of images

My webpage uses bootstrap to display content, and while I've managed to center my images vertically, I'm facing an issue with horizontal centering. I want to ensure that any image, regardless of size, remains within the column/row and adjusts it ...

CSS: Overlapping two divs with unique classes and varying margins - Unexpected occurrence or intentional design?

One thing I'm curious about: Will the margins of two different divs merge if they have the same value when intersecting? It appears so! Do paddings not merge when two different divs intersect, even if they have the same value? It seems that way! When ...

Is it feasible to verify for vacant dates with a single click?

Is there a way to determine if a date value is empty, and if it is, display a popup indicating so? After some research, I stumbled upon a similar issue where the date value was always filled with a default "mm/dd/yyyy" value. The solution provided involv ...

What is the best way to change the height of a div element as the user scrolls using JavaScript exclusively?

Coding with JavaScript var changeHeight = () => { let flag = 0; while(flag != 1) { size += 1; return size; } if(size == window.innerHeight/2){ flag == 1; } } var size = 5; document.body.style.height = &qu ...

Verify if the element has been clicked and chosen before proceeding to the next action

It has come to my attention that users are able to submit answers without actually making any selections from a menu of buttons. This results in an empty array being printed to the console, which is not the intended behavior. I am seeking a solution that ...

Is it possible to use jQuery animation to smoothly transition the background color?

I'm currently working on a function to dynamically change the background color of a div based on an array of colors. Here's what I have so far: HTML: <div class="bg-color"> CSS: .bg-color { width: 500px; height: 500p ...

Adaptive Positioned Image Display

I currently have a Section with four blocks of content. Along the right-hand side of the screen, there are some images (as shown in this example) that I want to position absolutely. However, I am encountering an issue where these images overlap with the te ...

Check if a specific condition is satisfied in Django models.TextChoices and then extract the relevant data

Current Program Versions: Django 3.1.13 Python 3.8.10 My models.py: # Defining models here. class Odorant(models.Model): Pubchem_ID = models.IntegerField(primary_key = True) Name = models.CharField(max_length =50) Ruletype = models.TextCh ...

Unable to Upload Image to MySQL Database

I encountered an unidentified index error when trying to upload images. I added a check to prevent the error, but the images are still not being uploaded. I have tested with legitimate images, but nothing seems to work. I've been struggling with this ...

Utilizing AJAX to Fetch Data from Python

I am attempting to utilize AJAX to dynamically update a table with data retrieved from a Python script. However, when I make an AJAX request to the Python script, instead of receiving just the content within the print commands, I get the entire Python scri ...

Guidelines for utilizing the php mail() function

It seems that there is an issue with this code when running it on a local host versus a live server. I am attempting to use this code for a mail function, which works on another site but not on this one. I know the code will produce an error on localhost ...

Using Flask to trigger a Python function that requires input parameters when a button is clicked

I am working on a Flask web application that includes a download button. Instead of triggering a JavaScript function, I want the button to execute a Python method. This Python method should take the recently modified HTML code from the webpage as input. H ...

jQuery and Internet Explorer 8 have an issue where a toggler will only open the first child

I am facing an issue with a group of divs on my page. The code for the div is as follows: <div class="demo1"> <h3 class="expand2">1:00 pm – 4:00pm<br>Item 1<p></p> <p><strong>Presented by:</strong>< ...

What is the best way to move one container below another in mobile view?

I need container-2 to be fully visible on mobile view, but the boxes in box2 are covering it. The size of the boxes in box2 should not change; I just want container 2 to be pushed down so that it is completely visible. body { margin: 0; width: 100%; ...

Unusual actions exhibited by a combination of JavaScript functions

Encountering an issue with a JavaScript file containing multiple functions causing strange behavior. The Logging.js file is responsible for writing to a text file: function WriteLog(message) { var fso = new ActiveXObject("Scripting.FileSystemObject") ...

Struggling to comprehend the method to extract information from a MySql Query

Recently, I posted a question seeking assistance on a complex query and was fortunate to receive helpful feedback. However, due to the complexity of the query, I am struggling to retrieve and display the results on my website. If you'd like to view th ...

The CSS styling for the rows in an Angular Material textarea is unresponsive

I have been working with angular Material design and have implemented a Textarea element. However, I am facing an issue where the height of the Textarea is fixed and cannot be changed. I have tried various solutions without success. How can I adjust the si ...

When the page is launched, creating a rectangle at a precise location on the area map

I have successfully implemented the maphilight jquery plugin with hover and onclick features from here. It works great. Is there a way to automatically draw a rectangle on the areamap image at a defined position and size when the page loads, without requi ...