Steps to align a div's content to the center of the screen using Bootstrap 4

Using Bootstrap 4, I have been trying to center the container content on my screen. Despite setting the height to 100% and using justify-content-center align-items-center, the content remains stuck at the top of the page.

Image:

https://i.sstatic.net/RZIk7.jpg

<!doctype html>
<html lang="en" class="h-100">

<head>
  ...
</head>

<body class="h-100">

  <header>
    ...
  </header>

  <main class="h-100">

    <div class="container h-100">

      <div class="row justify-content-center align-items-center">
        <div class="col">

          <!-- Title -->
          <div class="row text-center">
            <div id="title" class="w-100 mt-5">
              <h1>Let's Find You a Great Apartment.</h1>
            </div>
          </div>

          <!-- Subtitle -->
          <div class="row text-center">
            <div id="subtitle" class="w-100">
              <h2><small>Join more than 5,828,476 happy renters searching 1,025,792 local apartments.</small></h2>
            </div>
          </div>

          <!-- Search box -->
          <div class="row w-100 align-items-center">
            <div id="search" class="enterALocationSearchBox mt-2">
              <input type="text" class="w-100" placeholder="Enter a Location here">
            </div>
          </div>
        </div>
      </div>

    </div>

  </main>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous" />

</body>

</html>

Answer №1

No need for bootstrap in this case.

To center a child div within its parent, you can utilize position: relative for the parent and

position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
for the child. Just be sure to set the correct height and width values for the parent.

.parent {
    position: relative;
}
.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

For more information on CSS centering techniques, check out this resource.


If you still prefer using bootstrap, remember to specify the height of the parent div to ensure vertical centering works correctly.

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

Discover a foolproof method for effortlessly examining an flv or mp4 file embedded within a webpage simply by

I've encountered a challenge with JavaScript. I can successfully check a flash object in a webpage when hovering over it, but I'm unsure how to achieve the same for flv or mp4 objects when either hovering over or moving away from them. Currently ...

I require the ability to access a reference parameter with a dynamically changing name within a Vue template

<div v-for="x in 4" :class="(images[x] === null) ? 'not-removable' : 'removable'" class="img-container"> <input style="display: none" type="file" ...

What is the best way to obtain the file's pathway?

:image => StorageRoom::Image.new_with_filename(path) When trying to retrieve the image path, I initially specified it manually and it worked fine. However, after deploying to Heroku, an error message 'Load Error - No such file present' is dis ...

The ion-range slider does not function properly when trying to display a central label

After customizing the ion range slider to suit my needs, I am looking for some simple functionalities: 1. The label should be centered. 2. When clicking on the label, the handle should move accordingly. 3. There should be a color gap between two valu ...

Styling a select drop-down menu with CSS to show a button in Chrome and Webkit browsers

On my HTML page, I have a basic drop down menu set up like this: <select name="menu" class="menu"> <option value="a">A</option> <option value="b">B</option> <option value="c">C</option> </select> ...

How can I expand and collapse all the Bootstrap panels simultaneously?

Is it possible to make all three panels collapse and expand simultaneously with just one button? <button data-toggle="collapse" data-target="#collapseOne-1,#collapseTwo-1,#collapseThree-1" ...></button> <div id="#collapseOne-1" class="coll ...

Struggling to properly position a checkbox within an Angular Material project

<div class="checkbox-password"> <div> <mat-checkbox class="example-margin">Remember me</mat-checkbox> </div> <div> <a routerLink="/forgotPassword" class="createAccount" ...

What is the best method to remove the content of a div without using innerHTML = "";?

My dilemma lies in a div that is populated with dynamically created DOM elements via JS. As I strive for the div to refresh each time the JS function runs again, various sources have advised against utilizing document.getElementById('elName').in ...

"Generate a series of dropdown menus with choices using either jQuery or AngularJS from a JSON dataset

I'm in need of assistance. I want to generate select dropdowns dynamically based on data retrieved from my REST API, which is in JSON format. How can I dynamically inject these selects into my HTML? Below is an example data structure: JSON data fetch ...

How jQuery stops the submission of a form

Sample HTML Code <p><select name="status" class="form-control" id="showThisItem"> <option value=""> Select Status </option> <option value="Paid"> Paid </option> <option value="Unpa ...

Guide on ensuring a THREE JS model maintains constant orientation towards cursor pointer

I'm working on a 3D model that currently follows the mouse only when clicked. However, I am looking to make it move without requiring the click so that it always faces the cursor pointer. The project is built using THREE.JS. Is there a way to achieve ...

What is the method for setting up vertical tabs?

Is it possible for someone to provide guidance on how to create vertical tabs for the HTML content below? When a tab, such as tab1 in the left column, is clicked, only the content of tab1 should be displayed in the middle column. The same applies for tab2 ...

What is the best way to ensure that the image appears directly below the text, rather than being obscured by the content of the

Hey there! I'm pretty new to StackOverflow and currently diving into fullstack web development. Just the other day, I embarked on creating my very own website, but now I find myself in a bit of a pickle. It seems that my image keeps appearing behind t ...

Set the background image to always cover the entire page

My goal: When the site has a widescreen aspect ratio, I want the background image to scale to 100% horizontally, cropping the top and bottom. This can be achieved easily using background-size: cover; However, if the site has a vertical aspect ratio (such ...

What would you name this particular element?

I want to create a unique slider design but I'm unsure where to begin or how to search for information because I don't know the correct terminology. Does this type of slider have a specific name? I heard about Marquee, but that seems like someth ...

Transmit a JSON object while simultaneously receiving the corresponding response

As part of a school project, I need to work with JSON. The server will process the file and send back a response in JSON format. This entire exchange needs to be done using JavaScript. I recently learned about the XMLHttpRequest object, but I'm still ...

The HTMLAgilityPack for .Net is causing a tag attribute to be populated with empty quotes

I am currently utilizing the HMLAgilityPack to make changes to an html file. Within my template, there is this snippet of html: <model-viewer class="viewer" id="viewer1" ar ar-modes="scene-viewer quick-look" ar-scale=" ...

Unable to successfully send multiple values from input fields using forms in PHP

I am currently facing an issue with my HTML form that contains a field consisting of multiple selection boxes as shown below: <div class="form-group"> <select multiple name="Fee[]"> ...

Checking if multiple elements possess the same class using jquery: How is it done?

Can this selector be minified? if ($("#element1").hasClass("highlight") && $("#element2").hasClass("highlight") && $("#element3").hasClass("highlight") && $("#element4").hasClass("highlight") && $("#element5").hasClass("hig ...

JavaScript enables Partial Views to load: Potential XSS vulnerability identified by HP Fortify

Fortify has identified a XSS vulnerability in my JavaScript function. I need help finding a solution since this method is heavily used in my app. I am attempting to use ajax to call a partial view and then append the resulting HTML to a specific DOM div e ...