What is the best way to vertically align a container beneath a navbar without triggering the appearance of a scrollbar?

I'm in the process of developing an application using Bootstrap 4. The app features a navigation bar and a container located beneath it. Below you'll find the structure of the application:

<body>
    <div id="root">
        <div>
            <nav id="navbar" class="navbar navbar-expand navbar-dark bg-dark">
                <div id="logo" title="App"/>
                <button aria-controls="navbar-nav" type="button" aria-label="Toggle navigation" class="navbar-toggler collapsed">
                    <span class="navbar-toggler-icon"/>
                </button>
                <div class="navbar-collapse collapse" id="navbar-nav" style="--logoHeight:68px;">
                    <div class="mr-auto navbar-nav">
                        <a href="/home" class="nav-link">Menu</a>       
                    </div>
                </div>
            </nav>
            <div class="mt-2 container-fluid">
                <div>
                    <div class="d-flex align-items-center vh-100">
                        <div class="container" id="theContainerToCenterVertically">                         
                        </div>
                    </div>
                    <span/>
                </div>
            </div>
        </body>

My objective is to center vertically the div with the ID "theContainerToCenterVertically". Although the current code achieves this, it causes a scrollbar to appear even if the content can fit on one page. I would like to know how to center the div vertically without triggering a scrollbar when the content fits on a single page.

Answer №1

To prevent content from exceeding the page height and avoid scrollbars, you can utilize Bootstrap's overflow-hidden class. Additionally, using flex allows the content div to occupy the available space, with h-100 ensuring child elements fill that space.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bb9895929d9a9d9b889988b8f858c94">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid px-0 d-flex flex-column vh-100">
    <nav id="navbar" class="navbar navbar-expand navbar-dark bg-dark">
        <div id="logo" title="App" />
        <button aria-controls="navbar-nav" type="button" aria-label="Toggle navigation" class="navbar-toggler collapsed">
            <span class="navbar-toggler-icon" />
        </button>
        <div class="navbar-collapse collapse" id="navbar-nav" style="--logoHeight:68px;">
            <div class="mr-auto navbar-nav">
                <a href="/home" class="nav-link">Menu</a>
            </div>
        </div>
    </nav>
    <div class="container-fluid mt-2 flex-grow-1 overflow-hidden">
        <div class="row h-100">
            <div class="col-4 h-100 d-flex flex-column justify-content-center">
                <p>
                    The content in this column is vertically centered and hidden if it exceeds the column height.
                </p>
                <p>
                    Lorem ipsum dolor sit amet consectetur adipisicing elit. Numquam, eum? Officia, tenetur iste nemo soluta esse nesciunt nostrum quod ut ab rem eaque, facere magnam possimus quia error, praesentium veniam? Quis, iste? Repellat magni eveniet quos minima dignissimos deserunt cupiditate a animi! Repudiandae nobis sequi quas libero aliquam officia dolor, vero quod obcaecati recusandae ut deleniti consequuntur explicabo exercitationem fugiat quidem. Odit iusto voluptatem possimus dicta recusandae soluta odio, natus nobis, earum eos rerum ratione atque temporibus tenetur numquam reiciendis non. Odit adipisci, numquam fuga facere nesciunt aperiam consectetur expedita autem iure incidunt eligendi placeat necessitatibus animi blanditiis aspernatur nostrum.
                </p>                    
            </div>
            <div class="col-8">
                <p>The content in this column is not centered.</p>
            </div>
        </div>
    </div>
</div>

For demonstration purposes, I used two columns for the content area. If only one column is needed, you can simply use col or col-12.

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

Submitting data through a PHP server-side script

I am facing a dilemma regarding why the form is not submitting and directing me to the index.php page. Being new to PHP, I am attempting to utilize the User object. Below is my current progress: <main> <header> <h1>Cr ...

Adaptable CSS triangle design

Hello I'm looking to create a responsive CSS triangle similar to the image linked below. I attempted using percentages with border width, but didn't achieve the desired outcome. Can anyone suggest a simple solution? https://i.stack.imgur.com/Yah ...

What is the best method to eliminate the 2px flaws on the right edge?

