Ways to align a div vertically in relation to the remaining space's height

After successfully creating a webpage with a "sticky" footer using flexbox, I am now challenged with centering the content within the remaining height (vh - navbar height - footer height) when the content is small. Check out the image below for a visual representation.
https://i.sstatic.net/aDyx7.png

Below is my code snippet.

<div id="app">
    <div>
        <div class="d-flex flex-column full-height">
            <div id="div-top" class="d-flex flex-column" style="flex: 2">
                <nav class="navbar navbar-expand-md navbar-dark bg-primary sticky-top">
                    <div class="container">
                        <a class="navbar-brand" href="/">
                            <img class="logo d-inline-block align-top" src="https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-256.png" width="30" height="30"> Brand</a>
                        <button type="button" class="navbar-toggler">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse">
                            <ul class="ml-auto navbar-nav">
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/about">About</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/aaar">Ada</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/bbbr">sdfsdfs</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </nav>
                <br>
                <div class="container d-flex align-items-center justify-content-center h-100" id="div-1">
                    <div class="row w-100">
                        <div class="text-center col col-md-12 col-lg-6">
                            <img class="logo" src="https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-256.png" width="256" height="256" alt="logo">
                            <h2 class="pt-2 pb-3 font-weight-normal">Description</h2>
                            <div>
                                <a href="#" class="btn btn-primary">Add</a>
                                <p>description</p>
                            </div>
                            <p class="pt-4 text-muted">v1.0.0</p>
                        </div>
                        <div class="d-none d-lg-block col col-lg-6">test</div>
                    </div>
                </div>
            </div>
            <footer class="footer">
                <div class="text-center container">
                    <span class="d-block">Copyright &copy; 2018</span>
                    <span class="text-muted">Some text here</span>
                </div>
            </footer>
        </div>
    </div>
</div>
#div-1 {
  background-color: #00ff00;
}

.full-height {
    height: 100vh;
}

.footer {
    margin: auto auto 0 auto;
    width: 100%;
    padding-top: 1.2rem;
    padding-bottom: 1.2rem;
    background-color: #ff0000;
}

The objective is to ensure a consistent layout across various screen sizes without altering the content's height. However, two issues have been identified with this approach. Firstly, it doesn't function as intended in landscape mode on mobile devices (tested via Chrome dev tools emulation). Secondly, the navbar experiences height reduction.

You can access the jsfiddle version here. Feel free to compare it with the original version. Add /embedded/result/ at the end of the URL to view the result in fullscreen. Your assistance is greatly appreciated!

Answer №1

To achieve the desired outcome, make the following adjustments:

  • Update min-height: 100vh in the .full-height rule

  • Remove align-items-center/h-100 and add m-auto to the class list of elements with id="div-1"

Note that the <br> tag you had after the </nav> is invalid, so I've taken it out. If you need spacing there, consider using margin or padding properties instead.

Check the updated fiddle

Stack snippet

#div-1 {
  background-color: #00ff00;
}

.full-height {
    min-height: 100vh;
}

