Position a Bootstrap 3 division within a central division for ultimate centering

I have written a code snippet that looks like this-

html, body, .container-table {
  height: calc(100% - 50px);
}

/*Centering a div*/

.container-table {
  display: table;
}
.vertical-center-row {
  display: table-cell;
  vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>


<div class="container container-table">
  <!-- Main div to hold all Data -->

  <!-- Login Form -->
  <div class="row vertical-center-row">

    <h2 class="text-center">Login</h2>
    <form class="form-horizontal" action="login.html">
      <div class="form-group">
        <label for="inputEmail" class="control-label col-md-2">Email</label>
        <div class="col-md-4">
          <input name="email" type="email" class="form-control" placeholder="Email" required>
        </div>
      </div>
      <div class="form-group">
        <label for="inputPassword" class="text-center control-label col-md-2">Password</label>
        <div class="col-md-4">
          <input name="password" type="password" class="form-control" placeholder="Password" required>
        </div>
      </div>
      <div class="form-group">
        <div class="col-md-offset-2 col-md-4">
          <div class="checkbox">
            <label>
              <input type="checkbox">Remember me</label>
          </div>
        </div>
      </div>
      <button name="remember_me" type="submit" class="center-block btn btn-primary">Sign in</button>
    </form>
  </div>
</div>

Currently, the output I am getting looks like this-

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

But my desired outcome should look more like this-

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

If anyone could lend some assistance, it would be greatly appreciated.


Response-

This specific query-

Centering Login Form Bootstrap

Does not entirely solve my issue, as if you were to implement the provided codes and understand the requirements, you will see why this is the case.

Additionally- My challenge does not revolve around centering just any div, but rather centering a div within another div that has been horizontally and vertically centered already.

Answer №1

To ensure your form is properly aligned, add the class col-md-offset-3 to the first element of a row.

Bootstrap tables consist of 12 columns in total. If your form's width is set to 6 cells, you will have 6 cells remaining. To center your form with 3 cells on each side, push it by 3 cells.

html,
body,
.container-table {
  height: calc(100% - 50px);
}
/*Creating a centered div*/

.container-table {
  display: table;
}
.vertical-center-row {
  display: table-cell;
  vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>


<div class="container container-table">
  <!-- Main div to hold all Data -->

  <!-- Login Form -->
  <div class="row vertical-center-row">

    <h2 class="text-center">Login</h2>
    <form class="form-horizontal" action="login.html">
      <div class="form-group">
        <label for="inputEmail" class="control-label col-md-2 col-md-offset-3">Email</label>
        <div class="col-md-4">
          <input name="email" type="email" class="form-control" placeholder="Email" required>
        </div>
      </div>
      <div class="form-group">
        <label for="inputPassword" class="text-center control-label col-md-2 col-md-offset-3">Password</label>
        <div class="col-md-4">
          <input name="password" type="password" class="form-control" placeholder="Password" required>
        </div>
      </div>
      <div class="form-group">
        <div class="col-md-offset-5 col-md-4">
          <div class="checkbox">
            <label>
              <input type="checkbox">Remember me</label>
          </div>
        </div>
      </div>
      <button name="remember_me" type="submit" class="center-block btn btn-primary">Sign in</button>
    </form>
  </div>
</div>

Answer №2

test

.boxed-container
{
    display: table;
    margin: 0 auto; 
}

Answer №3

Why not utilize bootstrap columns for a seamless layout?

<div class="container row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
       Insert your login form HTML here...
    </div>
    <div class="col-md-4"></div>
</div>

Answer №4

Here's a suggestion to improve your layout:

Enclose your button within a div container like this:

<div class="center">
<button name="login_btn" type="submit" class="btn btn-primary center-block">Log in</button>
</div>

Then, apply the style text-align:center to the new div element.

Answer №5

html,
body,
.container-table {
  height: calc(100% - 50px);
}
body{top:50%;}

/*Centering a div*/

.container-table {
  display: table;
}
.vertical-center-row {
  display: table-cell;
  vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<div class="container container-table">
  <!-- Main div to contain all Data -->

  <!-- Login Form -->
  <div class="row vertical-center-row">

    <div class="col-md-10 col-md-offset-1">
    <h2 class="text-center">Login</h2>
    </div>
    <div class="col-md-10 col-md-offset-1">
      <form action="login.html" class="form-horizontal">
      <div class="form-group">
        <label class="control-label col-md-2" for="inputEmail">Email</label>
        <div class="col-md-8">
          <input type="email" required="" placeholder="Email" class="form-control" name="email">
        </div>
      </div>
      <div class="form-group">
        <label class="text-center control-label col-md-2" for="inputPassword">Password</label>
        <div class="col-md-8">
          <input type="password" required="" placeholder="Password" class="form-control" name="password">
        </div>
      </div>
      <div class="form-group">
        <div class="col-md-offset-2 col-md-8">
          <div class="checkbox">
            <label>
              <input type="checkbox">Remember me</label>
          </div>
        </div>
      </div>
      <div>
      <div class="col-md-10 col-md-offset-1">
        <button class="center-block btn btn-primary" type="submit" name="remember_me">Sign in</button>
    </div>
      
      </div>
    </form>
    </div>
  </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

I am unable to see the text field when I use element.text. What could be the reason for this

As I work on collecting online reviews using Selenium Chrome Webdriver, I've encountered a challenge with TripAdvisor. The reviews are truncated and require clicking a "More" button to view the complete text. Within the html code, there is a class cal ...

Node.js is great at hosting HTML files, but struggles when it comes to loading script files within those pages

Recently, I’ve been working on creating a simple login page using a mongoDB database along with Node.js and express. Since I'm still new to Node.js, I'm facing an issue that seems more complicated than it actually is. The main problem I’m en ...

What is the process for connecting CSS to an HTML page?

In my project, I have an index.html file and a style.css file. Currently, I am using inline CSS in my HTML file but now I want to utilize an external stylesheet. Inside my index.html file, just before the closing </head> tag, I have added the followi ...

The CSS Grid container fails to resize based on the content inside

I am facing an issue with the grid element inside a parent container with display: grid;. The parent container has a maximum size and allows scrolling for the grid if necessary. The problem is that the grid element does not expand to fit its children. Whe ...

Navigating through pages without using Javascript

While working on creating page numbers, I stumbled upon this intriguing CodePen: https://codepen.io/p-ziegler/pen/bmdaaN .b, input[type="radio"] { display: inline-block; width: 16px; height: 16px; vertical-align: middle; } .b { back ...

Refreshing a specific area on a webpage through the use of Ajax technology

I need to update a specific part of my webpage when a user clicks on the 'clear' button. I currently have some code that I borrowed from another answer I found on this site: $('.clear').click(function () { $.ajax({ url: "", ...

enhancing the appearance of the initial sentence in the closing passage through CSS styling

Is there a way to make only the first sentence in the final paragraph bold using CSS? Specifically, I would like to add extra padding to the top of this last paragraph so that it wraps around an image. However, when I increase the bottom padding of the flo ...

Why does my element appear to be a different color than expected?

I've developed a sleek wind map (check out for reference). Using a weighted interpolation every 10ms, I generate uVector and vVector data to calculate wind speed. Based on this speed, each point is assigned a specific color as outlined below. if (we ...

"jQuery Ajax function running smoothly on my local machine, but encountering issues when deployed on the server

I have explored extensively across the web in search of a solution to this issue. I possess a jquery ajax script that loads a csv file and creates html code by utilizing the data from the csv file. <script type="text/javascript"> $(document). ...

What could be the reason for the container div's height not being properly refreshed?

When adding elements to a container div with an initial height of 'auto', I assumed that the height would adjust based on the children elements added. However, this is not happening. Can anyone assist me in ensuring that the container div's ...

choose the initial element within a class

Seeking a way to target the first element within each group of the same class? Consider the following scenario: <div class="group"> <div class="element"></div> <div class="element"></div> <div class="element"&g ...

When an absolute positioned element exceeds the right border of its relative parent, its width will be truncated

When position: absolute; divs are placed inside or around a position: relative; parent, their height and width are based on the size and length of the text they contain. However, if one of these divs extends beyond the right border of the relative parent, ...

Confusing focus styling in React Bootstrap on mobile devices

THE ISSUE: Facing a problem in my project where on mobile, when a button is selected and then unselected, it remains dark due to focus which can be confusing. To see the issue in action, check out this screen recording: Screen recording You can view the ...

Is there a JavaScript toolkit specifically designed for animating mobile devices?

Currently in search of a JavaScript Library that is similar to impress.js, allowing me to create dynamic animations on text content that are compatible with iOS/Android. Although Hype by Tumult seems promising, I am not interested in an editor but rather ...

What is the best way to extract and analyze CSS code from an HTML page using Java programming language

I'm looking to extract and parse the content of HTML pages using the Jsoup API, which can be found at . However, I've encountered a challenge in extracting CSS that is located in a different folder. How can this be achieved? Currently, my code ...

Organize your blog content by utilizing post IDs as the designated id attribute

What is the best way to format blog posts and comments in HTML so that I can easily manipulate them later using jQuery/Javascript for tasks like updating, deleting, or making Ajax calls? I am considering using the IDs (primary keys in the database) of the ...

I'm encountering an issue with implementing a basic "remember me" feature on my PHP login page

I am in the process of implementing a basic "remember me" feature in my login.php file: <?php session_start(); if(isset($_SESSION["comp"])){ header("location view.php");//to redirect to the view page if already logged retur ...

Mobile devices are failing to display the logo as expected

Can anyone help me figure out why the logo is not displaying on mobile devices? I've already tried resetting my phone's network and web browser, as well as reducing the size of the logo from 100px to 80px. Despite these efforts, the logo still wo ...

Styling tip: Distance the tooltip from its parent element when scrolling through a list

I have a table inside li tag, and I want to display the description on :hover inside td. However, when scrolling through the list, the description content starts to appear with a distance from its parent. Despite trying various combinations of position se ...

Adjusting the style attributes for Material-UI Icons

I could use some assistance with changing the CSS property of MUI icons in order to assign a specific color when the icon is focused. I have implemented the icons in the header and am displaying different pages below them, so I would like to give each ac ...