Open In App

How to set the button alignment in Bootstrap ?

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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




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

    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="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script src=
        </script>
    </body>
      
    </html>                    

    
    

    Output:



    Last Updated : 04 Apr, 2023
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
Similar Reads