.footer {
    margin: auto auto 0 auto;
    width: 100%;
    padding-top: 1.2rem;
    padding-bottom: 1.2rem;
    background-color: #ff0000;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet"/>

<div id="app">
    <div>
        <div class="d-flex flex-column full-height">
            <div id="div-top" class="d-flex flex-column" style="flex: 2">
                <nav class="navbar navbar-expand-md navbar-dark bg-primary sticky-top">
                    <div class="container">
                        <a class="navbar-brand" href="/">
                            <img class="logo d-inline-block align-top" src="https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-256.png" width="30" height="30"> Brand</a>
                        <button type="button" class="navbar-toggler">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse">
                            <ul class="ml-auto navbar-nav">
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/about">About</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/aaar">Ada</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" aria-current="false" href="/bbbr">sdfsdfs</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </nav>
                <div class="container d-flex justify-content-center m-auto" id="div-1">
                    <div class="row w-100">
                        <div class="text-center col col-md-12 col-lg-6">
                            <img class="logo" src="https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-256.png" width="256" height="256" alt="logo">
                            <h2 class="pt-2 pb-3 font-weight-normal">Description</h2>
                            <div>
                                <a href="#" class="btn btn-primary">Add</a>
                                <p>description</p>
                            </div>
                            <p class="pt-4 text-muted">v1.0.0</p>
                        </div>
                        <div class="d-none d-lg-block col col-lg-6">test</div>
                    </div>
                </div>
            </div>
            <footer class="footer">
                <div class="text-center container">
                    <span class="d-block">Copyright &copy; 2018</span>
                    <span class="text-muted">Some text here</span>
                </div>
            </footer>
        </div>
    </div>
</div>

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

Utilizing the HTML5 Pattern Attribute in Conjunction with Websockets

Being completely new to web programming, I am struggling to understand why the pattern attribute in the text field below is not validating properly due to my javascript. <form id="aForm"> <input type="text" pattern="^[ a-zA-Z0-9,#.-]+$" id="a ...

What are the reasons why Bootstrap icons are not functioning properly in ReactJS?

I am facing an issue with bootstrap icons not working in ReactJS. Click here to view my buttons This is the code for my post items: import React from 'react'; const PostListItem = () => { return ( <li className="app-list ...

Is the space-between property not delivering the desired spacing?

I am trying to achieve equal spacing between list items, and I attempted the following code. ul { list-style-type: none; display: flex; justify-content: space-between; } However, it seems that this approach is not working as expected. I am confused ...

Show picture during the process of loading an external webpage

Utilizing a script to load an external page (external meaning a page within my website) inside a div. Loading the page takes time, so I want to display an image until the page is fully loaded. JAVASCRIPT function HideLoader() { $('#loader' ...

Establishing a class for wp_nav_menu

Is there a way to transform the following code: <ul class="ulStyle"> <li class="liStyle"> <div class="first"> <div class="second"> menu1 </div> </div> </li> </ul> into wp_nav_menu? I'm struggling with ...

Tips for showing the data from a JSON object with numerous arrays in Angular HTML

I managed to create a JSON object by parsing CSV file fields using the papa Parse library. Now, my goal is to showcase this data on an HTML page. However, I'm struggling with how to extract the arrays from the JSON object and transfer them into a vari ...

Does Internet Explorer have a tool similar to Firebug for debugging websites?

Is there a tool similar to Firebug that is compatible with Internet Explorer? Edit I am currently using IE8, so I need a solution that works specifically with IE8. ...

Enhancing websites with font-awesome icons and upgrading from older versions to the latest

Seeking guidance on updating our project to use the latest Macadmine theme. The transition from Font Awesome 3 to Font Awesome 4 requires changing all icons to their proper names. I came across a helpful resource at https://github.com/FortAwesome/Font-Aw ...

Guide for retrieving the maximum length of an input field using JavaScript

Is it possible to retrieve the maxlength of an input field using JavaScript? <input type="password" id="password" maxlength="20" > I attempted to do this, however it only returns undefined console.log(document.getElementById("password").maxlength) ...

Determining the aspect ratio of an image is essential in using flexbox to achieve consistent heights

Recently, I came across a fascinating code pen titled: this demonstration of equal responsive height images I am determined to make sure that all images have the same height, regardless of their width/height variances. The CSS displayed below utilizes fle ...

What can be done to improve the elegance of this jQuery function?

I am facing an issue on my webpage where I have a drop-down select box with a default value of (empty). The problem arises when the user selects an entry, and the appropriate form for that entry type should be displayed. However, there are a couple of iss ...

Stencil - React Integration Does Not Support Global CSS Styling

As per the guidance provided in the Stencil docshere, I have established some global CSS variables within src/global/variables.css. This file is currently the sole CSS resource in this particular directory. Upon attempting to incorporate my components int ...

Avoid the resetting of chosen value following a POST or submission

I am currently working on creating a "web dashboard" that requires a year_from parameter from an HTML form. Despite setting up my Flask backend to return data within this specified range, I am encountering a problem where every time I hit submit, the selec ...

The jQuery css method fails to apply styles to SVG circle elements

HTML: <div class="container"> <article class="caption"> <svg class="grid-point" width="100" height="100" style="height: 120px; width: 625px;"> <circle class="myPoint" cx="50" cy="50" r="5" fill="#80E1EE" / ...

"Add a touch of magic to your webpage with text effects that appear

I am looking to create a glowing text effect, and I stumbled upon this resource. http://jsfiddle.net/karim79/G3J6V/1/ However, the client prefers the text effect to appear after the page loads, rather than on hover. Unfortunately, I do not have experien ...

Different ways to modify the color and thickness of a variable in HTML5 with either JavaScript or CSS

I am currently working with a JavaScript file where I have a variable defined as follows: var nombre = document.getElementById('nombre').value; The 'nombre' variable corresponds to an element in an HTML file: Nombre: <input type=" ...

What is the process for updating button text upon clicking in Angular?

To toggle the text based on whether this.isDisabled is set to false or true, you can implement a function that changes it when the button is clicked. I attempted to use this.btn.value, but encountered an error. import { Component } from '@angular/core ...

Effective method for creating a responsive navbar

On my webpage, I have a navigation bar with three buttons on the left side and a search bar on the right side. While I attempted to write code to make them responsive for smaller screens, it seems that my approach was incorrect as they are still not respon ...

The image appears uniquely sized and positioned on every screen it is viewed on

After reviewing the previously answered topics on the site, I couldn't seem to make the necessary adjustments. Despite setting the size and location in the css file, the large image remains centered and fitting for the screen. The "imaged1" class seem ...

Barba.js Revolutionizes Page Reloading for Initial Links

On my Wordpress site, I am using Barba js v.2 for page transitions. However, I have encountered an issue where I need to click on a link twice in order to successfully change the page and make the transition work. Interestingly, the first time I click on t ...