Center the form on the page using Bootstrap

My bootstrap login form is currently appearing at the top of the page, but I want it to be centered.

<div class="container">
  <div class="row text-center">
    <div class="col-sm-6 col-sm-offset-3 well offset4">
      <div class="">
        <div class="text-warning">
          <h3>Login to our site</h3>
          <p>Enter your username and password to log in</p>
        </div>
      </div>
      <div class="">
        <form role="form" action="" method="post" class="">
          <div class="form-group">
            <label class="sr-only" for="form-username">Username</label>
            <input type="text" name="form-username" placeholder="Username..." class="form-control" id="form-username">
          </div>
          <div class="form-group">
            <label class="sr-only" for="form-password">Password</label>
            <input type="password" name="form-password" placeholder="Password..." class="form-control" id="form-password">
          </div>

          <button type="submit" class="btn btn-warning col-sm-12 mt-2"><strong>Sign in!</strong></button>
        </form>
      </div>
    </div>
  </div>
</div>

I'm looking for a way to center this form on the page using Bootstrap or AngularJS. Any suggestions would be appreciated!

Answer №1

Vertical centering is not natively supported in Bootstrap by default.

If you want to center elements vertically on a login page, you can use this absolute positioning technique:

.perfect-centering {
     position: absolute;
     top: 50%;
     left: 50%;
     transform: translate(-50%, -50%);
   }

It's possible that vertical centering functionality will be included as a mixin in Bootstrap 4 in the future. But for now, additional styles are needed.

Answer №2

To enhance the layout, I included an additional div outside your existing container:

<div class="center">    
<div class="container">
                <div class="row">
                    <div class="col-sm-6 col-sm-offset-3 well offset4">
                        <div class="">
                            <div class="text-warning">
                                <h3>Login to our site</h3>
                                <p>Enter your username and password to log in</p>
                            </div>
                        </div>
                        <div class="">
                            <form role="form" action="" method="post" class="">
                                <div class="form-group">
                                    <label class="sr-only" for="form-username">Username</label>
                                    <input type="text" name="form-username" placeholder="Username..." class="form-control" id="form-username">
                                </div>
                                <div class="form-group">
                                    <label class="sr-only" for="form-password">Password</label>
                                    <input type="password" name="form-password" placeholder="Password..." class="form-control" id="form-password">
                                </div>
                                <button type="submit" class="btn btn-warning col-sm-12"><strong>Sign in!</strong></button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
</div>

Furthermore, a new CSS class was introduced as follows:

.center{
  position:absolute;
  top:50%;
  left:50%;
  padding:10px;
  -ms-transform: translateX(-50%) translateY(-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);
  } 

UPDATE: If you have your Bootstrap file locally, simply integrate this class without creating additional files. Alternatively, if you are linking to Bootstrap externally, you can apply the style directly within a div like so:

<div style=" position:absolute;
  top:50%;
  left:50%;
  padding:10px;
  -ms-transform: translateX(-50%) translateY(-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);">....</div>

You can use this inline styling instead of the center class.

Answer №3

Here is a creative approach that avoids using absolute positioning and instead utilizes view-height units to maintain responsive width for the form.

CSS

/* body {height:100vh;} */ /* uncomment if necessary */
.full-height {height:100vh;background-color:yellow;} /* occupying 100% of the view-height */
.form-height {margin-top:-130px;} /* offsetting by half the form height */
.form-v-middle {margin-top:50vh;} /* aligning at center vertically based on view-height */

HTML

<div class="container full-height">
  <div class="row form-height">
    <div class="col-sm-6 col-sm-offset-3 well offset4 form-v-middle">
      <div class="">
        <div class="text-warning">
          <h3>Sign in to access our site</h3>
          <p>Provide your username and password to log in</p>
        </div>
      </div>
      <div class="">
        <form role="form" action="" method="post" class="">
          <div class="form-group">
            <label class="sr-only" for="form-username">Username</label>
            <input type="text" name="form-username" placeholder="Enter your Username..." class="form-control" id="form-username">
          </div>
          <div class="form-group">
            <label class="sr-only" for="form-password">Password</label>
            <input type="password" name="form-password" placeholder="Enter your Password..." class="form-control" id="form-password">
          </div>
          <button type="submit" class="btn btn-warning col-sm-12"><strong>Log In</strong></button>
        </form>
      </div>
    </div>
  </div>
</div>

Live Demo: View Demo

For information on browser support for view-height units, visit: Browser Support for vh Unit

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

Modify the colors of <a> elements with JavaScript

I am brand new to the world of UI design. I am encountering an issue with a static HTML page. (Please note that we are not utilizing any JavaScript frameworks in my project. Please provide assistance using pure JavaScript code) What I would like to achie ...

Opacity in text shadow effect using CSS

