Centering a form on a webpage with Bootstrap: A step-by-step guide

Utilizing twitter-bootstrap for styling, I have implemented a login form and am attempting to center it on the page. Most suggestions on stackoverflow advise placing elements inside the container class, which is what I have done.
The requirement is for the login form to have a background color and be centered on the page. To achieve this, I have created a CSS class called .login-dialog and applied it to a div wrapping the form.

However, I am encountering some issues:
- The entire row is being filled with the background color
- The form and its controls are not aligning at the center
- There is excess background space on the right side
(please refer to the image below)

I could resolve this using a table, but I want to avoid any fixed-width solutions. Can someone provide assistance with this?

    <div class="container page-login">
        <div class="login-control-padding">
            <div class="row ng-hide" ng-show="model.errorMessage">
                <div class="col-md-12 col-sm-12">
                    <div class="alert alert-danger ng-binding">
                        <strong>Error:</strong>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <span class="clientName ng-binding">Local Host Client</span>
                </div>
            </div>
            <div class="row">
                <div ng-show="model.loginUrl">
                    <div class="login-dialog">
                        <form name="form" class="form-horizontal ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength" role="form" action="/identity/login?signin=1111" method="post">
                            <input name="idsrv.xsrf" class="ng-isolate-scope" type="hidden" value="22222" token="model.antiForgery">
                            <div class="form-group">
                                <label class="control-label col-md-1" for="username">Username</label>
                                <div class="col-md-3">
                                    <input name="username" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" id="username" autofocus="" required="" type="text" maxlength="100" placeholder="Username" ng-model="model.username">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-1" for="password">Password</label>
                                <div class="col-md-3">
                                    <input name="password" class="form-control ng-pristine ng-untouched ng-isolate-scope ng-invalid ng-invalid-required ng-valid-maxlength" id="password" required="" type="password" maxlength="100" placeholder="Password" ng-model="model.password" focus-if="model.username" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group" ng-show="model.allowRememberMe">
                                <label class="control-label col-md-1" for="rememberMe"></label>
                                <div class="col-md-3">
                                    <input name="rememberMe" class="ng-pristine ng-untouched ng-valid" id="rememberMe" type="checkbox" value="true" ng-model="model.rememberMe">
                                    <span>Remember Me</span>
                                    <button class="btn btn-default pull-right">Login</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

CSS class

.login-dialog{
   background-color:rgb(40, 147, 194);
   border:solid;
   border-width:1px;
   border-radius:5px;
   border-color:rgb(28, 104, 137);    
   color:white;
   padding-top:10px;
  }


  .login-control-padding {
    padding-top: 30px;
  }

Output

https://i.sstatic.net/OWtcb.png

Answer №1

It seems that the issue lies in the fact that the .login-dialog is a block element, causing the blue color to span the full width.

To address this, modify the form control by adding the classes .col-md-1 and .col-md-3, making the elements span a total width of 4 out of 12 grid spaces, leaving 8 grids unused.

To center the form element, simply add the classes col-md-4 col-md-offset-4 to login-dialog. Additionally, change all instances of col-md-1 in labels to col-md-4, and col-md-3 in form controls to col-md-8.

If you are still unsure, refer to this link for clarification:

I hope this explanation helps resolve your issue.

Thank you!

Answer №2

-The background color is applied to the entire row

The reason for this is that the .login-dialog div does not have a specific bootstrap col class assigned, causing it to expand to the width of its parent container, the .row.

-The form and its controls are not centered properly

To align them better, you should use an offset class like col-sm-offset-3 to move the form towards the center.

-There is excessive background on the right side

This issue should be resolved once you adjust the form width and ensure proper center alignment.

Take a look at this quick snippet:

.login-dialog{
   background-color:rgb(40, 147, 194);
   border:solid;
   border-width:1px;
   border-radius:5px;
   border-color:rgb(28, 104, 137);    
   color:white;
   padding-top:10px;
  }


  .login-control-padding {
    padding-top: 30px;
  }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container page-login">
        <div class="login-control-padding">
            <div class="row ng-hide" ng-show="model.errorMessage">
                <div class="col-md-12 col-sm-12">
                    <div class="alert alert-danger ng-binding">
                        <strong>Error:</strong>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 col-sm-6">
                    <span class="clientName ng-binding">Local Host Client</span>
                </div>
            </div>
            <div class="row">
                <div ng-show="model.loginUrl">
                    <div class="login-dialog col-sm-6 col-sm-offset-3">
                        <form name="form" class="form-horizontal ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength row" role="form" action="/identity/login?signin=1111" method="post">
                            <input name="idsrv.xsrf" class="ng-isolate-scope" type="hidden" value="22222" token="model.antiForgery">
                            <div class="form-group">
                                <label class="control-label col-md-1" for="username">Username</label>
                                <div class="col-md-3">
                                    <input name="username" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" id="username" autofocus="" required="" type="text" maxlength="100" placeholder="Username" ng-model="model.username">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-1" for="password">Password</label>
                                <div class="col-md-3">
                                    <input name="password" class="form-control ng-pristine ng-untouched ng-isolate-scope ng-invalid ng-invalid-required ng-valid-maxlength" id="password" required="" type="password" maxlength="100" placeholder="Password" ng-model="model.password" focus-if="model.username" autocomplete="off">
                                </div>
                            </div>
                            <div class="form-group" ng-show="model.allowRememberMe">
                                <label class="control-label col-md-1" for="rememberMe"></label>
                                <div class="col-md-3">
                                    <input name="rememberMe" class="ng-pristine ng-untouched ng-valid" id="rememberMe" type="checkbox" value="true" ng-model="model.rememberMe">
                                    <span>Remember Me</span>
                                    <button class="btn btn-default pull-right">Login</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </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 attempted to update the text on an HTML page using the content of "conetnt", however, it was unsuccessful

