CSS 中有很多方法可以将按钮居中,但是有时候需要居中的是两个按钮,这时候该怎么办呢?下面介绍两种方法。
.method1 {
display: flex;
justify-content: center;
}
.method1 button {
margin: 0 10px;
}
.method2 {
text-align: center;
}
.method2 button:first-child {
margin-right: 10px;
}
.method2 button:last-child {
margin-left: 10px;
} 第一种方法是使用 display: flex; 和 justify-content: center; 来让两个按钮水平居中。同时在按钮上设置一个右、左边距,使按钮之间具有一定的间距。
第二种方法是使用 text-align: center; 来让按钮在父容器内水平居中,然后对第一个按钮设置右边距,对第二个按钮设置左边距,以达到按钮之间具有一定的间距效果。