Making sure that a footer div is consistently positioned at the bottom of the container div, regardless of the dimensions of the other elements

I am facing a challenge with a container div which has child elements.

    <div class='wrapper'>
       <div class='content'></div>
       <div class='footer'></div>
    </div>

The height of the content div may vary, but I want the footer div to always remain at the bottom of the wrapper.

When I attempt to set the wrapper to have:

    position: relative

and set the footer child to:

    display: absolute;
    bottom: 0;

This only works if I specify a specific height for the wrapper. Furthermore, when resizing the window, the footer disappears.

Is there a way to achieve this layout using flexbox without relying on positioning? In other words, is it possible to have a fixed footer and dynamic content without using position properties?

Answer №1

If you're looking to place your footer at the bottom of the page, I recommend utilizing the footer element with a positioning of either absolute or fixed based on your preference. By setting bottom: 0;, you can ensure that the footer stays at the very bottom. In my example, I used position: fixed; to keep the footer within the viewport at all times. To illustrate this, I included 200vh for the wrapper to demonstrate how the footer remains fixed at the bottom as you scroll.

footer {
  position: fixed;
  bottom: 0;
  background-color: grey;
  width: 100vw;
  text-align: center;
  color: yellow;
}

body {
  margin: 0;
}

.wrapper {
  background-color: lightgreen;
  height: 200vh;
}
<div class='wrapper'>
   <div class='content'></div>
</div>
<footer>FOOTER</footer>

Answer №2

Here is how to implement position :fixed;:

.content {
  font-size: 4rem;
}

.footer {
  position: fixed;
  bottom: 0;
  background: red;
  text-align: center;
  width: 100%;
  font-size: 2rem;
}
<div class='wrapper'>
  <div class='content'>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
  <div class='footer'>footer</div>
</div>

Answer №3

On all of my websites, I implement this method to ensure the footer stays at the bottom of the page. By avoiding the use of position:fixed for the #footer, the content itself will naturally push the footer to the bottom. Even if there's minimal content, the footer will still remain at the bottom due to the min-height:100vh. Remember to subtract the height of the #footer (120px) from the main content's 100vh.

body {
  height: 100%;
  width: 100%;
  min-width: 320px;
  color:#FFF;
  font-size:18px;  
}

#page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

#header {
  position: relative;
  left: 0;
  right: 0;
  top: 0;
  z-index: 100;
  height: 40px;
  background-color: orange;
}

#content {
  flex: auto;
  min-height: calc(100vh - 120px);
  background-color: yellow;
}

#footer {
  height: 120px;
  background-color: blue;
}
<body>
  <div id="page">
    <div id="header">this is header</div>
    <div id="content">this is content </div>
    <div id="footer">this is footer</div>
  </div>
</body>

To adjust, you can switch position:relative to position:fixed for the #header as per your preference.

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

Replicate the form to a new one while concealing the elements and then submit it

Initially, I was working with just one form. Now, I find myself in a situation where I need to utilize a different form which contains the same inputs. This is necessary because depending on the action taken upon submission, different processes will be tri ...

Verify the functionality of the input fields by examining all 6 of them

I'm facing a challenge with a validation function in my project that involves 6 input fields each with different answers. The inputs are labeled as input1 to input6 and I need to verify the answers which are: 2, 3, 2, 2, 4, and 4 respectively. For e ...

Is there a way to develop a login form that retrieves information from a Google Sheet database?

Please help me with a solution. I have developed a registration form which effectively saves users' information in a Google Sheet. However, now I am yearning to create a login form that verifies the stored data and redirects the user to a different pa ...

What is the method for defining CSS styles for child elements using the parent element's style attribute?

Here is an example: <div style="SET IMAGE ATTRIBUTES HERE!"> <img src="http://somesite.com/someimg.jpg"><br /> <img src="http://someothersite.com/someotherimg.jpg"><br /> ... </div> With my dynamic ASP.NET ...

Having trouble dynamically displaying the '<' symbol using JavaScript?

