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

Is there a way to verify if an ID includes more than one word?

I am trying to target a specific div with a unique id in jQuery: <div id="picture_contents_12356_title"></div> The '12356' is autogenerated and must be included in the id. I need to create a jQuery selector that combines "picture_co ...

Create a speech bubble using only CSS, featuring a partially see-through background and a stylish border

I wish to design a speech bubble using only CSS. There are specific requirements I need to meet: It should adjust its size based on content with some padding or a set size. The background color must be semi-transparent. You must be able to define a borde ...

Sorry, the provided text is already unique as it is an error message

I'm currently using the react-highlight-words package to highlight text inputted into a textbox After checking out the react-highlight-words documentation, I noticed that they are using searchWords as an array. https://www.npmjs.com/package/react-high ...

Building a dynamic tab menu using JavaScript: A step-by-step guide

In order to generate dynamic tab menus with JavaScript, I am seeking a solution that does not rely on jQuery as it may not be supported by all mobile devices. Any advice for replicating the same functionality using pure JavaScript would be greatly apprec ...

What is the best way to implement dynamic generation of Form::date() in Laravel 8?

Is there a way to dynamically generate the Form::date() based on the selection of 1? If 1 is selected, then display the Form::date() under the Form::select(). However, if 0 is selected, then hide the Form::date() in this particular view. For example: Sel ...

What could be causing AngularJS to truncate my URL in the search bar?

Currently, I am in the process of setting up a state provider for a CRUD website. One issue I encountered is that when I navigate to www.mysite.com/posts/mypost, the URL gets shortened to www.mysite.com/mypost and does not trigger the controller as intend ...

Easy jQuery image that smoothly fades in and out

Can anyone recommend a script that allows for creating a slideshow where images fade in and out, rather than sliding left and right? I have a list of images below that I would like to use for the slideshow. <ul class="slideshow"> <li><im ...

In Angular, encountering difficulty accessing object members within an array when using custom pipes

Here is a custom pipe that I have created, but I am facing an issue accessing the members of the customfilter array, which is of type Item. import { Pipe, PipeTransform } from '@angular/core'; import {Bus} from '/home/pavan/Desktop/Pavan ...

Encountering challenges with web scraping certain data that is not being captured after a line break tag using Python's

I've been attempting to scrape a public web directory in order to retrieve contact information for companies. While my code is functioning properly, it seems to be having trouble extracting information that comes after the br tag. For example, it fai ...

Having trouble with accessing the upvote/downvote input within a Django template

Currently, I'm trying to retrieve upvote/downvote data from a Django template in the following manner: <form method="POST" action="{% url 'vote' %}" class="vote_form"> {% csrf_token %} <input type="hidden" id="id_value" name="valu ...

Building a Dynamic Checkbox Validation Feature in Angular Using Data retrieved from an API

Currently, I have a function that retrieves and displays a list obtained from an API: displayEventTicketDetails() { this.Service .getEventTicketDetails().subscribe((data: any) => { this.eventTicketDetails = data.map(ticket => ticket. ...

What is the best way to highlight and extract only the white-coded texts on VSCode to facilitate the process of translating webpages manually?

Currently, I'm engaged in a project where my task involves duplicating an entire website using HTTrack. However, I only need to copy the main page and web pages that are one link deep. Upon copying the website, my next challenge is to translate all of ...

What is the best way to implement a scrollbar in a specific div rather than the entire window?

Hey everyone! So, I have this window in electronJS with a div where I'm dynamically adding elements using Javascript and the function --> document.createElement('section'). Here's the loop in Javascript to add these elements: for (var ...

Is there a method to use media queries to dynamically switch JavaScript files based on the user's device?

I've been working on optimizing the mobile layout of my website, and while I have successfully implemented a media query with a different stylesheet for mobile devices, I'm struggling to determine if it's feasible to also load a separate Jav ...

Inaccurate rendering of border widths on IOS platform

After setting the border width of these input boxes to 1px, it appears that on iOS devices, the top border seems larger than intended (regardless of the browser being used). Interestingly, on any other type of device, the border looks perfectly fine. & ...

perform an action in PHP when a button is clicked

I'm currently developing a PHP admin panel that displays a list of users in an HTML table format. Each row in the table includes a button that allows the admin to send a notification to the selected user. Below is the code I used to create and displa ...

What methods can be used to authenticate the user's input?

I am facing an issue with my program where it breaks if there is a space behind the last number entered. I want to prevent the function from breaking when a space is entered. I tried using $.trim but couldn't get it to work. I also attempted using an ...

A comprehensive guide to effectively formatting Recharts in React: addressing alignment and size management!

While attempting to style two graphs as floating cards, I am encountering difficulties in controlling the sizing and centering of the graphs. Specifically, I am having trouble with the pie chart in this example. To address this issue, I am passing paramete ...

A guide on mapping an array and removing the associated element

I have an array called responseData, which is used to display the available card options on the screen. public responseData = [ { id: 1399, pessoa_id: 75898, created_at: '2022-11-08T16:59:59.000000Z', holder: 'LEONARDO ', validade: ...

PHP Warning: Attempt to access undefined variable

Whenever I try to insert a new record into my HTML, I keep encountering errors. I'm unsure if it's due to incorrect code on my end or if the issue lies with inserting the records. <?php $servername = "localhost"; $dbusername = "r ...