How to Center a Button in HTML and CSS [Different Ways]

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

Centering a button on a webpage is a common requirement in web design. Whether you're working with a single button or multiple elements, CSS provides several ways to center buttons both horizontally and vertically. Centering can be done using properties like text-align, flexbox, or margin.

In this tutorial, you'll learn different methods to center a button in HTML and CSS, both horizontally and vertically.

Problem Statement

Create CSS code that:

  • Centers a button horizontally on the page.
  • Optionally centers the button vertically as well.

Example:

  • Input: A button element with the text "Center Me".
  • Output: A button centered horizontally and vertically on the webpage.

Solution Steps

  1. Use the <button> Element: Create the button in HTML.
  2. Center the Button Horizontally Using CSS: Use methods like text-align, flexbox, or margin to center the button.
  3. Center the Button Vertically (Optional): Use additional CSS techniques to align the button vertically.

Method 1: Centering Horizontally with text-align

This is the simplest method for horizontally centering a button when it's inside a container.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Center Button with Text-Align</title>
    <style>
        /* Step 1: Style the button */
        .center-button {
            font-size: 1.2rem;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        /* Step 2: Center the button using text-align */
        .container {
            text-align: center; /* Centers the button horizontally */
            margin-top: 100px;
        }
    </style>
</head>
<body>

    <div class="container">
        <button class="center-button">Center Me</button>
    </div>

</body>
</html>

Output

You can play with the above HTML in Online HTML Editor and Compiler. Here is the output of the above HTML page.:

Explanation

  • text-align: center: This centers inline or inline-block elements, such as buttons, inside their container.

  • margin-top: 100px: This adds space above the button to push it lower on the page.


Method 2: Centering Horizontally with margin: auto

You can use the margin: auto method to center a block-level button.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Center Button with Margin Auto</title>
    <style>
        /* Step 1: Style the button */
        .center-button {
            font-size: 1.2rem;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            display: block; /* Make button block-level to apply margin */
            margin: 100px auto; /* Center the button horizontally */
        }
    </style>
</head>
<body>

    <button class="center-button">Center Me</button>

</body>
</html>

Explanation

  • display: block: The button is treated as a block-level element, which allows margins to apply.

  • margin: 100px auto: The auto value centers the button horizontally, while 100px adds space above and below the button.


Method 3: Centering Horizontally and Vertically with Flexbox

Flexbox is a powerful layout tool that easily centers elements both horizontally and vertically.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Center Button with Flexbox</title>
    <style>
        /* Step 1: Style the button */
        .center-button {
            font-size: 1.2rem;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        /* Step 2: Center button using flexbox */
        .container {
            display: flex;
            justify-content: center; /* Center horizontally */
            align-items: center; /* Center vertically */
            height: 100vh; /* Full viewport height to center vertically */
        }
    </style>
</head>
<body>

    <div class="container">
        <button class="center-button">Center Me</button>
    </div>

</body>
</html>

Explanation

  • display: flex: This turns the container into a flex container, allowing you to control alignment.

  • justify-content: center: Centers the button horizontally.

  • align-items: center: Centers the button vertically within the container.

  • height: 100vh: The container takes up the full viewport height, so the button can be centered vertically.


Method 4: Centering Horizontally and Vertically with position: absolute

This method uses absolute positioning to center the button in both dimensions.

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Center Button with Position Absolute</title>
    <style>
        /* Step 1: Style the button */
        .center-button {
            font-size: 1.2rem;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%); /* Center the button */
        }

        /* Step 2: Full-height container */
        .container {
            position: relative;
            height: 100vh; /* Full viewport height */
        }
    </style>
</head>
<body>

    <div class="container">
        <button class="center-button">Center Me</button>
    </div>

</body>
</html>

Explanation

  • position: absolute: This positions the button relative to its nearest positioned ancestor.

  • top: 50% and left: 50%: Places the button's top-left corner at the center of the container.

  • transform: translate(-50%, -50%): Offsets the button by half of its width and height, ensuring it is centered.

Conclusion

Centering a button in HTML and CSS can be done in various ways, depending on whether you want to center it horizontally, vertically, or both. You can use methods like text-align, margin: auto, flexbox, or absolute positioning to achieve the desired result. Flexbox is the most versatile and modern approach, but each method has its use cases depending on the layout.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare