Struggling to align a vertical form horizontally in Bootstrap 4?

This code snippet shows the form I am working with:

<div id="site" class="container-fluid">
<div class="row">
    <div class="my-4 offset-3 col-6 justify-content-center align-items-center">
        <form action="/some/path" method="post">
            <div class="form-group row">
                <label for="username" class="col-2 col-form-label">Username</label>
                <div class="col-4">
                    <input type="text" class="form-control" id="username" name="_username" required="required" autocomplete="username" />
                </div>
            </div>

            <div class="form-group row">
                <label for="password" class="col-2 col-form-label">Password</label>
                <div class="col-4">
                    <input type="password" class="form-control" id="password" name="_password" required="required" autocomplete="current-password" />
                </div>
            </div>

            <div class="form-group row">
                <div class="col-2"></div>
                <div class="col-4">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" id="remember_me" name="_remember_me" value="on" />
                        <label for="remember_me" class="form-check-label">Remember me</label>
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <div class="col-6">
                    <button class="btn btn-burnt-orange" type="submit" id="_submit" name="_submit">Log in</button>
                </div>
            </div>
        </form>
    </div>
</div>
</div>

I've been trying to center this form on the page using Bootstrap, but currently it is not centered as expected, as seen in this screenshot:

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

I initially calculated the grid columns correctly, with a 3-column offset and 6-column content area, anticipating the form to center due to the combined 6-column width of form rows. However, the result is not as intended.

How can I correct this and ensure the form is both centered and 6 columns wide as desired?

Answer №1

The alignment of the form has been adjusted to be horizontally centered, however, it appears to only occupy half of its parent's width. This is due to...

"By setting the form rows to a total of 6-columns (2-column labels and 4-column inputs), the form will fill the 6-column content area, automatically centering itself."

With the number 6 representing half of 12, the form col-6 (2+4) will only take up half of the parent col-6. To achieve a narrower form in the center, you can utilize col-4 (1/3 width) and pair it with elements that add up to 12 for the labels and form input (e.g., 3+9). Alternatively, you can use other combinations like 4/8, 6/6, etc...

<div class="container-fluid">
    <div class="row">
        <div class="my-4 offset-4 col-4 justify-content-center align-items-center">
            <form action="/some/path" method="post">
                <div class="form-group row">
                    <label for="username" class="col-3 col-form-label">Username</label>
                    <div class="col-9">
                        <input type="text" class="form-control" id="username" name="_username" required="required" autocomplete="username">
                    </div>
                </div>

                <div class="form-group row">
                    <label for="password" class="col-3 col-form-label">Password</label>
                    <div class="col-9">
                        <input type="password" class="form-control" id="password" name="_password" required="required" autocomplete="current-password">
                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-3"></div>
                    <div class="col-9">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="remember_me" name="_remember_me" value="on">
                            <label for="remember_me" class="form-check-label">Remember me</label>
                        </div>
                    </div>
                </div>

                <div class="form-group row">
                    <div class="col-12">
                        <button class="btn btn-burnt-orange" type="submit" id="_submit" name="_submit">Log in</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


Related: Center the content inside a column in Bootstrap 4

Answer №2

To achieve this, you can utilize the col-6 class and justify-content-center in Bootstrap. Simply structure your elements, rows, and columns with Bootstrap to center the form group. Here is a sample code snippet for centering the form group:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="site" class="container-fluid">
  <div class="row justify-content-center">
    <div class="col-6" style="background: #eee;">
      <!-- formgroup start -->
      <form>
        <div action="/some/path" class="form-group">
          <label for="username">Username</label>
          <input type="text" class="form-control" id="username" placeholder="Enter username" name="_username" required="required" autocomplete="username" />
        </div>
        <div class="form-group">
          <label for="password">Password</label>
          <input type="password" class="form-control" id="password" placeholder="Password" name="_password" required="required" autocomplete="current-password" />
        </div>
        <div class="form-group form-check">
          <input type="checkbox" class="form-check-input" id="remember_me" name="_remember_me" value="on" />
          <label class="form-check-label" for="remember_me">Remember me</label>
        </div>
        <button type="submit" class="btn btn-primary" id="_submit" name="_submit">Submit</button>
      </form>
      <!-- formgroup end -->
    </div>
  </div>
</div>

For further information, refer to the following pages in the Bootstrap 4 documentation:

  1. https://getbootstrap.com/docs/4.1/layout/grid/
  2. https://getbootstrap.com/docs/4.1/components/forms/

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

"Help needed: The HTML dialog element is obstructing the invisible reCAPTCHA challenge popup. Any solutions for resolving this

Visit this link to access the example in incognito mode. Click on the "open" button to open the sample signin form, then click on the "Sign Up" button to trigger the challenge. There seems to be an issue with using recaptcha inside a dialog. I'm not ...

Performing a dynamic animation by toggling the class of an element with "classList.toggle" in JavaScript

