以下是一些常见的 display: flex 子元素布局示例的 CSS 代码。
css.container {
    display: flex;
    justify-content: center;
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightblue;
}
css复制代码
.container {
    display: flex;
    align-items: center;
    height: 100vh; /* 确保容器有高度 */
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightcoral;
}
css.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh; /* 确保容器有高度 */
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightgreen;
}
css.container {
    display: flex;
    justify-content: space-between;
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightpink;
}
css.container {
    display: flex;
    flex-direction: column;
    height: 100vh; /* 确保容器有高度 */
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightseagreen;
    margin-bottom: 10px; /* 添加间距 */
}
css.container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 100vh; /* 确保容器有高度 */
}
.item {
    width: 100px;
    height: 100px;
    background-color: lightskyblue;
}
css.container {
    display: flex;
}
.item {
    flex: 1;
    height: 100px;
    background-color: lightsteelblue;
    margin: 5px; /* 添加间距 */
}
这些示例展示了使用 display: flex 可以实现的常见布局方式,可以根据具体需求调整和组合这些布局属性。
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!