Adjust the content division to be 100% in height, while accounting for the changing height of the

I'm currently utilizing Bootstrap 4 and have a rather intricate footer:

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

...that dynamically adjusts its height. Since this is a PHP application, the content in the center of the screen is subject to change. When the content exceeds the screen size, it correctly pushes the footer to the bottom. However, there are instances where the content is minimal, resulting in whitespace below the footer.

There's also a header in place.

Is there a way to use CSS to modify the Bootstrap classes so that if, and only if, the combined height of the content and footer doesn't fill 100% of the viewport height, the content expands accordingly?

Here's how it currently appears:

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

As visible, there's space at the bottom of the screen below the footer. My intention isn't to create a sticky footer or enable scrolling in the middle; I simply want the footer to sit at the bottom of the screen when the content between the header and footer doesn't consume the remaining height.

Everything I've come across thus far involves setting a fixed height for the footer, but I'm concerned about its responsiveness across different screen sizes.

Below is my basic HTML layout with Bootstrap classes:

    <body>
    <!--Navbar-->
    <nav class="navbar navbar-expand-lg navbar-dark fixed-top top-nav-collapse">
        ...
    </footer>
</body>

This is the customized CSS I applied to the page:

html,
body,
header {
    height: 100%;
}

body {
    padding-top: 70px;
}

Any guidance on tackling this issue would be highly appreciated.

Thank you.

Answer №1

After tackling this issue, I came up with a solution that may not be the most elegant, but it gets the job done for me.

I had to leverage a mix of Javascript and CSS because I couldn't find a pure CSS workaround.

The challenge I faced was dealing with a dynamically changing footer height based on screen size, as well as varying content in the main section served by the server. Sometimes the content is lengthy, requiring users to scroll to view the footer (I intentionally avoided a sticky footer), while other times the content is shorter, causing the footer to display right below the content with whitespace at the bottom. To address this within Bootstrap 4.5 framework, I made the following adjustments:

To eliminate the unwanted space without resorting to a sticky footer, here's what I did.

CSS:

html,
body,
header {
    height: 100%;
}

body {
    padding-top: 70px;
}

The HTML structure remains unchanged:

<body>
    <!--Navbar-->
    <nav class="navbar navbar-expand-lg navbar-dark fixed-top top-nav-collapse">
        <div class="container">
            <a class="navbar-brand" href="https://dization.com/" target="_blank"><img src="../public/images/cropped-logo_white.png" width="185" height="58px"></a>

            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#basicExampleNav" aria-controls="basicExampleNav" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
            </button>
        </div>
    </nav>
    <main class="mt-4">
        <div class="container">
               <div class="row justify-content-center" id="divCardSignUp">
                    <div class="col-lg-12">
                        <div class="card my-5">
                            <div class="card-header pt-4 pb-4 text-center bg-light">
                                    <span><img src="../public/images/Logo-no-text.png" alt="" height="30"></span>
                            </div>
                            <div class="card-body p-4">
                                  <!-- boot strap rows and cols divs in here -->
                            </div> 
                        </div>
                    </div>
                </div>
        </div>
    </main>
    <footer class="page-footer font-small unique-color-dark mt-2">
        <div class="primary-color">
            <div class="container"></div>
        </div>
        <div class="container mt-5 mb-4 text-center text-md-left">
            <div class="row mt-3"></div>
        </div>
        <div class="footer-copyright text-center py-3"></div>
    </footer>
</body>

Additionally, I included this JS code before the closing body tag:

    <script language="JavaScript">
        function footerAlign() {
            var footerHeight = $('footer').height();
            var navHeight = $('nav').height();
            $('main').css('min-height', 'calc(100vh - ' + (footerHeight + navHeight + 32)  + 'px');
        }
        $(document).ready(function() {        
             footerAlign();
        });
        $(window).resize(function() {
            footerAlign(); 
        });
    </script>

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

CSS Child Selectors: Select elements based on their child values

Suppose I have an HTML setup that looks like this: <html><head></head> <body> <table> <tr> <td> <table> ...

Sophisticated CSS design featuring a heart-shaped cutout from the background

I am trying to design a shape using only CSS (no images) that is the opposite of a heart shape. It's a bit hard to explain, so here is a visual representation: The blue represents the background, but the shape I am aiming for is not a heart, it is ac ...

