Prevent the Bootstrap 4 footer from moving upwards

My bootstrap 4 footer is causing issues on pages with less content.

Take a look at the image of the problem below.

footerproblem

footer {
  background-color: white;
  color: #d5d5d5;
  padding-top: 1rem;
}
footer a {
  color: #d5d5d5;
}
hr.light-100 {
  border-top: 1px solid #d5d5d5;
  width: 100%;
  margin-top: .8rem;
  margin-bottom: 1rem;
  color: black;
}
<footer>
  <div class="container-fluid padding">
    <div class="row text-center">
      <div class="col-md-6">
        <p class="marker">blah blah blah.
        <a class="btn btn-social-icon btn-instagram">
         <span class="fa fa-instagram"></span>
        </a>
      </div>
      <div class="col-md-6">
        <p class="marker">more blah blah.
        <a class="btn btn-social-icon btn-facebook">
         <span class="fa fa-facebook"></span>
        </a>
        </p>
      </div>
      <div class="col-12">
        <hr class="light-100">
        <h5>&copy; app.com</h5>
      </div>
    </div>
  </div>

If there is anything else I have missed, please let me know so I can update the post.

I've experimented with footer: sticky; / fixed; but it hasn't resolved the issue.

The height of the footer is too tall, and adjusting the height CSS variable reduces the size but keeps the text in place.

funkyheight

Thank you for your assistance!

Answer №1

To solve the issue at hand, it is recommended to structure your webpage in the following manner:

<body>
  <header></header>
  <main></main>
  <footer></footer>
</body>

Utilize the following CSS styles:

body {
  display: flex;
  flex-direction: column;
  margin: 0;
  min-height: 100vh;
}
header, footer {
  flex-grow: 0;
}
main {
  flex-grow: 1;
}

This method can be implemented with or without Bootstrap. However, for non-Bootstrap projects, ensure to add

* { box-sizing: border-box; }

(although this is already integrated in Bootstrap).

Here's a demonstration using Bootstrap v4:

body {
  display: flex;
  flex-direction: column;
  margin: 0;
  min-height: 100vh;
}
header, footer {
  flex-grow: 0;
}
main {
  flex-grow: 1;
}
footer {
  background-color: #eee;
}
<!doctype html>
<html lang="en>
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

    <title>Hello, world!</title>
  </head>
  <body>
    <header>
  <nav class="navbar navbar-expand-md navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Logo</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarCollapse">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
      <form class="form-inline mt-2 mt-md-0">
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </div>
  </nav>
</header>
<main role="main">
<div class="container">
    <h1>Hello, world!</h1>
    <div class="row">
      <div class="col">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Aliquam ut porttitor leo a. Metus dictum at tempor commodo. Feugiat vivamus at augue eget arcu dictum. Amet consectetur adipiscing elit duis tristique sollicitudin nibh. In fermentum posuere urna nec tincidunt praesent semper feugiat nibh. Elementum eu facilisis sed odio morbi quis commodo odio. Tempus egestas sed sed risus. Pellentesque elit ullamcorper dignissim cras tincidunt lobortis. Eu ultrices vitae auctor eu augue ut. Viverra suspendisse potenti nullam ac tortor vitae purus faucibus ornare. In pellentesque massa placerat duis. Bibendum enim facilisis gravida neque convallis.
...
... (the text continues)
...
    </div>
    </div>
  </div>
  </main>
  <footer class="footer mt-auto py-3">
  <div class="container">
    <span class="text-muted">I'm your footer. Always at the bottom of the screen. When the page content is too tall, I get pushed down. I also adjust my height according to my contents.</span>
  </div>
</footer>

    <!-- Optional JavaScript -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
  </body>
</html>

Answer №2

CSS grid is the way to go.

body {
    margin: 0;
    min-height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr auto;
    grid-auto-rows: minmax(auto, auto);
}
<body>
  <header>Header</header>
  <main>main</main>
  <footer>footer</footer>
</body>

With

grid-template-rows: auto 1fr auto;
, auto represents the header and footer, while 1fr corresponds to your main content.

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

Can object methods be called using inline event attributes in an HTML tag?

Exploring the depths of core JavaScript has been my latest obsession. It's fascinating how we can easily trigger functions within the global context using events like this. HTML <span class="name" onclick="clicked()">Name</span> JavaSc ...

React is giving me trouble as I try to map my tables. I am struggling to figure out how to do this

I have two .CSV files: "1.csv" and "2.csv". I need to display the data from each file in a table using the BootstrapTable component. My async functions, GetData(file) and FetchCSV(file), are working fine and providing the necessary array of objects for the ...

Show a DIV spanning two frames

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test</title> </head> <frameset rows= ...

