Troubleshooting the issue with form centering in Bootstrap

I've been attempting to center my form within a div, but the techniques I've applied are not yielding the desired result. I followed the bootstrap documentation and used the justify-content-center attribute, but it isn't aligning the form properly. When I try using display: flex and justify-content: center in my CSS on the container wrapping the form, the layout gets disrupted. I'm struggling to figure out where I went wrong in centering the form. Any assistance or insights would be greatly appreciated. Thank you in advance.

.onlineRefilFormCon {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  width: 100%;
  height: 90%;
}

.onlineRefillTitleCon {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0%;
  left: 0%;
  width: 100%;
  height: 10%;
}

.onlineRefillForm {
  position: absolute;
  top: 20%;
  width: 40%;
  height: 80%;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form-group {
  padding-top: 5%;
}

.col-form-label {
  font-weight: 600;
}

.form-check-input {
  border: 2px solid;
}

.form-control {
  width: 60%;
  border: 2px solid;
}

.textarea {
  margin-top: -10%;
  margin-left: 17%;
  border: 2px solid;
}

.radio1 {
  margin-top: 3%;
  margin-left: 20%;
}

.form-check-inline {
  padding: 0%;
}

.OnlineSubmitBtnCon {
  position: absolute;
  bottom: 7%;
  width: 100%;
  height: 10%;
}

.OnlineSubmitBtnCon button {
  margin-left: 35%;
}
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<div class="onlineRefilFormCon">
  <div class="onlineRefillTitleCon">
    <h2 class="onlineRefillTitle">Online Refill</h2>
  </div>
  <form class="onlineRefillForm" id="onlineRefillForm">
    <div class="form-group row justify-content-center">
      <label for="text-input" class="col-2 col-form-label">First Name</label>
      <div class="col-10">
        <input class="form-control" type="text" value="" id="text-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="text-input" class="col-2 col-form-label">Last Name</label>
      <div class="col-10">
        <input class="form-control" type="text" value="" id="text-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="date-input" class="col-2 col-form-label">Date</label>
      <div class="col-10">
        <input class="form-control" type="date" value="yyyy-mm-dd" id="date-input">
      </div>
    </div>
    <div class="form-group row justify-content-center">
      <label for="email-input" class="col-2 col-form-label">Email</label>
      <div class="col-10">
        <input class="form-control" type="email" value="" id="email-input">
      </div>
    </div>
    <div class="form-check-inline radio1">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios1"
                        value="option1" checked>
                    Pickup
                </label>
    </div>
    <div class="form-check-inline">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
                        value="option2">
                    Delivery
                </label>
    </div>
    <div class="form-check-inline">
      <label class="form-check-label">
                    <input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
                        value="option2">
                    Mail
                </label>
    </div>
    <div class="form-group justify-content-center">
      <label for="textarea" class="col-2 col-form-label">Refill Details</label>
      <textarea class="form-control textarea" rows="7" placeholder="Example: Lisinopril"></textarea>
    </div>
    <div class="OnlineSubmitBtnCon">
      <button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">Submit</button>
    </div>
  </form>
</div>

Answer №1

To center align the form elements, you can apply the text-align:center style. Additionally, consider adding the text-center bootstrap class to the elements you wish to have centered alignment.

Has this method proven effective for you?

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

Answer №2

Is there a possibility that the issue lies in the broken link to the bootstrap styles? I encountered a failed URL when testing the example code, so I replaced it with a new CDN link which resulted in the form appearing centered.

.onlineRefilFormCon {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  font-weight: 600;
}

.onlineRefillForm {
  display: flex;
  flex-direction: column;
  width: 40%;
  padding: 48px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}

.form-group {
  padding-top: 5%;
}

.form-check-input {
  border: 2px solid;
}

.form-control {
  width: 60%;
  border: 2px solid;
}

.textarea {
  margin-top: -10%;
  margin-left: 17%;
  border: 2px solid;
}

.radio1 {
  margin-top: 3%;
  margin-left: 20%;
}

.form-check-inline {
  padding: 0%;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2909d9d868186809382b2c7dcc2dcc3">[email protected]</a>/dist/js/bootstrap.min.js"></script>
<div class="onlineRefilFormCon">
  <form class="onlineRefillForm" id="onlineRefillForm">
    <label for="text-input">First Name </label>
    <input type="text" value="" id="text-input" />
    <br>
    <label for="text-input">Last Name</label>
    <input type="text" value="" id="text-input" />
    <br>
    <label for="date-input">Date</label>
    <input type="date" value="yyyy-mm-dd" id="date-input" />
    <br>
    <label for="email-input">Email</label>
    <input type="email" value="" id="email-input" />
    <br>
    <label>Pickup
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked /></label>
    <br>
    <label>Delivery
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
    <br>
    <label>Mail
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
    <br>
    <label for="textarea" class="col-2 col-form-label">Refill Details</label>
    <textarea rows="7" placeholder="Example: Lisinopril"></textarea>
    <br>
    <button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">
      Submit
    </button>
  </form>
</div>

Answer №3

Implementing the formatting

margin-left:auto;margin-right:auto;

This will conveniently centralize content moving forward

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

"Rearranging the Firefox ghost image by dragging and dropping it

My drag and drop game is working perfectly on all browsers except for Firefox. In Firefox, the ghost image that appears when an item is dragged seems to be positioned very far away from the cursor. Although the ghost image can still be seen on the page, it ...

I need the links to open up the content area on the website in HTML

Is it possible to have a fixed header that does not disappear when clicking links from the navigation bar? I tried dividing the page into tables to achieve this. Now, I want to open the links in the navigation bar in the right column (the main content col ...

Determine if the content within the YouTube iFrame has been updated

I currently have an iframe on my webpage that does not initially display a video. However, when the user clicks a button, a Youtube video will be loaded into the iframe. I am trying to detect when the video has finished loading by using the on() method t ...

Vanilla Javascript's alternative to $(document).on

Can someone provide me with the plain vanilla JavaScript code equivalent to the jQuery code below? $(document).on("mousemove touchmove", function(e) { console.log(e.touches[0].pageX); //code }); I understand how to implement this in ...

Fixed bottom navigation bar in Bootstrap 4

Here is my current navbar: <nav class="navbar navbar-inverse"> <div class="container"> <ul class="nav navbar-nav"> <li><a href="">page 1</a></li> <li> ...

Ensure that the background image is perfectly fitted within the second background image

I am facing a challenge with two images - a frame and another image that needs to fit perfectly inside that frame. The tricky part is ensuring that the layout remains responsive. The two images are: https://i.sstatic.net/XWsTq.png Here is how it should ...

Is it beneficial to incorporate AJAX functionality into my PHP classes and scripts?

I recently started working on a PHP/AJAX website and I'm contemplating the best approach to "only include the content". Should I incorporate if statements in my header and footer scripts to check for a "ContentOnly" get parameter? Then, append that p ...

What is the best way to gradually show the contents of the second div starting from the bottom?

I want to create a scrolling effect where the contents of the second div are revealed as if they were fixed on the first div, similar to Conichi View example in JSFiddle .one { width: 768px; height: 700px; color: white; background-color: black; ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

Allow editing for only a specific part of a text box

Creating a customized personal URL page for users on my site is important to me. I envision the value of a text box being "example.com/username," with the "example.com/" part displayed in the text box, but not editable. I've noticed that Tumblr accom ...

Populate a database with information collected from a dynamic form submission

I recently created a dynamic form where users can add multiple fields as needed. However, I'm facing a challenge when it comes to saving this data into the database. You can view a similar code snippet for my form here. <form id="addFields" me ...

Error loading console due to JSON data on website

After creating a Json data file named question.json with the following content: "Endocrinology":[ { "title":"Endocrinology", "id": "001", "date":"08J", "question":"In adult men, anterior pituitary insufficiency does not caus ...

What is the best way to align the Storefront Menu in the center?

If you want to reproduce the issue I'm facing, visit the demo page of the storefront here: then remove the node <ul id="site-header-cart" class="site-header-cart menu"></ul> that contains the cart box (located next to the menu) so that on ...

The animation triggered by scrolling is not functioning on this element

I'm currently attempting to add a scroll animation to my section1 element, but for some reason, it's not working. This is puzzling to me because I've successfully implemented the same scroll code on another element without any issues. The on ...

iOS application becomes unresponsive when attempting to load a video within a webview, but does not

I developed an iPad app using a 3rd party tool called OpenPlug which converts AS3 to C++ and then exports to iOS. This app displays pictures and videos in a slideshow. The videos are shown using a WebView loading an HTML page with the video source set to a ...

Adjust the size of CSS sprite pixel art without blurring the image

I am attempting to enlarge CSS sprite pixel art images and need to completely eliminate anti-aliasing/image smoothing. After reading this post on the subject: Disable antialising when scaling images, I experimented with the following CSS: image-rendering ...

What options are available for labels in Bootstrap v5.0.0-beta1 compared to BS 3/4?

How can I use labels in Bootstrap v5.0.0-beta1? In Bootstrap 3, the labels are implemented like this: <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" /> <span class="label label-success ...

PHP do-while loop triggers out of memory error while running SQL query code

While running the code below, the website crashes with the error: "Allowed memory size of 268435456 bytes exhausted (tried to allocate 254643 bytes)" $queID = "LCGQ00" . $ID; $sqlquecheck = "Select QueryID FROM Questions Where QueryID = & ...

Animation in CSS Appears Jumpy in Chrome

For some reason, the text inside a moving div (which is transitioning using CSS) seems to jitter and shake in Chrome. However, this issue does not occur in Firefox, where the movement is very smooth and achieves the desired effect. #hidexpert { display: b ...

Apply rounded corners to the table row

Currently, I am utilizing a datagrid to display information. I have been attempting to implement border radius on all the table rows, but it doesn't seem to be working. Does anyone have insight into how I can apply border-radius to all rows in the t ...