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:
    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,

  • .text-left
  • .text-center
  • .text-right

Syntax:

class="text-left"|"text-center"|"text-right"

Note: You can also use HTML5 <center> tag to align buttons to the center.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

    Buttons inside FlexBox:
    Another way of arranging buttons is by using flex-utilities. The basic flex classes provided by Bootstrap are

  • .d-flex .flex-row
  • .d-flex .flex-column

The following pattern can be achieved by choosing either one of them.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output: