Having trouble configuring the sticky-footer correctly

Currently enrolled in a web development course on Udemy, I am facing an issue with the footer on my webpage. Even after setting its CSS position to relative, the footer overlaps the content when more data is added. However, removing this positioning causes the footer to jump up when the page isn't completely filled, such as on the Login page.

I need guidance on how to make the footer stick to the bottom of the page, adjusting accordingly as the content fills up and reaches the footer's edges. Please note that I am using EJS with header and footer partials.

Here is the HTML for the Login form:

<% include ./partials/header %>

<div class="row">
    <div class="col-md-12">
        <form class="form-signin" action="/login" method="POST">
            <h1 class="mb-3 text-center">Please sign in</h1>
            <label for="inputUsername" class="sr-only">Username</label>
            <input type="text" id="inputUsername" class="form-control" name="username" placeholder="Username" required
                autofocus>
            <label for="inputPassword" class="sr-only">Password</label>
            <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password"
                required>
            <div class="mb-3">
                <a href="/forgot" id="forgotPassword">Forgot password?</a>
            </div>
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>
        <div class="form-signin">
            <a href="javascript:history.go(-1)">Go back</a>
        </div>
    </div>
</div>

<% include ./partials/footer %>

The header section:

<!DOCTYPE html>
<html>

<head>
    <title>Yelp Camp</title>
    <meta name="viewwport" content="width-device-width, initial-scale-1">
    <link href="https://stackpath.bootstrapcdn.com/bootswatch/4.2.1/united/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-udHIRBY7G8ZUr7aO8wRn7wD4bsGGRLR5orCz1FV93MZ7232xhAdjDYEvqeZLx45b" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.css">
    <link rel="stylesheet" href="/stylesheets/main.css">
</head>

<body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-primary mb-2">
        <a class="navbar-brand" href="/">YelpCamp</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01"
            aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarColor01">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'campgrounds' ? 'active' : '' %>">
                    <a class="nav-link" href="/campgrounds">Home</a>
                </li>
            </ul>

            <ul class="navbar-nav navbar-right">
                <% if(!currentUser){ %>
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'login' ? 'active' : '' %>">
                    <a class="nav-link" href="/login">Login</a>
                </li>
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'register' ? 'active' : '' %>">
                    <a class="nav-link" href="/register">Sign Up</a>
                </li>
                <% } else { %>
                <li class="nav-item">
                    <a class="nav-link" href="#">Signed in as
                        <%= currentUser.username %></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/logout">Logout</a>
                </li>
                <% } %>
            </ul>
        </div>
    </nav>

    <div>
        <% if (error && error.length > 0) { %>
        <div class="alert alert-danger" role="alert">
            <%= error %>
        </div>
        <% } %>
        <% if (success && success.length > 0) { %>
        <div class="alert alert-success" role="alert">
            <%= success %>
        </div>
        <% } %>
    </div>
    <div class="container">
        <div class="page-content">

The footer section:

</div> <!-- /.page-content -->
</div>
<footer class="footer">
    <div class="container">
        <p class="text-muted">
            &copy; YelpCamp 2018 | <a href="/campgrounds">Home</a>
        </p>
    </div>
</footer>
<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
<!-- Bootstrap JS CDN -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
    crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
    crossorigin="anonymous"></script>

</body>
</html>

The relevant CSS styles:

.form-signin {
    width: 100%;
    max-width: 330px;
    padding: 15px;
    margin: auto;
}

.form-signin .checkbox {
    font-weight: 400;
}

.form-signin .form-control {
    position: relative;
    box-sizing: border-box;
    height: auto;
    padding: 10px;
    font-size: 16px;
}

.form-signin .form-control:focus {
    z-index: 2;
}

.form-signin input[type="text"] {
    margin-bottom: -1px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 0;
}