Does CSS have a selector that does not include any elements?

Considering the examples below <span id="foo_repeater_bar_0"></span> <span id="foo_repeater_batz_1"></span> I am looking for a CSS selector that will allow me to target only the elements whose id does not include foo_repeater. ...

Capturing all subdomains dynamically using simple HTML/jQuery techniques

Currently, I am working on revamping an older web page with a monolithic structure. In my code, I have implemented a switch statement that evaluates the URL's pathname and then executes various functions to display or hide elements on the page. switc ...

Null values have been detected in the request.form elements

I am facing an issue with a simple asp.net webform. When I post it to another page, the request.form collection appears to be empty. I am unsure of what mistake I am making. Form: <form runat="server" id="myform" action="submitForm.aspx" method="post" ...

Position the DIV in the center within another DIV that contains an image

Having trouble centering a DIV horizontally inside another DIV that contains an image? It can be tricky, especially when the image is added into the mix. I've attempted various methods, but my goal is to have box2 centered at the bottom of the parent ...

What is the best way to visually refresh a polymer element that displays data from a frequently changing URL?

Hey there! I'm currently facing an issue with displaying a polymer element that contains some important information. This element is supposed to appear when I tap on an icon, but for some reason it doesn't show up even after the data has been loa ...

The background color and border are failing to apply

I've been struggling to set a background color for the class="imgd", but I can't seem to make it work. .portrait { border: #C7C7C7 solid 1px; } .circular--square { border-radius: 50%; padding-right: 10%; padding-bottom: 10 ...

Looking to streamline this intricate grid/table structure with the help of Twitter Bootstrap

I've been experimenting with creating a complex layout using tables in Twitter Bootstrap, but I'm struggling to get it just right – especially when it comes to displaying the number 5! Is there a way to achieve the same layout using divs instea ...

Troubleshooting Alignment Problem of Maximize and Close Icons in RadWindow Using ASP.Net

Currently, I am utilizing telerik radwindow to showcase a PDF document. The window seems to be functioning correctly, but there is an issue with the alignment of the maximize and close icons. Ideally, they should appear in the same row, however, they are b ...

Using CSS grid to completely fill a 100vh area without impacting the height of the rows

I am working on a grid layout with 3 columns and a variable number of rows. The background of the grid is colored, but when there are not enough rows to fill the vertical space, it remains uncolored: .grid { align-self: flex-start; display: grid; ...

Modifying the queryKey parameter within the bootstrap autocomplete functionality

I'm currently utilizing Bootstrap Autocomplete to retrieve data from Django Tastypie. The issue at hand is that Bootstrap Autocomplete uses a parameter q, whereas Tastypie follows the standard Django options. I'd like to modify q to name__contai ...

I'm having trouble aligning a bar to match the alignment of an image next to it. Instead of lining up to the right, the bar keeps widening to the left

As I construct the sidebar, I have successfully moved the image to the far right of the screen using style="text-align: right;". However, I am facing difficulty aligning a red bar in the same div along with the image. The image alignment is perfe ...

Place an element in the center without specifying its height, while making sure it does not affect

Encountering a challenging issue where alert messages need to be displayed on the page just below the navigation and above the content. The problem is demonstrated in this fiddle http://jsfiddle.net/53b6g/1/. It's tricky because the style used can&apo ...

The getimagesize() function encountered an error: "unable to access stream - file or directory does not exist"

I'm encountering a problem and I'm not sure how to solve it? Below is the script I'm using: <?php include('../include/cfg.php'); if($approval_system == 1) { $ap = 1; } else { $ap = 0; } $path = "../uploads/"; function resi ...

Use the `document.getElementsByClassName` method to retrieve elements with a specific class name and then insert content between

Is it possible to select the only post with a 'selected' class and then insert a div inside or between other divs? I am currently uploading images to my website's timeline and now I am attempting to group multiple images posted on the same d ...

Execute a jQuery script within an iframe on a webpage by triggering it with an onclick event from a

I have a homepage with two iframes. One of the iframes contains links: <a href="home.html" target="content" onclick="run.slimmscroll(#anchor1);">Home</a> <a href="home.html" target="content" onclick="run.slimmscroll(#anchor2);">Home</ ...

Deactivate the jQuery function based on the size of the window

I have a jQuery script that should be activated based on the size of the window. The current code works fine when the page loads, but it doesn't respond to window resizing as expected. The jQuery functions don't enable or disable according to th ...

AMP causing distortion in images

My images are stretching on this page and I cannot figure out the cause. Any help would be greatly appreciated. The issue seemed to arise after I implemented Bootstrap, but I am using a customized version of Bootstrap that only includes grid system and fo ...