Using CSS Grid to position components in specific regions

Take a look at my HTML code:

<div>
  <div id="navbar" class="box">Navbar</div>
  <div id="sidenav " class="box">Side Navbar</div>
  <div id="main " class="box">Main</div>
  <div id="footer " class="box">Footer</div>
</div>

Now check out my SCSS code:


div{
  display: grid;
  width:100%;
  height: 100%;
  grid-template-columns: 25% 75% 25%;
  grid-gap: 15px;
  grid-template-rows:25% 50% 25% ;

  grid-template-areas:
  "hd hd hd "
  "sd ma ma "
  "ft ft ft ";

  .box{
    display: flex;
    border: 3px solid red;
    margin: auto;
    align-items: center;
    justify-content: center;
  }
  #navbar{
    grid-area: hd;
  }
  #sidenav{
    grid-area: sd;
  }
  #main{
    grid-area: ma;
  }
  #footer{
    grid-area: ft;
  }
}

I'm facing an issue where the footer div is not displaying at the bottom. You can see the problem in this screenshot: https://i.sstatic.net/lIvaK.png

How can I make sure that the footer displays at the bottom? Any ideas on what might be causing this issue?

Answer №1

I noticed a few issues in your code that are causing the layout to appear a bit off.

grid-template-columns: 25% 75% 25%;

The total column width exceeds 100%, which can lead to rendering problems.

For a better solution, I suggest using an online CSS grid generator like

In terms of structuring your layout, I recommend the following:

  1. 1 row for "Navbar" (no sub columns needed)
  2. 1 row for Content with 2 columns: 1 for "SideNav" and 1 for "Main"
  3. 1 row for "Footer" (no sub columns needed)

Based on this recommendation, your HTML structure should resemble:

<div class="container">
  <div class="navbar">Navbar</div>
  <div class="Content">
    <div class="SideNav">Side Nav</div>
    <div class="Main">Main</div>
  </div>
  <div class="Footer">Footer</div>
</div>

Your CSS code will then look similar to this:

body{
  padding: 0;
  margin: 0;
}
.container {
  width: 100vw;
  height: 100vh;
  display: grid; 
  grid-template-columns: 1fr; 
  grid-template-rows: 25% 50% 25%; 
  gap: 0px 0px; 
  grid-template-areas: 
    "navbar"
    "Content"
    "Footer"; 
}
.navbar { 
  grid-area: navbar; 
  background-color: #f5f5f5;
  padding: 16px;
  text-align: center;
}
.Content {
  display: grid; 
  grid-template-columns: 360px 1fr; 
  grid-template-rows: 1fr; 
  gap: 0px 0px; 
  grid-template-areas: 
    "SideNav Main"; 
  grid-area: Content; 
}
.SideNav { 
  grid-area: SideNav; 
  background-color: #e5e5e5;
  padding: 16px;
}
.Main { 
  grid-area: Main;
  background-color: salmon;
  padding: 16px;
}
.Footer { 
  grid-area: Footer;
  background-color: #d5d5d5;
  padding: 16px;
  text-align: center;
}

This layout features 3 rows within the container (25% - navbar, 50% - content, 25% - footer) and content divided into 2 columns (360px - Sidenav, 1fr - Main).

I hope this explanation clarifies any confusion :)

You can also view the live code on my CodePen profile here: https://codepen.io/raunaqpatel/pen/WNyQqmm

Alternatively, see the code snippet below:

body{
  padding: 0;
  margin: 0;
}
.container {
  width: 100vw;
  height: 100vh;
  display: grid; 
  grid-template-columns: 1fr; 
  grid-template-rows: 25% 50% 25%; 
  gap: 0px 0px; 
  grid-template-areas: 
    "navbar"
    "Content"
    "Footer"; 
}
.navbar { 
  grid-area: navbar; 
  background-color: #f5f5f5;
  padding: 16px;
  text-align: center;
}
.Content {
  display: grid; 
  grid-template-columns: 360px 1fr; 
  grid-template-rows: 1fr; 
  gap: 0px 0px; 
  grid-template-areas: 
    "SideNav Main"; 
  grid-area: Content; 
}
...
<div class="container">
  <div class="navbar">Navbar</div>
  <div class="Content">
    <div class="SideNav">Side Nav</div>
    <div class="Main">Main</div>
  </div>
  <div class="Footer">Footer</div>
</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

When hovering over a hyperlink, an image appears but I want to adjust the image's position in relation to each link

I have a unique feature on my website where text hyperlinks reveal small thumbnail images when hovered over. This concept was inspired by an example I found on this page. I initially implemented the code from a post on Stack Overflow titled Display Image O ...