Adding icons to form fields based on the accuracy of the inputs provided

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Assignment 2 - Website Bui ...

Ways to adjust iframe height as it loads

$('#song-link').change(function () { var link = $('#song-link').val(); SC.oEmbed(link, { element: document.getElementById('putTheWidgetHere') }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1 ...

The mobile menu dropdown link in jQuery is not functioning as expected

I am having an issue with my drop down menu links - they are not clickable. Every time I try to click on them, the menu just closes back to its original position and nothing happens. If you want to see the issue, you can check out my fiddle here. EDIT: T ...

Adjust the CSS for the "a" tag's "title" attribute

I am using jquery.fancybox to create an image modal on my website. I have noticed that I can add a description using the title attribute. However, when the description is too long, the text is displayed on a single line and some of it disappears. How can ...

Background cutting off right side of HTML website

While updating my supervisor's university website, I noticed a problem with the responsiveness. When resizing the browser window, a horizontal scroll bar would appear and the right side of the website would get covered by the background. On mobile dev ...

Struggling to keep up with responsive behavior on a page featuring vertically and horizontally centered content that spans the full height

I'm working on a full-height website and the first section needs to have both vertical and horizontal centered content. The issue I'm facing is that there's an image included, which doesn't scale responsively when the screen size change ...

Material UI AppBar fixed position that is not part of a grid container

Hey everyone, I'm really struggling with this issue. I recently set my AppBar to have a position of "fixed". However, now the appbar is no longer contained within its container and instead spans the entire screen. I've been trying to figure it ou ...

Place the two buttons in position

I have a section where I need to include two buttons. One button on the left labeled "Sunday" and one on the right labeled "misc". I want the two buttons to be evenly placed within the area, so I assigned them the same class since they are the same size. ...

The Stylish Choice: Materialize CSS Dropdown Selector

I am currently integrating Materialize CSS into my application and have used media queries to ensure that the layout is responsive. However, I am facing an issue with a select dropdown element. It works fine on laptops but does not allow for selecting any ...

What is the best way to place two radio buttons side by side in a row?

How can I place two radio buttons in a single row with their labels, like the example shown in the picture? ...

The css values for component _nghost-c0 in an Angular application

Recently, I've been delving into Angular 5 and couldn't help but notice the peculiar html tags with ng generated attributes like _nghost-c0 and _nghost-c1... This got me wondering, what exactly do these attributes signify? [_nghost-c3] .employee ...

Guide to changing the background colors of multiple elements when hovered over by the mouse?

I want to customize my website's search bar by changing the background color when it is hovered over. Currently, the search bar has two main elements: the text box and the submit button. I have successfully programmed the text box element to change to ...

FlexBox in CSS not aligning horizontally on vBulletin forum in Internet Explorer

I've been working on aligning four images/links using Flexbox for a basic banner row. It's working smoothly in Chrome and Firefox, but in IE 11, the Flexbox is not behaving as expected, causing the images to stack vertically instead of horizontal ...

What could be causing the div to remain stationary despite using the offset-xs-* class in Bootstrap 4?

I wrote this HTML: <h2>Column Offsetting</h2> <div style="max-width: 1200px; margin: 0 auto; padding: 10px;"> <div class="container-fluid"> <div class="row" style="margin-top: 20px ...

Intermittent Z-index conflicts in need of a solution - seeking guidance

Is there a way to make the h1 heading and img image elements appear on top of the opaque div they are enclosed in? I want them to look unaffected by the transparency of their parent div. Here is a Fiddle demonstrating my issue: <div id="main"> ...

Can you make a vertical line that remains a constant size even when the window is resized?

Can someone help me with this? I'm not sure if I'm using the right values for top, left, and bottom. Here's an example below: div.line { width:4px; background-color: black; position: fixed; top:180px; bottom:400px; ...

Two objects precisely located in the same spot yet not visible simultaneously

There are two elements here. One is a transparent button, and the other is an image positioned behind it. The background property didn't work for me, so I attempted different variations without success: .top-container > button { background-ima ...

What is the process for creating a sub-menu for a dropdown menu item in Bootstrap 5?

https://i.sstatic.net/o4FLn.pngthis is my code which i have created this navigation bar with bootstrap and all of its drop downs but i want to add another drop down to the services drop down section inside of webdevelopment but it can't any easy solut ...