.form-signin input[type="password"] {
    margin-bottom: 10px;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

.page-content {
    min-height: 100% !important;
    padding: 0 0 -60px !important;
    position: relative;
}

footer .footer-push{
    height: 60px !important;
    position: absolute !important;
    clear: both;
}

.container .text-muted{
    margin: 20px 0 0;
}

Despite trying several solutions for sticky footers, none have resolved the issues I'm facing. The main concerns are:

  • The footer appears below screen bounds or right after the login form on pages with minimal content;
  • On pages with sufficient data, the footer overlaps the content prematurely.

Current challenges:

Codepen link showcasing the Login Page example code. (Note: Problem visible only in Full view)

Your assistance in finding a suitable solution would be greatly appreciated. Thank you.

Answer №1

Make sure the footer is positioned at the bottom of the screen by adding this CSS code:

.footer {
    bottom: 0;
}

If you encounter any issues with the footer overlapping content, simply add margin or padding to the last div element equal to the height of the footer.

Answer №2

Utilize the CSS property position:fixed to create a sticky element and specify bottom:0 to determine its position.

Here is an example:

.form-signin {
    width: 100%;
    max-width: 330px;
    padding: 15px;
    margin: auto;
}

.form-signin .checkbox {
    font-weight: 400;
}

.form-signin .form-control {
    position: relative;
    box-sizing: border-box;
    height: auto;
    padding: 10px;
    font-size: 16px;
}

.form-signin .form-control:focus {
    z-index: 2;
}

.form-signin input[type="text"] {
    margin-bottom: -1px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 0;
}

.form-signin input[type="password"] {
    margin-bottom: 10px;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

.page-content {
    min-height: 100% !important;
    padding: 0 0 -60px !important;
    position: relative;
}

footer .footer-push{
    height: 60px !important;
    position: absolute !important;
    clear: both;
}

.container .text-muted{
    margin: 20px 0 0;
}

footer {position: fixed; bottom:0; text-align:center; width:100%; background:#fff;}
<div class="row">
    <div class="col-md-12">
        <form class="form-signin" action="/login" method="POST">
            <h1 class="mb-3 text-center">Please sign in</h1>
            <label for="inputUsername" class="sr-only">Username</label>
            <input type="text" id="inputUsername" class="form-control" name="username" placeholder="Username" required
                autofocus>
            <label for="inputPassword" class="sr-only">Password</label>
            <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password"
                required>
            <div class="mb-3">
                <a href="/forgot" id="forgotPassword">Forgot password?</a>
            </div>
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>
        <div class="form-signin">
            <a href="javascript:history.go(-1)">Go back</a>
        </div>
    </div>
</div>
<!DOCTYPE html>
<html>

<head>
    <title>Yelp Camp</title>
    <meta name="viewwport" content="width-device-width, initial-scale-1">
    <link href="https://stackpath.bootstrapcdn.com/bootswatch/4.2.1/united/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-udHIRBY7G8ZUr7aO8wRn7wD4bsGGRLR5orCz1FV93MZ7232xhAdjDYEvqeZLx45b" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.3.3/semantic.css">
    <link rel="stylesheet" href="/stylesheets/main.css">
</head>

<body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-primary mb-2">
        <a class="navbar-brand" href="/">YelpCamp</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01"
            aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarColor01">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'campgrounds' ? 'active' : '' %>">
                    <a class="nav-link" href="/campgrounds">Home</a>
                </li>
            </ul>

            <ul class="navbar-nav navbar-right">
                <% if(!currentUser){ %>
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'login' ? 'active' : '' %>">
                    <a class="nav-link" href="/login">Login</a>
                </li>
                <li class="nav-item <%= typeof page !== 'undefined' && page === 'register' ? 'active' : '' %>">
                    <a class="nav-link" href="/register">Sign Up</a>
                </li>
                <% } else { %>
                <li class="nav-item">
                    <a class="nav-link" href="#">Signed in as
                        <%= currentUser.username %></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/logout">Logout</a>
                </li>
                <% } %>
            </ul>
        </div>
    </nav>

    <div>
        <% if (error && error.length > 0) { %>
        <div class="alert alert-danger" role="alert">
            <%= error %>
        </div>
        <% } %>
        <% if (success && success.length > 0) { %>
        <div class="alert alert-success" role="alert">
            <%= success %>
        </div>
        <% } %>
    </div>
    <div class="container">
        <div class="page-content">
          </div> <!-- /.page-content -->
</div>
<footer class="footer">
    <div class="container"> 
        <p class="text-muted">
            &copy; YelpCamp 2018 | <a href="/campgrounds">Home</a>
        </p>
    </div>
</footer>
<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
    crossorigin="anonymous"></script>
<!-- Bootstrap JS CDN -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
    crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
    crossorigin="anonymous"></script>

</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

Arrangement of SVGs within one another

I am working on achieving a layout with multiple SVG elements arranged in a specific way. This layout involves a top-level gray box containing several orange boxes, each of which holds red boxes. The width and height of the top-level SVG are known. https: ...

Validate Bootstrap - Transmit data from all form fields to external PHP script

Is there a way to send all input field values to a remote PHP file using Bootstrap Validator? In my log in form, I have two input fields. I'm utilizing Bootstrap Validator's remote validation on both of them. However, each validation only sends ...

Styling the pseudo element ::part() on an ion-modal can be customized based on certain conditions

Looking for a solution regarding an ion-modal with specific CSS settings? I previously had the following CSS: ion-modal::part(content) { width: 300px; height: 480px; } Now, I need to adjust the height based on conditions: if A, the height should be lo ...

What is the best way to eliminate specific elements from an array of objects in MongoDB aggregate based on a condition?

I have a database of documents called ChatRooms stored in MongoDB with the following structure: { _id: ObjectId('4654'), messages: [ { user: ObjectId('1234'), sentAt: ISODate('2022-03-01T00:00:00.000Z') ...

Unwrapping nested objects in a JSON array with JavaScript: A step-by-step guide

After trying some code to flatten a JSON, I found that it flattened the entire object. However, my specific requirement is to only flatten the position property. Here is the JSON array I am working with: [{ amount:"1 teine med 110 mtr iletau" comment:"" ...

What changes can I make to the script in order to display every element from a MySQL query using PHP, MySQL, JavaScript,

Enhanced Multiple Dropdown Selection Using Ajax In the following scenario, we have a webpage featuring a multiple dropdown selection that interacts with a MySQL database. By choosing options in the dropdowns labeled site, menu, and category (categ), a que ...

Why is it that aligning items in a row doesn't seem to function properly when I apply a custom class to a container-fluid in Bootstrap 4?

Recently, I started delving into the world of front end development. After mastering the basics of HTML5 and CSS3, I ventured into Bootstrap 4. My current project involves creating a replica of the Facebook login page. To achieve a full-width design, I uti ...

Input the chosen icon list values into a designated box

As a beginner in the world of Javascript, I am venturing into creating a password generator that involves using icons. The concept is simple - by clicking on any number of icons from a list, the corresponding hex code will be displayed in an input box. The ...

What is the best practice for adding one string to the end of another?

So, is this the best way to concatenate strings in JavaScript? var str = 'Hello'; str += ' World'; While most languages allow for this method of string concatenation, some consider it less efficient. Many programming languages offer a ...

The twelfth child in the sequence is not functioning properly, although the others are working correctly

In my list, I have 12 elements and each element contains a div. My goal is to set different sizes for each div by using nth-child on the li element. However, the configuration for the 12th element does not seem to work correctly compared to the rest of the ...

challenge with altering data in transmission when using the GET method

$('.textedit').editable('/webpage/skeleton/edit_text/text/process', { loadurl: '/webpage/skeleton/edit_text/loadraw', loaddata: {id: $(this).attr('id'), client: $(this).data('client')}, submitda ...

Run a chunk of HTML with dynamic PHP elements using JavaScript

Within a single .php file, there is a combination of HTML, PHP, and JavaScript. The JavaScript section is crucial for detecting the browser; if the browser is not Internet Explorer, JavaScript will trigger the HTML block containing PHP. Here is the JavaS ...

What strategies can be implemented to avoid re-rendering in Angular 6 when the window is resized or loses focus?

I am currently working with a component in Angular 6.0.8 that consists of only an iframe element. Here is the code in page.component.html: <iframe [src]="url"> The logic for setting the URL is handled in page.component.ts: ngOnInit() { this.u ...

Is there a way to exclude a specific div based on two select choices?

Check out my Fiddle demonstration here $('select#classes').on('change', function() { var selectedClass = $(this).val(); $('.col-sm-6:not(:contains(' + selectedClass + '))').hide(); $('select#subjec ...

Creating a TypeScript generic type for the "pick" function to define the types of values in the resulting object

I am facing an issue while writing the type for the pick function. Everything works smoothly when picking only one key or multiple keys with values of the same type. However, if I attempt to pick a few keys and their values are of different types, I encoun ...

Using JQuery to Customize Navigation Menu Targeting

On my website, I am trying to make an href button and a menu fade in as the page loads. However, I am struggling to find the correct code due to its placement within multiple tags, especially since I am new to JS and JQuery. I have provided a fiddle with a ...

What are the differences between Node's bcrypt and bcryptjs libraries?

When it comes to using bcrypt in Node, the abundance of libraries available can be overwhelming. Among the top packages on npm are bcrypt with 247k downloads per month bcryptjs with 337k downloads per month (any other options worth considering?) What s ...

Error in Dimplejs: Line is not visible when series is empty

I have a dimplejs.org line chart set up. I am trying to colorize the Clicks data points from blue to red (blue for fewer clicks and a gradient from blue to red for more clicks). When I set the series as shown below, it works fine but the tooltip only incl ...

Tips for updating information when a button is chosen

Hello everyone, I need some help with a form that has three select buttons. The button labeled "Distribute" is already selected when the page loads, and it contains information about full name, password, and location. How can I use JavaScript to create a c ...

Elessar - addressing touch event issues

I have been experimenting with implementing multiple ranges using a library called elessar. Despite being a great library, the documentation is lacking. I managed to set everything up but it doesn't seem to work on touch devices. There are no errors s ...