Accessing office documents such as XLS and PPT files directly within the browser window using Internet Explorer 7.0

Currently using XP, IE 7.0, Office 2010, and operating within a company INTRAnet environment. I have created a HomePage in Word and saved it as HTML, including links to other Office files. Despite my attempts, I am unable to get these office files to ope ...

How to align several DIVs within a container with an unspecified size

I have several blue rectangles with known dimensions (180 x 180 px) within a container of unknown size. <html> <head> <style> @import url('http://getbootstrap.com/dist/css/bootstrap.css&a ...

Modifying the appearance of the initial column in a panelGrid

Is there a way to left align the first column and right align the second column of a <h:panelGrid>? I've attempted to assign a class to elements in the first column for left alignment, but as they are outputText elements nested in a span inside ...

Is there an issue with the functionality of the number input type in HTML 5?

<form name="form" method="get" action="" > Number : <input type="number" name="num" id="num" min="1" max="5" required="required"/> <br/> Email : <input type="email" name="email" id="email" required="required"/> <br /> <in ...

React Div Element Not Extending to Web Page Margin

creating white space between the green div and both the top and sides of the screen This is my index page export default function Home() { return( <div className = {stylesMain.bodyDiv}> <div>home</div> &l ...

How to Copy and Paste Code from Chrome after Using Selenium and Pressing F12?

When I opened Chrome and pressed F12, I was able to view my code. However, I am unsure of how to copy this code (see screenshot). I tried clicking on the ellipses on the left, selecting Copy, then choosing Copy Element. This method successfully copies ...

Hide the navigation bar when the page first loads

Upon loading my page, the navbar button at the top is automatically expanded. Here is the code snippet: <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-main" ...

Basic JavaScript Calculator Utilizing JQuery

I have been diligently working on a Javascript Calculator for quite some time now. The CSS and HTML elements seem to be in order. However, the calculator is only functioning properly for sevens and division operations! While the input for other numbers g ...

Creating a dynamic slideshow using Jquery Ajax requests

I am looking for help with creating a slideshow using Ajax calls. I have successfully implemented it by adjusting the margin left, but now I need to approach it differently. I have a PHP array that I fetched from a database and I want to use it to display ...

Tips for aligning images within Bootstrap columns and using auto zoom to properly fill the space

Here is the code snippet for displaying images: <div class="row"> <div class="col-12"> <div class="row"> <div class="col-lg-6 col-md-6 col-sm-12"> <div class=&qu ...

Challenges with displaying content in Bootstrap tabs

I am currently working on the following code: HTML <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" role="tablist" id="tabs_list"> <li class="nav-item"> <a class="nav-li ...

React Image Slider not loading pictures

Can someone help me troubleshoot why my images are not displaying in the slider on my homepage? I have set up the slider using React Slideshow Image, but the images are not showing up for some reason. I am also wondering if anyone knows of any full-screen ...

After pressing submit, any associated links should automatically open in a separate tab

I have a homework assignment that requires creating a form with multiple elements, including a checkbox. Once this checkbox is checked, the links displayed on the resulting page should open in a new tab. The catch? I can only use HTML and CSS - no JavaScri ...

Adjustable height for Material UI tab components

I encountered an issue that I initially thought was related to the general configuration of my app and the height I assigned to my page wrapping classes. However, after creating a simple out-of-the-box material UI tab example, it became clear that this beh ...

Adjusting the font size in Bootstrap grid rows to include an offset

I am currently working on improving site navigation using the Bootstrap Grid system. My goal is to create a layout with 3 columns and two rows structured as follows: |Title | |Login| |Subtitle|Menu buttons| | Everything seems to be functi ...

Discussing a non-existent web address

Is it possible to generate a URL that appears to exist but actually doesn't? Take, for example, https://twitter.com/i/notifications - this link functions as expected. However, if you delete the /notifications segment from the URL, you end up with http ...

Utilize AJAX or another advanced technology to refine a pre-existing list

I am trying to implement a searchable list of buttons on my website, but I haven't been able to find a solution that fits exactly what I have in mind. Currently, I have a list of buttons coded as inputs and I want to add a search field that will filte ...

Bootstrap loader failed to load, causing issues with CSS in Django

Just getting started with Django and Python, I created a test project that was working fine until I attempted to change the design by adding some CSS, JS, and image files. I also installed Bootstrap, but when I uploaded the files, my page couldn't fin ...

What are the steps to resolve issues with my dropdown menu in IE9?

I have a cool CSS built hover-over drop-down menu that I want to add to my website. It works perfectly on all browsers except for IE9. Here is the code and stylesheet I am using: Check out the code and sheet here If anyone has any insights on what might ...