×

How to center a button in CSS

How to center a button in CSS?

In the tutorial, we will learn how to center a button in CSS.

CSS (Cascading Style Sheets) gives an HTML web page the greatest possible look. CSS allows you to control the layout of all of the page's elements. Elements can be aligned equally horizontally and vertically. If we want a CSS button to be centrally placed inside a div or in the middle of the web page, it must be center aligned. Making the button center might be difficult because there are so many different ways to do it using CSS.

The button in CSS can be placed in the center position by some methods, and the methods are as follows:

  1. margin: auto
  2. text-align: center
  3. display: flex
  4. display: grid
  5. position: fixed

Now let’s understand each of them one by one with the help of examples.

1)Text-align: center: - Add the text-align: center CSS property to the button element's outer container to center an HTML button element.

The 'text-align' center attribute on the <body> tag is used in the below example, so all the content inside the <body> tag is aligned in the center.

If we have a <div> element with two button elements, we must add the text-align: center CSS attribute to the <div> tag.

Example: -

<!DOCTYPE html>
<html lang="en">
<head>
    <title> </title>
    <style>
      
  body {
            text-align: center;
            color: purple;
        }
        button
        {
        	color: red;
            font-size:20px;
  }
    </style>
</head>
<body>
	<h1>This is an example of how to center button using text-align: center property</h1>
    <button>Click me. I am center aligned</button>
</body>
</html>

Output: -

How to center a button in CSS

2) margin: auto:-

We frequently encounter scenarios where we must center a button within its parent element. We usually use margin: auto; however, this does not appear to work.

It won't operate because a <button> is an inline-block level element. It won't operate. As a result, using margin: auto; seems to have no effect. To summaries, we must first make the <button> a block-level element before applying margin: auto; to it.

Here we will be using margin: auto for alignment of button in center.

Example: -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .button-container-div 
{
            width: 300px;
            height: 300px;
            margin: auto;
            font-size:20px;  
}
    </style>
</head>
<body>
    <div class="button-container-div">
  <h1>This is an example of how to center button using text-align: center property</h1>
    <button>Click me. I am center aligned</button>
    </div>
</body>
</html>

Output: -

How to center a button in CSS

3) display: flex: - We're applying the flex properties 'display,' 'justify-content,' and 'align-items' to the parent <div> element with class.  The button inside the< div> will be in the middle of the vertical and horizontal axis.

Vertically, display flex will move it to the middle.

The button will be moved to the center of the page using justify-content.

It will keep the button in the center or middle, beginning from the top and ending at the bottom.

Let’s understand with an example.

Example: -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> </title>
    <style>
        .button_eg_container {
            width: 300px;
            height: 300px;
            border: 2px solid blue;
            display: flex;
            justify-content: center;
            align-items: center;
   }
    </style>
</head>
<body>
<h1>This is an example of how to center button using display:flex property</h1>
    <div class="button_eg_container">
     <button>Click me. I am center aligned</button>
    </div>
</body>
</html>

Output: -

How to center a button in CSS

4)Display: grid:-

We are using the 'display' grid attribute on the <div> tag with class. The center of vertical and horizontal locations will be occupied by the button inside< div>.

Example: -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .eg_button_div{
            width: 400px;
            height: 400px;
            border: 2px solid purple;
            display: grid;
        }
        button {
            background-color: cyan;
            border: 2px solid;
            padding: 10px;
            width: 100px;
            outline: none;
            margin: auto;
        }
    </style>
</head>
<body>
 <h1>This is an example of how to center button using text-align: center property</h1>
    <div class="eg_button_div">
      <button>Click me. I am center aligned</button>
    </div>
    </div>
</body>	
</html>

Output: -

How to center a button in CSS

5)Position: fixed: - We will give a 50 percent margin from the left side of the page, and then we will use position: fixed to modify the position in the middle of the body.

Example: -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .eg_button_div {
           position: fixed;
           left: 20%;
           right:20%;
        }
    </style>
</head>
<body>
<div class="eg_button_div">
    <h1>This is an example of how to center button using text-align: center property</h1>
  <button>Click me. I am center aligned</button>
    </div>
</body>
</html>

Output: -

How to center a button in CSS

Related Topics

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

How to get rid/remove of bullet pioints in CSS

We include a list of items on the web pages that were sometimes numbered and sometimes bulleted in various styles. In HTML, there are two kinds of lists: ordered and unordered...

3 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

Css Text Orientation

Introduction: The text-orientation property of CSS is used to specify the orientation of the text characters in the line of content. This property applies only with the vertical writing-mode and not...

4 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

CSS Text-Transform Property

Text-Transform in CSS The text-transform property permits us to alter the text case. It controls the capitalization of the text. This property is employed to design the text appearance in every...

3 minutes read.

CSS Letter-spacing

CSS Letter-spacing: Letter-spacing attribute is applied to manage the space among all letters within the block or element of the text. This property sets the behavior of the spacing between...

2 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 minutes read.

CSS Background Color

It is one of the CSS properties to apply styling to html elements.This property is used to apply the background color to any HTML element.The background of an element includes...

2 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Inline

What is CSS? CSS is an acronym for Cascading Style Sheets. CSS is the language that we use to style an HTML document. It specifies how HTML components should appear. CSS...

4 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

CSS Text-decoration

CSS Text-decoration: It is an essential property of CSS that is used to decorate the content of the text. It can add-on the lines above, under, and over the text....

3 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.