Position the footer at the bottom of the content and shift it to the right of the sidebar

I was aiming to position the footer at the bottom of the content, without extending it across the entire width of the page. The footer should always be aligned with the bottom if the content is not filling up the space, as illustrated in figure 1. The sidebar can be toggled using the button located in the navbar.

https://i.sstatic.net/eHudI.png

I managed to achieve the following layout:

https://i.sstatic.net/JeOcc.png

The sidebar should push the footer to the right, but it seems to always stay at the top. Ideally, when the sidebar is hidden or collapsed, the footer should expand to occupy the same width as the content.

The HTML structure for this implementation is as follows:

<div class="wrapper">
   <!-- Navbar -->
   <nav class="navbar navbar-expand-sm fixed-top navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
      <div class="container-fluid">
      </div>
   </nav>
   <!-- Sidebar -->
   <div id="sidebar" class="shadow-sm"></div>
   <!-- Page Content -->
   <div id="content">
      <div class="container-fluid">
         <div class="card">
            <div class="card-body">
               <h1>
                  Collapsing Menu
                  <small class="text-muted">Version 2.1</small>
               </h1>
            </div>
         </div>
      </div>
   </div>
</div>
<footer id="page-footer" class="py-3 bg-dark text-light">
   <div class="container">

   </div>
</footer>

The CSS styles for the footer and sidebar are as below:

footer {
    bottom: 0;
    left: 0;
    position: fixed;
    width: 100%;
}
#sidebar {
    margin-top: 54px;
    width: 250px;
    position: fixed;
    top: 0;
    left: 0;
    height: calc(100vh - 55px);
    z-index: 999;
    padding: 10px;
    -webkit-transition: right .5s ease,left .5s ease;
    -moz-transition: right .5s ease,left .5s ease;
    -o-transition: right .5s ease,left .5s ease;
    transition: right .5s ease,left .5s ease;
}

Answer №1

To optimize the layout, my suggestion would be to place the footer within the div labeled "content" or the one with the class "container-fluid."

Give it a try and see how it impacts the design.

In more detail: Currently, your footer is positioned based on its parent container. However, in the current setup, the context of where you're placing the footer doesn't align with what you've shared. This means that the footer isn't properly linked to the content container for rendering purposes. The best fix would be to nest the footer inside the same container as the main content section. By doing this, the footer will be restricted within the boundaries of the content div. How it appears at the bottom of the page can then be fine-tuned using your CSS styles.

I hope this explanation clarifies things for you.

Answer №2

$(document).ready(function(){
  var wheight= $(window).height();
  var contentheight=$(".content").height();
  if(wheight>contentheight){
    $("footer").addClass("fixedfooter");
  }
  else{
     $("footer").removeClass("fixedfooter");
}
})
*{
margin:0;
padding:0;
}
.sidebar {
   height: 100vh;
  float:left;
  width:20%;
  background:red;
}
.wrapper{
  overflow:hidden;
}
.page{
  width:80%;
  float:left;
  
}
.content{
  width:100%;
  height:200px;
  background-color:green;
}
footer {
  width:calc(100%);
  height:50px;
  background-color:blue;
}

