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

jQuery prioritizes enforcing style over using a CSS file

I am facing an issue with a nested div functioning similar to a drop-down menu. The problem arises when I click on the #two div as I want it to disappear. To handle this, I utilized jQuery and tried using both .hide() and .slideUp(). However, upon implem ...

Tips for creating two lines of text within one line using CSS

My explanation skills are not the best. I have the words "Tasty Burger" stacked on top of each other, but I want them to be on a single line. .footer-container { margin: 25px 0; display: flex; gap: 3rem; align-items: center; justif ...

"Troubleshooting: Issue with CSS code on Codepen when trying to create

I am attempting to replicate the Google logo using HTML and CSS. While the code functions properly in browsers, there seems to be an issue with Codepen not positioning the horizontal blue line correctly. How can this be resolved? What adjustments should ...

Using TypeScript with Angular, you can easily include parameters to HTML tags

I have an HTML element that looks like this: eventRender(info){ console.log(info.el); } This is the output I'm seeing: https://i.stack.imgur.com/G0hmw.png Now, I want to include these attributes: tooltip="Vivamus sagittis lacus vel augue ...

Creating a modal using JavaScript and CSS

When I click on the hello (plane geometry) as shown in the figure, a popup appears on the extreme left of the screen instead of appearing on the plane geometry box. Can someone please help me fix this issue? I have also included the code below: https://i. ...

The adhesive navigation bar isn't adhering

I'm having issues with making my navbar inside a header sticky over the whole page. Despite trying various solutions like removing overflow, adjusting the flexbox, and setting a specific height on the parent element, I still can't get it to work. ...

The Facebook Comments widget causes the webpage to automatically scroll to the bottom on older versions of Internet Explorer like

My webpage is quite lengthy and includes a Facebook comments widget at the bottom. However, when loading in IE7 and IE8, the page instantly jumps to the bottom due to this widget. Interestingly, removing the widget allows the page to load normally. This ...

How can you align square cells within a container using CSS grids while maintaining a consistent grid gap?

Our objective is to perfectly align square cells with the edges of their container while maintaining a consistent gap between cells in each row and column. Although this Codepen solution is close, there are two key issues: (1) vertical spacing doesn' ...

How can I prevent list items in jQuery Mobile from being truncated with ellipses?

Here is the list I am working with: <ul id="linksList" data-role="listview" data-inset="true" data-filter="true"> <!-- Dynamic contents! --> </ul> This list pulls its data from a local XML file (RSS feed). I am looking f ...

Ways to prevent the jQuery simple slider from transitioning slides while it is in an invisible state

There is a jQuery slider on my website that behaves strangely when I navigate away from the Chrome browser and return later. It seems to speed through all pending slides quickly when it was not visible. Now, I want the slider to pause when it becomes invi ...

What is the best way to create a navbar with a grid that spans the full height and includes two rows

My approach to structuring the layout using CSS grid is as follows: nav content nav footer This means that the 'nav' will occupy the entire height of the page with a specified width, and the remaining space will be divided between the 'cont ...

What is the reason for Python Flask to consume both instances of a file when using .decode()?

In my program, I am fetching a CSV file from an HTML form and decoding it using utf-8. However, I need to have two instances of this file for different purposes. The issue arises when I use .decode('utf-8'), as both instances end up being consume ...

Creating Dynamic Web Design: Horizontal Scrolling Window and Adaptive HTML Content

After spending countless hours researching online, I am still unable to find a solution for responsive web design that maintains the aspect ratio of both the window and HTML body. Here are examples of what I'm looking for: original view, stretched le ...

Media Queries elude my complete comprehension

Can anyone provide a brief overview? For example, should buttons and images, as well as a footer, all be placed in one Media Query or kept separate? I'm feeling overwhelmed by this. ...

Using Sencha Touch: What is the best method for populating an HTML panel with a JSON response?

I've exhausted all possible combinations in my attempts to load a JSON object from the server and use it to render a panel with video and other information. Despite my efforts, I haven't been able to make anything work. What could I be doing inco ...

I am looking to bring in just the tables from a different html/php page

I am currently working on a webpage that includes a php library, header, and other elements. I only need to import specific tables and information from this page onto another screen display, with the tables arranged side by side instead of vertically. My ...

Dealing with ng-repeat and button alignment across Chrome, Edge, and Firefox

Can someone help me understand this strange behavior I am experiencing across all browsers? <div style="margin-top:5px"> <button translate="clear" ng-click="xyz.clear()" class="btn btn-default"></button> <butt ...

CSS styles are combined and included at the beginning of the index.html file

During the creation of a production version of my Angular 9.0.4 application, I noticed that the CSS is bundled and placed at the top of the dist/index.html file as shown below: <link rel="stylesheet" href="styles.6ea28d52542acb20a4c6.css"><!docty ...

Individuals have the capability to utilize .php as an image extension

It seems that someone is able to use any type of extension as long as they add .png at the end of the link. is currently being used, causing users on my website to log out when visiting the homepage. I tried to stop this issue, but unfortunately, my sol ...

"Discover the versatility of implementing a single ag grid across various components, all while dynamically adjusting its style

I am facing an issue where I have an angular grid ag-grid in component1, which is being used in some other component2. Now, I need to use the same component1 in component3 but with a different class for ag-grid, specifically 'ag-grid-theme-dark'. ...