Having trouble with vertical centering in Bootstrap 4 - need assistance

I tried to center text vertically on my webpage using a code snippet that I found, but it didn't work for me. Here is the HTML code I used:

<html lang="pl">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="./css/bootstrap.min.css">

    <title>Under Construction Page</title>
</head>
<body>
    <div class="row h-100">
        <div class="col-sm-12 my-auto">
            <h1 class="display-4">UNDER CONSTRUCTION</h1>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
</body>

Answer №1

For simple tasks like this, it's best to utilize Bootstrap 4 classes instead of relying on css hacks. Using css hacks often leads to more problems down the line, necessitating even more hacks to fix them.

A straightforward solution for vertical centering using Bootstrap 4 classes is to apply the align-items-center class to the row. If the content within the row is minimal, you may also need to set a height for the row. This can be done by adding style="height: 100vh", giving it 100% viewport height.

To horizontally center the text as well, use the text-center class on the column.

Below is the complete code snippet (click the "run code snippet" button and expand to full screen):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container-fluid">
    <div class="row align-items-center" style="height: 100vh">
        <div class="col-sm-12 text-center">
            <h1 class="display-4">UNDER CONSTRUCTION</h1>
        </div>
    </div>
</div>

Answer №2

Give This a Shot:

.col-sm-12.my-auto {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

.col-sm-12.my-auto {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="row h-100">
    <div class="col-sm-12 my-auto">
        <h1 class="display-4">UNDER CONSTRUCTION PAGE</h1>
    </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

What is the best way to calculate the total of multiple columns using JavaScript and jQuery?

I am looking to modify my table that has two different columns in order to calculate the sum of both. Currently, I can only add up one column using JavaScript, and duplicating the JS code with different value names for the second column is not ideal. How c ...

Carousel with Bootstrap: Heading positioned over the content

My goal is to have the caption of Bootstrap Carousel displayed above the content, but I'm encountering issues with it. When clicking on the chevrons, you may notice the < Item 1 > bouncing... (This behavior is a bug, and logically speaking, I ex ...

Struggling to center items on a page with Bootstrap 5

I'm attempting to center an item on the page using Bootstrap flex boxes in HTML. However, I am having trouble with the align-items-center attribute not working. <!DOCTYPE html> <head> <link href="https://cdn.jsdelivr.net/npm/& ...

Explore the contents of an HTML table with Swift 3

I have obtained the entire HTML code for a specific page on the Vermont Lottery's official website. This page contains a large table that I am interested in extracting data from. Upon successfully fetching the HTML content of the page using some code ...

Is there a way to adjust the transparency of individual words in text as you scroll down a page, similar to the effect on https://joincly

Is there a way to achieve a text filling effect on page scroll similar to the one found here: . The specific section reads: "Deepen customer relationships. Own the brand experience. Add high margin revenue. Manage it all in one place. Get back your pr ...

Make Jekyll process HTML files even without front matter by using the Knitr tool to add front matter to the HTML files

I am currently utilizing RMarkdown in combination with knitr to produce various HTML documents. After uploading these documents to GitHub, Jekyll is utilized for rendering the files onto GitHub pages. My objective is straightforward: I want the index.html ...

How do I create a Bootstrap 4 multi-level navigation bar that drops to the right on the third level?

clickable dropdown hover dropdown.jpg Source: Is there a way to create a dropdown menu that opens to the right like the clickable version, rather than to the left like the hover dropdown in the provided example? I'm open to other solutions as long ...

Experiencing problems with website loading due to jquery?

Recently, I've been experiencing slow loading times on my website . It's taking about a minute for the site to load properly, but strangely, if I click on the stop loading button, the site loads instantly. Does anyone have any insight into what ...

Transforming video files into HTML5 ogg/ogv and mpg4 formats

Is there a reliable software out there that can effectively convert video files (such as avi, flv, etc.) to HTML5 supported ogg/ogv and mpeg4 formats? I have tested a few options but unfortunately none of them seem to get the job done correctly. ...

"Enhance your date input with UI-Bootstrap's date picker

I recently implemented a date picker feature in my form. However, I am facing an issue where if an invalid date is entered, the error message displayed is always "Availability Date is required" instead of "Enter a valid date". Can anyone help me identify w ...

Angular Dynamic Form Troubles

Recently, I followed a tutorial from https://angular.io/guide/dynamic-form to create a dynamic angular form. So far, everything has been working smoothly. However, I am now facing a challenge as I try to implement a question type where users can add more i ...

Enhancing interactivity by incorporating GIFs upon clicking a JavaScript button

A JavaScript button has been created to generate Facts (gifs), with a desire to include a social media share option for platforms like Facebook below the GIF. The code snippet provided includes an array of GIF images and the functionality to display them u ...

When attempting to update a database, the Jquery ajax response is not being reflected

Currently, I am utilizing a dialog box for updating my database and removing services. However, I have encountered an issue where it only functions properly on the initial "remove" click. When I open the dialog box, I am presented with a list of 5 servic ...

What is the correct way to effectively refresh an included PHP file?

As a novice in the programming world, I've encountered yet another challenge that I hope you can assist me with. Here is the HTML code I'm currently working with: <div class='hold'><?php include 'image.php'; ?>< ...

CSS issue with highlighting menu items

Recently delving into the world of HTML and CSS, I encountered a tricky issue with my menu. The problem arises when hovering on an element - it should highlight by expanding padding and changing border color. However, for some reason, when I hover on one ...

Escaping multiple levels of quotations in a string within HTML documents

Currently, I am developing an application with Java as the backend and AngularJS 1.0 for the frontend. To display data tables, I am utilizing the veasy AngularJS plugin which I have customized extensively for my app. However, I am facing a small issue. I ...

Retrieve a specific column value upon button click in HTML and JavaScript

I am faced with a table containing multiple columns, each row equipped with an edit button. https://i.sstatic.net/3S5VP.png Upon clicking the edit button, a modal view pops up where users can input values that are then sent via AJAX to my controller. ht ...

Do elements containing {visibility:hidden} properties exist within the HTML DOM structure?

Do HTML elements that have the css property visibility:hidden still exist within the DOM tree? ...

Restrict the amount of pages shown on the bootstrap navigation bar

I am currently using this code to display a range of pages, but all pages are being displayed. I would like to limit the display to pages 1 through 10 only, with 100 results per page. For example, if you select page 2, it would display page 11 and so on. ...

Steps for changing the background color string in mui?

I have a component where I am trying to dynamically change the color based on the user type. The issue arises when I use gradient colors, as the transition stops working. If I stick to simple colors like blue, red, or green, it works fine. Currently, the c ...