How to Center a Button Inside a div

Learn how to use CSS to center button elements vertically and horizontally.

How to Center a Button Vertically

<style>
.container {
  height: 200px;
  position: relative;
  border: 3px solid green;
}
.vertical-center {
  margin: 0;
  position: absolute;
  top: 50%;
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
</style>
<div class="container">
  <div class="vertical-center">
    <button>Centered Button</button>
  </div>
</div>

Try It Yourself

How to Center Vertically and Horizontally

<style>
.container {
  height: 200px;
  position: relative;
  border: 3px solid green;
}
.center {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
</style>
<div class="container">
  <div class="center">
    <button>Centered Button</button>
  </div>
</div>

Try It Yourself

You can also use Flexbox to center elements:

Example

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green;
}

Try It Yourself

Related Pages

Tutorial:CSS Alignment

Tutorial:CSS Transformation

Tutorial:CSS Flexbox