How to horizontally center align a div using Bootstrap

Having difficulty center-aligning the fields in a simple login page sample. The h:commandButton is aligned to center, but the other fields remain unchanged with the <div align="center">.

How can I ensure that all fields are centered?

<div align="center">            
    <h:message for="btnSubmit"/>
    <div class='form-row'>
        <div class='col-xs-2 form-group required'>
         <label class='control-label'> User Name </label>
          <h:inputText styleClass="form-control" size="12" maxlength="20" 
                       id="userName" value="#{loginController.userName}" 
                       required="true"/>
        </div>
    </div>

    <div class='form-row'>
        <div class='col-xs-2 form-group card required'>
           <label class='control-label'>Password</label>
           <h:inputText styleClass="form-control" size="12" maxlength="20"
                        id="password" value="#{loginController.password}"
                        required="true" />
         </div>
    </div>

    <div class='form-row'>
        div class='col-md-12 form-group'>
      <h:commandButton styleClass="btn btn-success" 
               value="#{bundle['label.Login']}" id="btnSubmit" 
               action="#{loginController.clientLoginAction}"/>
        </div>
    </div>
</div>

Answer №1

If you want to easily center a div, one simple method is to create and add a custom class called .col-center yourself.

.col-center{
  margin:0 auto;
}

Why use this approach? Bootstrap has the offset methodology, but it only works for even column sizes like .col-X-2, .col-X-4, col-X-6, col-X-8, and col-X-10. By using .col-center, you can bypass this limitation.

Remember: Ensure that the div you want to center has a specified width property assigned to it. The .col-center class will take care of the rest for you.

Answer №2

It was decided in October 2019 that the "center-block" class would be removed from Bootstrap, and instead the "mx-auto" class was introduced to serve the same purpose in the latest version.

<div class="mx-auto bg-info" style="width:200px">Centered Element</div>

Answer №3

To horizontally center a block level element in Bootstrap, you can utilize the center-block class.

Here is an example:

<div class="center-block">...</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

Customizing Bootstrap icon with padding, background color, and radius settings

Struggling with Bootstrap Icons and Bootstrap 5, I attempted to create a perfect circle background behind an icon by setting padding, background color, and a rounded circle radius. The code I used was <i class="bi-globe-americas text-white p-3 bg-p ...

PHP: Invoking a function within the HTML content of a variable

I need help with adding a PHP function to a variable storing HTML code for a form. This is what the current setup looks like: <?php function example_function() { // code } $form = ' <form action" <!-- etc. --> > <input <!-- etc ...

Issue with Angular CDK table: The element 'td' cannot be placed inside of the element 'table'

In my Angular 7 project in Visual Studio 2017, I am attempting to implement the Angular CDK table. While following the official code sample provided here, everything functions perfectly. However, Visual Studio alerts me with warnings stating: Element &apos ...

jquery ajax request not functioning properly on Firefox browser

This query is following up on this inquiry and this one. I am experiencing difficulties sending form field values through the jquery ajax API. Here is the code snippet: index.html <!DOCTYPE html> <html> <head> <meta content="tex ...

Harnessing PHP-grown JSON to retrieve information using JQuery

After successfully using Alert on the response parameter in jQuery, I can see the values I need. However, the issue arises when trying to extract them using key/value pairs. I'm not sure if this is a compatibility problem with the JSON format from PHP ...

Is there a way to update an image dynamically within CSS using the :before pseudo-element?

I am attempting to dynamically change the background image, but I am having trouble accessing the wrapper before "wrapper:before{}" from jQuery $('.wrapper:before) .wrapper:before { content: ""; position: fixed; left: 0; right: 0; z-index: -1; displa ...

There was an issue encountered while attempting to utilize orgchart: The property '_aZ' of null could not be read

I've been exploring the use of an organization chart library from Everything goes smoothly with a few nodes, but when I attempt to load the entire organization (approximately 1100 subjects with multiple nested levels), I encounter this console error: ...

Keep the conversation flowing with a continuous chat update

I'm struggling with keeping my chat constantly refreshed. Here's the setup I have: All messages are stored in a MySQL database connected through PHP. Messages are loaded and posted using PHP, with jQuery handling the submission to the chatbox u ...

Trigger javascript code upon the clicking of a hyperlink

I need to trigger a JavaScript function after clicking on a specific link on my webpage. I came across this code snippet: <a href="#" id="myHref">Click me</a> $("#myHref").on('click', function() { document.getElementById(".myDiv").s ...

Two background videos exhibit distinct behaviors when placed within the identical <video> tag

I have noticed a strange behavior with two videos I uploaded online. One, called "Bunny video," appears on top of the navigation bar while the other, named "Space video," is positioned below with an unknown black background (that I did not intentionally se ...

Hover effects can be applied to CSS tabs

There seems to be a CSS issue on Chrome where the background image is not hiding when clicking on next tabs. I have used the following code: .paymentBank ::after { content: " "; } You can view the live code [here][1] and I have also attached a scree ...

What is the best way to ensure a multi-page form is validated using HTML5 and JavaScript before progressing to the next page?

Currently, there are two functionalities at play. The submit button is not being recognized while the functionality in JS $(".next").click(function(){..} is working as expected. HTML This section involves 4 fieldsets. I am attempting to validate ...

Challenges encountered when using Jasmine's spyOn with toHaveBeenCalled on a method within a prototype

When attempting to spy on a method, my test is failing with the message "Expected spy handle_click to have been called," even though I can see in the console log that "Foo handle_click called!" is being logged. Here is the code snippet from Foo.js: funct ...

Submitting a form using jQuery.serialize over POST method and anticipating a JSON reply from the controller

I'm looking for help on how to send an AJAX request with a serialized form using jQuery and receive a JSON response from the controller. I've tried various solutions without success, as my experience in this area is limited. Could you provide a ...

What could be the reason behind the function's failure to return values? Instead of returning the expected results, it's displaying [object object]

I'm currently delving into the world of JQuery and finding myself puzzled as to why certain functions are not producing any output. Check out the script below: function Person(name,country){ this.name = name; this.country = country; } var ...

Does the content='text/html; charset=gb2312' html meta tag attribute impact the GET Query String?

Here's the question: When making a standard HTTP Request to a server (non-ajax), does the Query String passed by the GET method to the server get affected by the encoding specified in this meta tag: <meta http-equiv='Content-Type' conte ...

Create a stunning border image with a touch of transformation

Dealing with a button dilemma here. The client wants the border to also be the image, which is creating quite the challenge for me. Changing the box is no problem, but the tricky effect on the next button is preventing me from making the necessary adjustme ...

Is it possible for me to utilize Bootstrap CSS directly from a CDN instead of including the SASS file in my Node project?

Just a quick inquiry to kick things off. I'm currently developing a node application and considering using SCSS for styling. My question is, if I include the Bootstrap CSS file from a CDN in my index.html, will I be unable to override the variables? ...

What is the method for displaying a canvas scene within a designated div element?

I need help displaying a scene inside an existing div on my webpage. Whenever I try to do this, the canvas is always added at the top of the page. I attempted to use getElementById but unfortunately, it didn't work as expected. What could I be overlo ...

Using jQuery to dynamically create and select HTML elements

Is there a way to access the dynamically created HTML element by its ID in the code below? var line = $('<div class="showInGrid" id="removeMeLater"> <span class="dateP" id="calendar' + ind + '" ></span> ...