Whenever I attempt to show a string with the character '<' in it, the part of the string that comes after the symbol is not displayed. Strangely enough, when I output it to the console, it appears correctly. Take a look at this excerpt showcas ...

The footer and login form are tangled up in a web of confusion

Here is my HTML code for a login form: <div class="middle-box text-center loginscreen"> <div> <form class="" role="form" action="index.html"> <div class="form-group&q ...

Why are my elements not appearing where I want them to in absolute positioning?

Could use some help with a seemingly simple question, I just need some clarity on this issue. Background: I am working on developing a complex website and one of my ideas involves generating a series of boxes on the screen. For various reasons that are t ...

Regular expression for selecting characters and numbers in jQuery using a selector

Can someone help me figure out how to select all IDs like this: r1 r2 r3 etc. and not the other IDs like helloWorld I attempted using the CSS selector with jQuery but I'm having trouble understanding how. I tried $('#r[0-9]') but it didn& ...

Modify MUI's ListItemText component by targeting a specific span to implement customized styles using the useStyle hook

It's quite perplexing that although this is a straightforward task in regular CSS, it must be accomplished through MUI's useStyles for some peculiar reason. Essentially, I possess a ListItem containing a ListItemText. It appears as follows: cons ...

Determining the dimensions of form elements in an inline layout using Bootstrap 3.1

Currently in the process of upgrading my application from version 3.0 to 3.1. The application is still in its initial phase, resembling more of a prototype with only a few pages created. Therefore, I didn't anticipate encountering many issues during t ...

Issue with horizontal scrolling in ng-scrollbars occurs when scrolling from right to left

We are currently developing a single page application that supports two languages, one being right to left and the other left to right. For scrolling functionality, we have implemented ng-scrollbars, an Angularjs wrapper for the malihu-custom-scrollbar-pl ...

Unable to retrieve the iframe variable from the parent as it returns undefined

I am trying to retrieve a variable within an iframe from the parent index.html. Here is the index.html code: <!doctype html> <html lang="en-us> <head> <title>Test</title> </head> <body> <p>Test& ...

New alternative for form-control-feedback in Bootstrap 4

The form-control-feedback class doesn't appear to be included in Bootstrap 4. What is the most effective way to achieve the desired outcome (an icon inside the textbox) using Bootstrap 4? <head> <link rel="stylesheet" href="https://max ...

Repeating percentages displayed in a progress bar

I created a responsive progress bar, but the progress values are repeating. I only want the central value to be displayed. Any suggestions on how to fix this issue? DEMO <div id="tab1"> <dx-data-grid class="tableTask" [dataSource]="datas" ...

Troubles with NextJS and TailwindCSS Styling

I encountered a strange issue when I used the component separately. Here's how the code looked like: <> <Head> <title>Staycation | Home</title> <meta name="viewport" content="initial- ...

Clear out any unnecessary white space beneath the content on your webpage

I am relatively new to Web Development and currently following Colt Steele's udemy course. I have been working on a landing page, but as I try to set the height of HTML to 100%, it leaves whitespace at the bottom. After some research, I suspect that t ...

Can a sophisticated matrix-like design be created using a CSS grid system?

I'm currently working on implementing a unique grid structure in Material-UI using a CSS grid: Browse the desired complex layout here Here's the current CSS grid layout I've created with Material-UI: View the existing layout here This is ...

What could be causing this Bootstrap column to misbehave?

Why is the content stacking within each list item instead of being on the same line? I've attempted using floating and adjusting the position, but nothing seems to be working. How can I resolve this issue? .icon-left{ float:left; padding: 1 ...

"Create dynamic web pages with multilingual support using HTML, JQuery, and nested

For my multilingual website, I wrote this simple JQuery code: (function() { var language, translate; translate = function(jsdata) { $('[tkey]').each(function(index) { var strTr; strTr = jsdata[$(this).attr('tkey')] ...

The CSS overflow property is a great tool to demonstrate how a box can be neatly cut off at its

According to the specifications outlined in this resource, In situations where overflow occurs, the 'overflow' property determines if a box is clipped to its padding edge. Additionally, it specifies whether or not a scrolling mechanism will b ...