I have been attempting to incorporate a fade animation using CSS when altering the class of the input and label elements within a form by utilizing the classList.toggle method in JavaScript. I'm puzzled as to why my code isn't producing the desir ...

What are the best methods for utilizing scrollbars to navigate a virtual canvas?

I am interested in developing a unique jQuery plugin that can simulate a virtual HTML5 Canvas, where the canvas is not physically larger than its appearance on the page. However, the content intended for display on the canvas may be much larger and will ne ...

Toggling a div updates the content of a different div

I am looking to make the content within <div class="tcw-content"> dynamically change when a different div is clicked. I have little experience with Ajax, so I'm wondering if there are alternative ways to accomplish this using JS/CSS. If anyone h ...

How can I selectively import the "reboot.scss" file from bootstrap using npm in my project?

For a simple playground project, I've set up my environment with just Bootstrap (installed with npm install bootstrap) and the SCSS compiler package (installed with node install node-sass). At this point, all I need is the code from reboot.scss. I wa ...

Tips for sending php data to javascript efficiently

I'm wondering about the best way to retrieve HTML table rows and display the values on another page upon clicking a button. Ideally, I would like to store these variables in a session for future use. Any advice on how to proceed? ...

Guide on breaking up a string into separate lines

Can someone help me with separating the strings in this line "Add Problem Report \n Add Maintenance Report \n Add Expenses Report \n Add Contract", into new lines? I have tried using br, pre, and \n but it does not seem to work. HTML ...

Arranging an image next to a text input in Bootstrap 5: A step-by-step guide

I want to align a magnifying glass image next to a text input on the left side. However, it is currently displaying above the text input. How can I make sure they are positioned side-by-side using Bootstrap 5? <link rel="stylesheet" href="https://c ...

Stylized search input inspired by Pinterest with a bubbly design

When it comes to my search bar, I want the user's entered keywords to be displayed within a bubble that has a delete option when they press space to add another keyword. This functionality is similar to what Pinterest does with their search bar, as de ...

How can I position an element at the bottom of a page using VueJS and Bootstrap CSS?

https://i.sstatic.net/ojanG.png I am trying to move my footer (Copyright statement) to the bottom of the page. However, it is creating some extra white space beneath the footer. How can I eliminate this additional white space? This issue is occurring in ...

Playing only audio and no video in an Android WebView using HTML5

I am currently facing a challenge with a webpage that includes an html5 video and css3 animations. While the web page functions perfectly on an android device's browser, I am experiencing laggy, glitchy, and jumpy animations when using hardware accele ...

What is the best way to apply a top padding to a background image using CSS?

I attempted to adjust the top padding using various pixel values in the style, but the image padding relative to the webpage remained unchanged. For example: padding-top: 5px; Here is part of my code snippet: <div class="row pb-5 mt-4" style ...

Is it possible to obtain the index of a table row using querySelector?

Is it possible to find the rowIndex of the first occurrence of a table row within the #myTable using JavaScript? http://jsfiddle.net/bobbyrne01/fmj6np6m/1/ html .. <table id="otherTable"></table> <table id="myTable"></table> js ...

Steps for accessing an image from the static folder in a Python-Django application

I am currently working on a portfolio website project where I am using html, css, js, and django. However, I am facing an issue with loading an image from the assets folder within the static directory. Here is a snippet of the template code causing the pr ...

Tips for inserting an HTML tag within an object

How can I format a list from an array of objects in React? The 'answer' key consists of the text... Question: blah, Answer: `List title The top advantages of Kin are: 1. 2. 3. 4. ` I want to create so ...

What steps should I take to effectively configure the header cache on my website?

After navigating through various links and attempting different solutions such as adding <meta HTTP-EQUIV="EXPIRES" CONTENT="Thu, 31 Dec 2015 12:00:00 GMT"> to my header without success, I am struggling to find a comprehensive guide on how and wher ...

Guide to setting up collapsible sections within a parent collapsible

I came across this animated collapsible code that I'm using: https://www.w3schools.com/howto/howto_js_collapsible.asp Here is the HTML: <button type="button" class="collapsible">Open Collapsible</button> <div class="content"> &l ...

Struggling to remove the translucent effect when hovering over the share button

I have been struggling with a seemingly simple issue for a few hours now. On my website, I have some share buttons located at . Although I want these buttons to remain completely black when hovered over, they are inexplicably becoming slightly transparen ...

Adjusting jQuery Button Width with input type of "button"

Currently, I am working with jQuery Mobile to develop a straightforward Golf Score Card App. To calculate the total at the end of a form submission, I've implemented the following code. <input id="total" type="text" name="total" value="" readonly= ...

"Revealing the specific row and column of the selected element in each table when hovering over it

On my webpage, I have set up two tables positioned side by side like this: <!DOCTYPE html> <table> <tr> <td> <table> <tr><td>1</td><td>2& ...