.custom-div { outline: none !important; width: 450px; margin: auto; background-color: #ccc !important; text-indent: -9999px;} .custom-div::before{content: 'sample';color: black;} ...

Utilizing the child element's attribute value in a list to dynamically modify the CSS styling of a div element

I'm struggling with designing a ranking bar that consists of 9 bars. I want the height of the bars to vary, with bar 0 and bar 8 being the longest. However, I am having difficulty manipulating my jQuery selector to change the CSS style of the bars bas ...

problems with hovering over radio buttons in Internet Explorer 9

Encountering a curious issue in IE9: When hovering over my top level wrapper div, the first radio button seems to be triggered as though it's being hovered over. This means that even if the last radio input is selected, clicking anywhere within the wr ...

iOS: Incorrect element is currently being scrolled by the user

I encountered an unusual scrolling problem on iOS (7 or 8) while browsing www.cahri.com/tests/scroll How can you replicate this issue? Visit the example page on your iPhone/iPad/iOS Simulator in landscape mode Use your right thumb to touch and scroll th ...

Tips for reducing the JavaScript file size outputted by a liquid template generator

Currently, I am working on enhancing the performance of my Shopify website and GoogleSpeed Insights is suggesting that I minify css and js files. However, the recommended files are all created by the liquid template generator, making it challenging to uti ...

How can you align two div elements side by side, if the first div is spanning across two lines?

I'm looking to position two divs next to each other, with the second div starting right where the first one ends. I came across a similar question on Stack Overflow, but unfortunately, it didn't have the answer I needed. <div id="wrapper"> ...

The hover effect is implemented across all image elements

Below is the code snippet in question: #mg1 { margin-left: 3%; } #mg1:hover { margin-top: 4%; } <div id="section1"> <center> <div id="bgp"> <center> <p>THUMBNAILS</p> </center> ...

Activating the CSS :active selector for elements other than anchor tags

How can I activate the :active state for non-anchor elements using JavaScript (jQuery)? After reading through Section 5.11.3 of the W3C CSS2 specification in relation to the :hover pseudo selector in hopes of triggering the activation of an element, I stu ...

Tips for maintaining the size of a div container

Take a look at the following code snippet: <head> <title></title> <style> .section_divs { background-color: lightblue; float: left; } #section_empty { background-color: tomato; width ...

Using jQuery to make an element follow you as you scroll down a page inside a div

I've made some progress on my code: HTML <div id="header"></div> <div id="content"> <div class="sidebar-wrapper"></div> </div> <div class="session-wrapper"></div> <div id="footer"></div> ...

Expand the <div> by clicking on it, then hover away to return it to its normal size

One interesting feature I have on my website is a <div> that expands when clicked, and returns to normal size with another click. However, I am looking for something a bit different... What I want is for the <div> (with the class name .topHead ...

The background appears only in the upper portion of the screen upon refreshing the table

Whenever the user clicks on the edit button or when the page is initially loading, a backdrop appears and disappears once the modal is displayed. The backdrop effectively highlights the entire page. However, after updating my table with data via Ajax witho ...

CSS page breaks function properly in Internet Explorer 8, however, they are not functioning correctly in Firefox and Google Chrome

Help Needed: I encountered an issue where I wrote CSS to display page breaks in my HTML. It seems to work fine in Internet Explorer, but not in Firefox and Chrome. Here is the inline CSS I used: <br style="page-break-after: always;"> Below is a sni ...

Creating a custom styled Drawer with flexbox styling using ReactJS, MUIv5, and the styled-engine-sc library

Hello community, I am currently working on implementing a custom styled drawer within a flexbox container using the styled-engine-sc. I started with a template and tried to convert it into styled-components. Here is the source: https://codesandbox.io/s/vm ...

What is the best way to set the value of the days in a month to a div

I've been experimenting with creating a calendar using javascript, HTML, and CSS on my own. I managed to achieve some functionality like obtaining the current month and year as well as the previous month and year by clicking a button in HTML. However, ...

Gradually shifting alignment of Bootstrap tooltips

I am encountering an issue with Bootstrap tooltips They seem to be progressively off-center Below is the code I am using for the icons {{#each guilds}} <div class="col-sm-1"> <a href="/dash/{{this.id}}"> <div class="gicon"> ...

Reducing the slide margins in impress.js?

When using Impress.js, I noticed that the default slides have a large left margin (or padding - it's hard to determine with Firefox's Inspector). I have a slide with a wide <pre> block that would fit if it were aligned to the left, but it d ...

Bootstrap Button Problem: Difficulty arranging buttons in a neat way, unable to position them next to each other

I'm currently working on a real estate website project and have designed a Photoshop template which is uploaded on Behance. Check it out here. Right now, I'm creating the static version of the real estate store template and facing an issue with ...

Body section CSS selector

Can a CSS selector be included within the body section of an HTML document? Here's an example of my code (although it is not functioning as expected): <html> <head> </head> <body> <div style= "a[target=_blank] {backgroun ...

What is the best way to implement a CSS transition in React when the state changes, the component remounts, or it re-rend

Imagine having a React functional component with some basic state: import React, { useState } from 'react' import { makeStyles } from "@material-ui/core" export default function Cart() { const [itemNumber, setItemNumber] = useState ...