Monday, January 13, 2025
HomeProgrammingHow to center a button in CSS

How to center a button in CSS

To center a button in CSS, you can use several methods depending on the layout. Here are common ways:

  1. Using Flexbox:
    css
    .container {
    display: flex;
    justify-content: center; /* Horizontal center */
    align-items: center; /* Vertical center */
    height: 100vh; /* Full viewport height */
    }
  2. Using Grid:
    css
    .container {
    display: grid;
    place-items: center; /* Centers both horizontally and vertically */
    height: 100vh;
    }
  3. Using Text-align and Margin (for horizontal centering):
    css
    .container {
    text-align: center;
    }
    .button {
    margin: 0 auto; /* Horizontal centering */
    }

These methods will center the button within its container element effectively.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x