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-

But my desired outcome should look more like this-

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

The presence of a white block as the background on a webpage in Ubuntu

Currently working on a website in Ubuntu 14.10. Encountering an issue with Chrome not rendering the background correctly. The footer displays fine, covering the entire width, but the content div background remains white. Check out the image for visual re ...

utilizing angularjs and bootstrap to manage multiple button models

Recently delved into learning angularjs and bootstrap. Found a tutorial on creating buttons. <div ng-controller="ButtonsCtrl"> <h4>Checkbox</h4> <pre>{{checkModel}}</pre> <div> <button type="butto ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

The hover effect fails to function properly after altering the background color. The default color setting is transparent

<button class="button-main slide">Slide Left</button> .button-main{ color: black; outline: none; background: transparent; border: none; letter-spacing: 0.0625em; padding: 8px 10px; text-transform: uppercase; font: b ...

Enable landscape mode exclusively for tablet and mobile devices, excluding desktop users

Looking to rotate content on tablet and mobile devices without affecting desktop view. Attempted solutions: rotate(90deg) -> Didn't work as it rotated even on desktop @media screen and (orientation:lanscape) -> Also didn't work as it rotat ...

Utilizing CSS styling to create page breaks in R Markdown disrupts the flow of table of contents

Encountering difficulties in achieving a visually pleasing document using knitr's export to pdf, I have resorted to converting the html file for my Markdown project to pdf through the wkhtmltopdf command line utility tool. A simplified Markdown docum ...

Populate HTML form with return values using jQuery AJAX

I am struggling with retrieving values from jQuery/AJAX and displaying them in an HTML form within the index.php file. This is what my index.php looks like: <script type="text/javascript"> $(document).ready(function () { $('#display'). ...

The Link Breaks the Overlay Hover Effect

Currently, the code functions as intended when you hover over or touch the thumbnail, an overlay will appear. The issue lies in the fact that to navigate to a specific URL, you have to click directly on the text. The overlay itself is not clickable and ca ...

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 ...

No display of Tailwind updates on local server in Safari browser using NextJS and a Mac M1

For my personal project with NextJS, I am using Tailwind CSS, but I am experiencing frequent issues with updating styles. It seems like there is a 50/50 chance that Tailwind will apply the styles to the component properly, and sometimes I have to go throug ...

What is the trick to getting the <label> and <input> elements to show up side by side in an HTML form?

I am designing a registration form for a new website. My goal is to have each label and its corresponding input element displayed on the same line. Here's the CSS code I'm using: #registration-form { background-color: #FFF; height: 600px; ...

What steps should I take to ensure my clock stays in sync with my runTime function?

I am developing a mini digital clock project with the ability to mimic a physical clock. The clock is activated by using a power button to switch it on and display the current time. It should also be able to turn off and show an empty screen. However, th ...

Skip the loading screen and directly retrieve meshes from the GWT server

I have a unique challenge with my animated models, each ranging from 5Mb to 8Mb in size. Downloading them individually can be time-consuming, especially when I need them on demand rather than preloading them in libGdx. While I am experienced with both GWT ...

The appearance of my website appears differently depending on the resolution

Just recently, I began exploring the world of HTML/CSS/JS and created a handy tool for my personal use. However, I encountered an issue when viewing it on different resolutions which caused some elements to look distorted. The tool I made allows me to inp ...

Move to Fieldset Upon Link Click

Here's an example of what I have implemented so far here It's evident that this is not fully functional due to the PHP and jQuery integration. This demo is just a showcase of my progress. I am looking to create a functionality where clicking on ...

Update Personal Information with PHP and MySQL

i have two files named edit.php and update.php. The content of edit.php is as follows: <?php session_start(); $_SESSION['id'] = '9'; $id = $_SESSION["id"]; $username = $_POST["username"]; $fname = $_POST["fname"]; $password = $_POST ...

What are the style attributes that can be edited in each ant design component?

My goal is to customize an ant design card by adding a border only on the left and right sides of the card. The bordered attribute in the card component seems to either remove the entire border or display it on all sides. I am looking for a way to specif ...

Dealing with CSS overflow issues in Safari, Chrome, and the ancient IE 6

Are Safari and Chrome compatible with overflow? How does IE 6 or higher handle overflow? ...

The method of document.getElementsByName does not provide any results

I am a beginner in using python flask and understanding the MVC concept. My goal is to display the selected option in a dropdown list. Below is my list defined in a .py function. @app.route('/', methods=['GET']) def dropdown2(): ...

What is the best way to align a jQuery Cycle 2 Slider in the middle of the

Having some trouble centering a jQuery Cycle 2 Slider on my page. You can see the slider here. I've attempted multiple methods to center it, but none have been successful. Check out the HTML code: <div class="slider"> <div id=outside> ...