Issue only observed on small screens using Android Chrome (Nexus 5x, Nexus 6) with Android 5+. Cannot replicate problem on a Nexus 4. Utilizing basic bootstrap setup and angular; unsure if related. Experiencing artifacts up to 2px wide on the right side o ...

Aligning content within a div block

I created a div block that houses a form structured like this: <div class="divClass"> <form id="frm" method="post" action="/NotDefinedYet/index.xhtml" class="frmClass"> <input type="text" name="j_idt9:j_idt10:Username" placehold ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

Applying a gradient overlay to an image using CSS

Feeling a bit frustrated with this issue. I'm working on my Joomla website utilizing Phoca Gallery, and the code it generates includes the following: <img class="pg-image" src="/images/phocagallery/other/thumbs/phoca_thumb_m_something.jpg" alt="So ...

Retrieve detailed Paypal donation transaction information in HTML format with the donation button functionality integrated

I'm currently in the process of building a web application for a non-profit organization, which includes a Paypal Donation feature. I am using HTML5, CSS3, and jQuery to develop this application. I have successfully implemented the Donation button fro ...

In a multidimensional array, locate the key corresponding to the specified value

I am facing an issue with my code that involves an array containing various fruits with product ID and price as keys for different values. While I am able to retrieve the correct price, I am struggling to get the name of the chosen product. For instance, ...

What is the best way to retrieve the value of a custom attribute from a dropdown list using either JavaScript or jQuery?

I have a dropdown list on a web form and I need to have a 'hidden' variable for each item in the list that can be retrieved using the onchange event on the client side. To achieve this, after databinding the dropdownlist, I am setting a custom at ...

Setting initial opacity for CSS on page load

I'm not a pro at web design, but I'm trying to create my own website. I've divided my page into two sections - the left side for the menu bar and the right side for content. To add a 'cool' blur effect over my menu bar, I decided t ...

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Is it possible to keep content visible in Bootstrap tab by adding CSS?

Having an issue with the "formbox" class that is causing unexpected behavior when applied to a form. When removed, everything works as expected. I've tried narrowing down the issue by removing each CSS rule individually and testing it out but haven&ap ...

Execute a function only once when using it in conjunction with ng-repeat in AngularJS

I need a way to ensure that a specific function is only called once when used in conjunction with ng-if and ng-repeat. <td ng-if="!vm.monthView && vm.yearView=='contract-year'" nginit="vm.ContractYearBaselines()" class="baseline-data ...

Displaying content using Jquery's slideDown feature, overlapping existing content

I am working on displaying one piece of content over another using Jquery slidedown as an overlay. Despite adding z-index properties, I am struggling to make it work. My goal is to have the div with class "selection_nip" appear as an overlay on top of ano ...

Automatic Update Division in Jade Template (Node.js/Express)

In my development project using Node.js, Express, Jade, and Redis, I have successfully created an app that pulls telnet stream data from reversebeacon.net. This data is then cross-referenced with Redis databases containing amateur radio club member informa ...

margin-top: automatic adjustment, with a minimum of 50 pixels

I am trying to add a minimum of 50px margin to the top of my footer tag using CSS, but I haven't been successful with the min() function. I'm not sure if I am overlooking something or if there is another correct approach to achieve this. * { ...

Hover effects in CSS animations can be hit or miss, with some working smoothly while others may

Below is the HTML content: <div class="wrapper"> <figure align="center"><img src="http://www.felipegrin.com/port/or-site.jpg" alt=""><br><span>RESPONSIVE HTML5 WEBSITE FOR <br> OR LEDS COMPANY</span></fig ...

Troubles with Placing Image on WordPress

Working on a Wordpress component for a website where I placed a "SOLD OUT" image over text. However, the issue arises when viewing the site on mobile devices, as the images are smaller and not in their correct positions due to using absolute positioning. ...

The Angular Material md-input-container consumes a significant amount of space

Currently, I am exploring a sample in Angular Material. It appears that the md-input-container tag is taking up a substantial amount of space by default. The output is shown below: However, I have come across the md-input-content tag, which occupies less ...

Deleting the clone <div> while ensuring the main <div> is kept clear of any remaining data

Initially: After adding a new row and then removing it. Why is this happening? When I set val(""), the textbox should have no value. What mistake did I make in my code? Please assist. Here's the code snippet: <div id="rows" class="block"> ...