在 CSS3 中,**弹性盒子布局(Flexbox)**主要通过 display: flex 来实现,用来更方便地控制子元素在容器中的排列、对齐和空间分配。
下面从 基本用法 → 常用属性 → 示例 一步步说明。
.container {
display: flex;
}
或者:
.container {
display: inline-flex;
}
flex:块级弹性容器
inline-flex:行内弹性容器
flex-direction.container {
flex-direction: row; /* 默认,水平 */
/* row-reverse | column | column-reverse */
}
flex-wrap.container {
flex-wrap: nowrap; /* 默认不换行 */
/* wrap | wrap-reverse */
}
justify-content.container {
justify-content: flex-start; /* 默认 */
/* flex-end | center |
space-between | space-around | space-evenly */
}
align-items.container {
align-items: stretch; /* 默认 */
/* flex-start | flex-end | center | baseline */
}
align-content(仅在 flex-wrap: wrap 时生效)
.container {
align-content: flex-start;
/* flex-end | center | space-between | space-around | stretch */
}
flex-grow.item {
flex-grow: 1; /* 默认 0 */
}
flex-shrink.item {
flex-shrink: 1; /* 默认 */
}
flex-basis.item {
flex-basis: 100px;
}
flex.item {
flex: 1; /* grow */
/* flex: 1 1 auto; */
}
align-self.item {
align-self: center;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
display: flex;
justify-content: space-between;
align-items: center;
height: 100px;
background: #f0f0f0;
}
.item {
width: 50px;
height: 50px;
background: skyblue;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
.container {
display: flex;
justify-content: space-between;
}
.item {
flex: 1;
}
如果你有具体布局需求(比如导航栏、卡片列表、移动端布局),可以直接说,我可以帮你写完整代码。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。