×

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

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

CSS Dropdowns

Dropdowns are one of the maximum crucial parts of an interactive internet site. CSS is a kind used to design the drop-down menus. A drop-down essentially is a group of...

5 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 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 Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

How to create blinking text using CSS

The blinking effect is used in HTML and CSS to take the attraction towards a particular word or section. If you need such a blinking effect, we need to take...

2 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

Cascading Style Sheet

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

7 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

35 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

CSS Align

CSS Center Align Elements CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space....

2 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

Untitled Reusable Block

  ...

1 minute read.

CSS 2d Transform

Transform property in CSS is used mainly for enhancing the effects and to animate the elements. Transform is an effect that changes the shape, size and position of an element. It helps...

6 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.