How to center a button element vertically and horizontally with CSS?

To center a button vertically and horizontally with CSS, use the justify-content and align-items properties with flexbox. This creates perfect centering for button elements within their container.

Syntax

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: value;
}

The Justify-content Property

In CSS, the justify-content property is used to align the items horizontally. The syntax of CSS justify-content property is as follows −

Selector {
    display: flex;
    justify-content: /*value*/;
}

The following are the values −

  • flex-start − The flex items are positioned at the start of the container. This is the Default value.

  • flex-end − The flex items are positioned at the end of the container

  • center − The flex items are positioned in the center of the container

  • space-between − The flex items will have space between them

  • space-around − The flex items will have space before, between, and after them

  • space-evenly − The flex items will have equal space around them

The Align-items Property

In CSS, the align-items property is used to set the default alignment for flex-items in a flexbox. It aligns the items vertically.

The syntax of CSS align-items property is as follows −

Selector {
    display: flex;
    align-items: /*value*/;
}

The following are the values −

  • stretch − The flex-items are stretched to fit the container

  • center − The flex-items are positioned at the center of the container

  • flex-start − The flex-items are positioned at the beginning of the container

  • flex-end − The flex-items are positioned at the end of the container

Example: Centering a Button

The following example demonstrates how to center a button both vertically and horizontally using flexbox −

<!DOCTYPE html>
<html>
<head>
<style>
    .centered {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 200px;
        border: 3px solid rgb(0, 70, 128);
        background-color: #f9f9f9;
    }
    button {
        font-size: 18px;
        border: none;
        padding: 15px 25px;
        background-color: darkblue;
        color: white;
        border-radius: 5px;
        cursor: pointer;
    }
</style>
</head>
<body>
    <h1>Centered Button Example</h1>
    <div class="centered">
        <button>This button is centered</button>
    </div>
</body>
</html>

The output of the above code is −

A dark blue button with white text "This button is centered" appears perfectly centered both horizontally and vertically within a bordered container.

Key Points

  • Set display: flex on the parent container

  • Use justify-content: center for horizontal centering

  • Use align-items: center for vertical centering

  • The container must have a defined height for vertical centering to be visible

Conclusion

Using flexbox with justify-content: center and align-items: center is the most reliable method to center buttons both vertically and horizontally. This technique works consistently across modern browsers and provides responsive centering.

Updated on: 2026-03-15T14:59:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements