要想让多个元素都居中,需要将该属性应用于它们的父容器上。下面是一个示例:
HTML代码:
CSS代码:
.container {
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 100px;
height: 100px;
background-color: red;
margin: 10px;
}
在该示例中,我们将 align-items:center 应用于容器 .container 上,并使用 justify-content:center 将子元素居中。这样,在 .container 中的所有子元素都将垂直居中。