Within my text lies the following css snippet: .text-description-header { font-size: 250%; color: white; text-shadow: 0 1px 0 black; } Observe the dark shadow effect it possesses. Inquiry I am curious if there is a way to alter the shadow t ...

creating a vertical scroll bar for an HTML table's content

How can I set a fixed height for my HTML table on a webpage so that if it exceeds the height, a vertical scrolling bar is automatically displayed? Any assistance would be greatly appreciated. Thank you. ...

Is it possible to retrieve resolved parameters from a template-rendering function?

I'm facing a scenario where I have a state setup like this .state('dropdown', { url: '/dropdown', views: { "@":{ template: ...

Exploring the world of using Bootstrap containers, rows, and columns

I have a good grasp on containers and columns, but I am a bit confused about rows. The example below shows a 50/50 split. <div class="container"> <div class="row"> <div class="col-md-6"> 50 / 50 Content </ ...

Counting the number of visible 'li' elements on a search list: A guide

In the following code snippet, I am attempting to create a simple search functionality. The goal is to count the visible 'li' elements in a list and display the total in a div called "totalClasses." Additionally, when the user searches for a spec ...

IBM Watson Conversation and Angular Integration

After recently diving into Angular, I am eager to incorporate a Watson conversation bot into my angular module. Unfortunately, I'm facing an issue with including a library in Angular. To retrieve the Watson answer, I am utilizing botkit-middleware-wat ...

tips for obtaining the highest value among multiple keys within an array

How can I find the maximum value among multiple keys in an array? I previously attempted to find the maximum values for just three keys. getMaxValuefromkeys(values: any[], key1: string, key2: string, key3: string) { var val1 = Math.max.apply(Math, v ...

How to Customize the Menu Style in Your WordPress Theme

As a newcomer to Wordpress theme development, I am struggling to properly style my navigation menu. Below is the raw HTML code I have: <!-- Navigation --> <nav class="navbar navbar-default navbar-custom navbar-fixed-top"> <div cl ...

Utilizing ES6 syntax, the afterCellEdit event in gridApi is triggered when

Working with es6, angular 1.5, and ui.grid. When setting up the afterCellEdit event on the grid api, what should I use instead of $scope? export default class MyController{ // other codes skipped registerGrdApi(gridApi) { t ...

Change the color of this element and input field background

Having trouble with setting the background color of an input field to red in this code: $(document).ready(function(){ $(".abc").focus(function(){ $(this).attr('background', 'red'); $("label").text('Insert tex ...

Using a directive to insert HTML content from a separate file into a div element

As a newcomer to angularjs, I am experimenting with appending html from another file using my directive. The goal is to only do this if there is a successful response from the server after an http request. The current approach involves defining my directi ...

Incorporating the use of font color in CSS styles and

I am creating a newsletter template using foundation. Within the table, there are 2 <th> elements containing the content "Reservationnumber" and "23425-FF3234". I have multiple instances of these <th>. I want to apply a colored font to them. Wh ...

ECONFLICT: No compatible version of angular-bootstrap was found during the installation of datetimepicker

I encountered an issue with ECONFLICT while attempting to install a datetime picker in angularjs. The error message reads: "Unable to find suitable version for angular-bootstrap." My bower file specifies angular.js 1.2.16, but it seems that the datetime p ...

Utilizing Multiple Carousels on a Single Bootstrap 5 Page

I am facing a challenge in making multiple Bootstrap carousels work correctly on a single page. According to Bootstrap guidelines, each carousel should have a unique ID. However, I am struggling to implement this and only one carousel functions properly ...

Incorporating a scrolling text box within an <aside> element set in a flex layout

Just trying to make sure the title is clear, as this happens to be my initial post in this space. Lately, I've been venturing back into the creation of websites and currently looking to incorporate a "concert log" below a set of GIFs on my website&apo ...

A guide on setting option values using ng:Options

It seems that AngularJS has trouble constructing a proper list of <option> elements using a simple template in Internet Explorer. As a workaround, the ng:Options directive is provided (refer to https://github.com/angular/angular.js/issues/235). Upon ...

Struggling with overlaying Bootstrap modals on top of each other

This idea is inspired by the topic Multiple modals overlay I am working on developing a folder modal that allows users to either 1) open an existing file or 2) create a new file within each folder modal. To see how it functions, please run the code below ...

Ensure that the image inside a resizable container maintains the correct aspect ratio

I often encounter a situation where I need to showcase a series of thumbnails/images that adhere to a specific aspect ratio within a flexible container. My current approach involves using a transparent, relatively positioned image to enforce the correct as ...

Angular's minimum date validation is not accurate for dates prior to the year 1901

Any assistance or clarification on this matter would be greatly appreciated. It appears that there may be an issue with my implementation, as otherwise it seems like a significant bug within Angular. Setup Create a form with a minimum date of 0001-01-01 ...