.fixedfooter{
  position:fixed;
  right:0;
  bottom:0;
  width:calc(100% - 20%)!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="sidebar">sidebar</div>
  <div class="page">
    <div class="content">my content</div>
    <footer>my footer </footer>
  </div>
</div>

this is it with jquery! applying a class with fixed position to the footer when the content height is not higher than windows height! try changing the content height to see the result!

Answer №3

.sidebar {
   height: 100vh;
  width:20%;
  background:red;
}
.wrapper{
  display:flex;

}
.page{
  width:80%;
    display:flex;
  flex-direction:column;
  justify-content: space-between;
  height:;
  
}
.content{
  width:100%;
  height:200px;
  background-color:green;
}
footer {
  width:calc(100%);
  height:50px;
  background-color:blue;
}
<div class="wrapper">
  <div class="sidebar">sidebar</div>
  <div class="page">
    <div class="content">my content</div>
    <footer>my footer </footer>
  </div>
</div>

this is a different approach utilizing only CSS!

Answer №4

I created this solution by referencing the code on w3schools.com

.footer {
   position: fixed;
   left: 0;
   bottom: 0;
   width: 100%;
   background-color: red;
   color: white;
   text-align: center;
}
<!DOCTYPE html>
<html>
<body>
<h2>Fixed/Sticky Footer Example</h2>
<p>The footer is placed at the bottom of the page.</p>

<div class="footer">
  <p>Footer</p>
</div>

</body>
</html> 

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

Problem encountered during the transfer of JSON data from PHP to JavaScript

Currently, I am working on a PHP file that extracts data from a database to display it on a chart using the Chart.js library. The chart is functioning properly, but I am facing an issue where I need to use the json_encode() function to pass the array value ...

The stylish overflow feature in CSS

After reviewing various CSS templates, I noticed that some classes contain the overflow:hidden property without a defined size. As far as I understand, block elements typically expand to accommodate their content unless instructed otherwise. Given this, ...

What is the most effective way to extract data from a .dpbox table using selectorgadget in R (rvest)?

Recently, I've been experimenting with web scraping data from different websites using selectorgadget in R. One successful example was when I extracted information from . My usual approach involves utilizing the selectorgadget Chrome extension to choo ...

Load a page from a different domain using uframe

Looking for a solution to successfully load an external URI using uFrame. Currently encountering an "Access Denied" issue when attempting to do so on Firefox. Any suggestions? ...

Django causing issues with displaying Bootstrap modal on website

I am having issues with implementing a Bootstrap modal in my Django template. I followed an example from the Bootstrap website to create a button that triggers a modal, but when I click the button, nothing happens. There are no errors displayed in the co ...

Disappearing Data in Chart.js When Window is Resized

I am utilizing the Chart.js library to present a line chart in a div that is enclosed within a <tr>. <tr class="item">...</tr> <tr class="item-details"> ... <div class="col-sm-6 col-xs-12 chart-pane"> <div clas ...

encountering difficulty incorporating menu.php within main.php

I'm currently working on integrating the menu.php file into my main.php. Here is what I have accomplished so far: Main.php <html> <body> <?php include("menu.php"); ?> <p>This is an example to demonstrate how ...

Is there a way for me to include the input into the link within the action of a search bar?

As I work on my HTML script to create a search bar, I've run into an issue. Currently, the page redirects to instead of the input in the search box. For example, if the input in the search box is "icecreamshop", I want it to redirect to <form a ...

The stylesheet failed to load due to an incorrect MIME type (text/html instead of text/css)

As I delve into creating a personal project centered around a basic expense tracker web app, I encountered an obstacle right at the start of my journey. The main page's stylesheet (index.html) refuses to load, resulting in the common error message: Th ...

What are the recommended practices for server-side Java when compiling CSS to SWF?

Users of my project have the ability to design custom CSS for our flexible application. When it comes to converting the CSS into SWFs on the server side: Is it recommended to utilize the flex2.compiler.css.Compiler class in mxmlc-3.5.0.12683.jar? Altern ...

Create a division that will remain visible on the screen and allow scrolling when a certain class is

Having some trouble with a fixed class div that is not continuing to scroll after the class is removed on scroll. I've attempted to fix this issue but the div keeps getting hidden instead of continuing to scroll. If anyone has any advice or can poin ...

Has Apache initiated a forced logout process?

Running a LAMP server, I have implemented Apache basic authentication to log in users accessing the server homepage. I am currently seeking a way to enforce user logout, but my attempts with mod_session & mod_auth_form have not been successful. The page ...

Building an accordion collapse feature in HTML using CSS and JavaScript

I've been working on creating an accordion interface, but I'm facing an issue where only the first collapsible button works properly. The rest of the buttons are not functioning and don't even appear on the page. When I remove the CSS styli ...

Bootstrap 4: The `.flex-column` class is functioning correctly, however the `.justify-content-center` class is not working as intended

Currently, I am revisiting some older code and working on arranging the content properly on my webpage. However, I have encountered an issue: while my flex properties are functioning correctly, the '.justify-content-...' is not working as expecte ...

Removing margins from the footer and header after adding a video background: A step-by-step guide

After implementing a video as the background of my HTML project, I noticed some margins appearing on the right side of the header and footer. Despite following advice to set "margin: 0" in my CSS for the Body element, the issue persists. You can view the ...

Having trouble with Vue.js not returning HTML elements from methods properly?

I have been attempting to retrieve html elements from a method, and I thought of using v-html for this purpose (not sure if there is a better approach). However, I seem to have encountered an issue with backtick templates and string interpolation. An error ...

Trouble with Bootstrap CSS buttons: color consistency problem persists

I am facing an issue with my Bootstrap button, which currently looks like this: https://i.sstatic.net/MWHYP.png The HTML code for the button in question is as follows: <button type="button" class="btn-primary.custom-btn" data-bs-to ...

`Overlapping navigation against a backdrop of backstretch image`

Is there a way to make the navigation display in front of the background image? Currently, the navigation becomes un-clickable and is hidden behind the image once it loads. ...

Is Jsoup receiving the HTML incorrectly?

Currently, I am utilizing Jsoup to fetch HTML data from a website and attempting to display it in a ListView within my app. Although the application compiles without any errors, clicking the button results in an empty ListView. In my opinion, this issue ...

Concealing an input field in AngularJS

I am currently developing a login page with register and login options using AngularJS. The interface includes three input fields: username, password, and name. I aim to display the name field upon clicking the register button and hide it upon clicking t ...