Learn how to center a button element vertically and horizontally with CSS.
<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 »
<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 things:
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
}
Try it Yourself »
Tip: Go to our CSS Align Tutorial to learn more about aligning elements.
Tip: Go to our CSS Transform Tutorial to learn more about how to scale elements.
Tip: Go to our CSS Flexbox Tutorial to learn more flexbox.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!