flex布局案例-网格布局
# flex布局案例-网格布局
可用F12开发者工具查看元素及样式,可打开codepen在线编辑代码。
参考:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html (opens new window)
编辑 (opens new window)
上次更新: 2023/09/12, 14:55:44
可用F12开发者工具查看元素及样式,可打开codepen在线编辑代码。
<html>
<div class="grid">
<div class="grid-cell">1/2</div>
<div class="grid-cell">1/2</div>
</div>
<div class="grid">
<div class="grid-cell">1/3</div>
<div class="grid-cell">1/3</div>
<div class="grid-cell">1/3</div>
</div>
<div class="grid">
<div class="grid-cell">1/4</div>
<div class="grid-cell">1/4</div>
<div class="grid-cell">1/4</div>
<div class="grid-cell">1/4</div>
</div>
<div class="grid text">
<div class="grid-cell">
高度会跟随右侧元素变化
</div>
<div class="grid-cell">
内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充
</div>
</div>
<h4>某个网格设置百分比宽度</h4>
<div class="grid">
<div class="grid-cell u-full">100%</div>
</div>
<div class="grid">
<div class="grid-cell u-1of2">50%</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
</div>
<div class="grid">
<div class="grid-cell u-1of3">33.33%</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
</div>
<div class="grid">
<div class="grid-cell u-1of4">25%</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
<div class="grid-cell">auto</div>
</div>
</html>
<style>
.grid {
display: flex;
}
.grid-cell {
flex: 1;
}
.grid-cell.u-full {
flex: 0 0 100%;
}
.grid-cell.u-1of2 {
flex: 0 0 50%;
}
.grid-cell.u-1of3 {
flex: 0 0 33.3333%;
}
.grid-cell.u-1of4 {
flex: 0 0 25%;
}
/* 基础样式 */
.grid-cell {
background: #eee;
text-align: center;
margin: 5px;
padding: 10px 0;
}
.text .grid-cell {
text-align: left
}
</style>
参考:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html (opens new window)