How to set the button alignment in Bootstrap ?
Bootstrap buttons are no different from any other DOM elements of an HTML document. Aligning them is more likely the same as aligning paragraphs, divs and sections. Here are some of the scenarios that one can encounter.
- Buttons in ‘container’ class:
- .text-left
- .text-center
- .text-right
Buttons in ‘container’ class can be aligned with the ‘text-align’ properties. Wrap the group of buttons inside a div and align them using the following classes,
Syntax:
class="text-left"|"text-center"|"text-right"
Note: You can also use HTML5 <center> tag to align buttons to the center.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" <title>Aligning Buttons</title> </head> <body> <h1 style="color:green;text-align:center;">GeeksforGeeks</h1> <div class="container"> <div class="text-left"> <button type="button" >Button 1</button> <button type="button" >Button 2</button> </div> <div class="text-center"> <button type="button" >Button 1</button> <button type="button" >Button 2</button> </div> <div class="text-right"> <button type="button" >Button 1</button> <button type="button" >Button 2</button> </div> <center> <button type="button" >Button 1</button> <button type="button" >Button 2</button> </center> </div> </script> <script src= </script> </body> </html> |
Output:

- Buttons inside FlexBox:
- .d-flex .flex-row
- .d-flex .flex-column
Another way of arranging buttons is by using flex-utilities. The basic flex classes provided by Bootstrap are
The following pattern can be achieved by choosing either one of them.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" <title>Aligning Buttons</title> <style type="text/css"> html, body { height: 200px; } </style> </head> <body> <h1 style="color:green;text-align:center;">GeeksforGeeks</h1> <div class="container h-100"> <div class="d-flex h-100"> <div class="align-self-start mr-auto"> <button type="button" class="btn btn-danger"> Click Me! </button> </div> <div class="align-self-center mx-auto"> <button type="button" class="btn btn-primary"> Click Me! </button> </div> <div class="align-self-end ml-auto"> <button type="button" class="btn btn-success"> Click Me! </button> </div> </div> </div> </script> <script src= </script> </body> </html> |
Output:

- Relative-Absolute method:
- Vertical alignment in Bootstrap with Examples
- BootStrap | Text Utilities (Alignment, Wrapping, Weight etc.)
- Bootstrap 4 | Button Groups
- How to place button in top Right corner using bootstrap?
- How to align button to right side of text box in Bootstrap?
- How to load notification alert on top right corner without click of button in bootstrap ?
- button tag vs input type="button" attribute
- Include Bootstrap in AngularJS using ng-bootstrap
- HTML | button Tag
- jQuery | :button Selector
- How to call PHP function on the click of a Button ?
- How to change an input button image using CSS?
- How to check a radio button with jQuery?
- How to know which radio button is selected using jQuery?
- How to get value of selected radio button using JavaScript?
This is the most used method present online. In this case, the parent div is assigned a position ‘relative’ while the child divs are assigned position ‘absolute’.
Elements inside ‘absolute’ divs can be aligned in whatever way you want. Consider the following example. It shows all possible alignments a DOM element, in this case, buttons can have.
Example:
<!DOCTYPE html> <html> <head> <meta name="viewport" ontent="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" <title>Aligning Buttons</title> <style type="text/css"> html, body { height: 300px; } .top-left { top: 0; left: 0; } .top-center { top: 0; left: 50%; transform: translateX(-50%); } .top-right { top: 0; right: 0; } .mid-left { top: 50%; left: 0; transform: translateY(-50%); } .mid-center { top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); } .mid-right { top: 50%; right: 0; transform: translateY(-50%); } .bottom-left { bottom: 0; left: 0; } .bottom-center { bottom: 0; left: 50%; transform: translateX(-50%); } .bottom-right { bottom: 0; right: 0; } </style> </head> <body> <h1 style="color:green; text-align:center;">GeeksforGeeks</h1> <div class="container h-100"> <div class="position-relative h-100"> <div class="position-absolute top-left"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute top-center"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute top-right"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute mid-left"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute mid-center"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute mid-right"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute bottom-left"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute bottom-center"> <button type="button" class="btn btn-primary">Click Me!</button> </div> <div class="position-absolute bottom-right"> <button type="button" class="btn btn-primary">Click Me!</button> </div> </div> </div> <script src= </script> </body> </html> |
Output:

Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



