How to center a div both vertically and horizontally within a flex-fill using Bootstrap 4

Hello there! I am currently using Bootstrap 4 and the layout I have implemented seems to be working fine as all my pages start from the top. However, now I am trying to center the elements.

<style>
    body,
    wrapper {
        min-height: 100vh;
    }

</style>

<body>

    <wrapper class="d-flex flex-column">
        <nav>
            
        </nav>
        <main class="flex-fill">
            <div class="container-fluid">
                THIS IS THE DIV I'M CURRENTLY TRYING TO CENTER
            </div>
        </main>
        <footer>
            
        </footer>
    </wrapper>



</body>

I attempted to change the position using 'top', but that didn't work. What did work was changing the position using 'margin-top' with a specific value in vh, however, this method is not responsive on different screen sizes. I would greatly appreciate any help in finding a responsive solution to vertically centering this div. So far, I've successfully centered the div horizontally using 'row justify-content-center', but vertical centering has proven to be more challenging. Thank you!

Answer №1

It seems like this question has already been addressed here.

To center the content of the main section, you can set it as a d-flex container and then use align-items-center.

<wrapper class="d-flex flex-column min-vh-100">
    <nav>
        nav
    </nav>
    <main class="flex-fill d-flex align-items-center">
        <div class="container-fluid text-center"> THIS IS THE DIV IM ATTEMPTING TO CENTER </div>
    </main>
    <footer>
        footer
    </footer>
</wrapper>

Please note that the default direction of d-flex is flex-direction:row, so using align-items:center will vertically align. To vertically center in flex-direction:column, you would need to use justify-content:center

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

Upon clicking the checkbox, only the ul tag will be displayed on the screen

It's puzzling to me that when I click on the checkbox, only the ul tag with id=menu is visible on the screen, not id=social. I need to ensure display:block is set for every ul tag. .show-menu { display:block; } #menu{ float:left; } #social{ ...

Having trouble with an AJAX request to display a template in Flask

Imagine having two radio buttons labeled 1 and 2, along with some text at the bottom of a webpage. Initially, the text value is set to -1 and no radio buttons are selected. If one of the radio buttons is clicked, the text value should change to either 1 or ...

AngularJS form submission with Ajax requiring a double click to successfully submit the form

I need to perform the following activities on my HTML page: User inputs email and password for registration Form is sent to Controller when user clicks Submit Controller uses AJAX to create JSON request to RESTful Server and receives response from server ...

Displaying React components using Bootstrap in the navigation bar

I'm feeling a little stuck here. I'm currently working with React Bootstrap and the Navigation component. One of the routes in my application is the dashboard, which is only accessible after logging in. When a user navigates to the dashboard, I ...

Display or Conceal Multiple Divisions Using Angular 2

I am looking to implement a functionality using Li lists to toggle the visibility of multiple divs in Angular 2. Initially, all divs on the page will be visible. When viewing on a smaller screen, I want to hide some divs and show a specific div when a cor ...

Is there a way to make placeholder text automatically adjust its size to fit within the text input element and display its complete content?

I am working on a piece of HTML code that includes a form with a table for traveler information. However, I am having trouble ensuring that the placeholder text remains fully visible at all times. To give you a visual example, here is what I currently hav ...

Developing filters for a PHP and MySQL database table

Creating a filter tab for my table is the current task at hand. When clicked, the filter tab will present different options to filter the data from the table and display a new set of results. Each dropdown selection will then filter the table results accor ...

What is the method for compressing text in CSS?

I'm curious about how to condense my text so it doesn't extend beyond the page. Any ideas on how to achieve this? Issue: Solution: .Subheading{ text-align:center; font-family: 'Mukta', sans-serif; margin-top:70px; m ...

Formatting Text in CSS and HTML

I need help aligning three images horizontally with text underneath, using CSS. Despite trying multiple methods, the images remain vertically aligned and the text does not align properly with the images. #img_cont { display: table; width: 90%; ...

How can we use JavaScript to retrieve an element with custom styling?

I've encountered a strange issue with my script where it seems to stack up borders and display a 2px border instead of the intended 1px border when switching elements on click. Here is the link code I am using: <li ><a href="plumbing.php"&g ...

Customizing the arrow of a Bootstrap tooltip and adjusting its positioning

I've implemented a bootstrap tooltip to show an image in it. So far, I've been successful in achieving this. https://i.sstatic.net/hgzyL.png However, my current requirement is as follows: https://i.sstatic.net/xcxWZ.png I would like to adjust ...

Different ways to manipulate the CSS display property with JavaScript

Having trouble changing the CSS display property to "none" using JavaScript. Check out my code: <div class="mydiv" onClick="clickDiv1()">hello</div> <div class="mydiv" onClick="clickDiv2()">hi</div> <div class="mydiv" onClick=" ...

Use PHP and HTML to create a table that will allow for running specific MySql queries from each cell of the table

I tried my best with the title, but feel free to suggest any edits. Overview: Coming from Android development, I have experience with RecyclerView to display a grid of items from a database. I could easily handle item clicks using onClickListener() and P ...

Issues with the disappearance of elements when using the Toggle() function

I've created a toggle feature for displaying a sub menu (.li-one) when clicking on the main menu (.ul-one). The issue arises when you click on another menu item, as the previous one does not disappear unless you click outside of the .main-nav div. Is ...

CSS - Adjusting width based on height ratio

I'm in the process of developing a website and could use some guidance. I have a specific section where I am aiming for the height to be 79.6% of the body, with the width of that div being dependent on its height. Thus far, my research has only yielde ...

Modify the date format inside the content of an HTML element or its descendants (excluding their attributes)

I have been trying to reformat some dates using JavaScript. My initial approach was: var regex = /(\d{4})-(\d{2})-(\d{2})/g; $('.container td').each(function() { $(this).html($(this).html().replace(regex, '$3-$2-$1')); ...

Dynamic pop-up content based on AJAX response

I don't have much experience in web development, so please excuse any beginner mistakes;) Currently, I'm working on creating a dynamic popup window in HTML, which is hidden by CSS. When a button is clicked, an AJAX post request is triggered to r ...

Issues with Google fonts not displaying properly on WordPress site

I'm just starting out with Wordpress and I'm using the html5blank theme to bring in my stylesheets. However, I've run into an issue where the google fonts are not being applied on my wordpress site. In my header.php file, I have included th ...

The webpage does not dynamically resize based on screen height

Encountered an issue with the excess space below the footer. Resolved it by implementing a jQuery sticky footer as CSS techniques were unsuccessful. However, facing a problem with the main content of the webpage. Adjustment of .bg-photo's height impa ...

presentation not transitioning smoothly as the next slide appears

Initially, my aim is to center the slideshow on the page but I am uncertain about how to achieve it. This is how my current webpage appears: https://i.sstatic.net/E6bzQ.png Furthermore, as shown in the image, the sliding effect of the blue slide is movi ...