flex布局应用,一种讨巧的header布局方法
在开发移动端页面的时候,我们时常需要一个页面的header。 例如提供返回,设置菜单,或者标题显示。
这里提供一种利用flex属性进行响应布局的方法:
情况一,三栏布局。
如下图:
想法是这样的:
示例demo:
//vue + vant demo
我的信息
//css
.My {
height: 100vh;
background-color: aquamarine;
}
.header {
justify-content: space-between;
display: flex;
height: 3em;
}
.header .one {
display: flex;
justify-content: flex-start;
align-content: center;
}
.header .two {
display: flex;
justify-content: center;
align-content: center;
}
.header .three {
display: flex;
justify-content: flex-end;
align-content: center;
}
.header .one,
.two,
.three {
width: 100px;//也可以写auto
}
.header .one *{
border: none;
background: transparent;
display: flex;
justify-content: center;
align-items: center;
}
.header .info {
font-size: larger;
display: flex;
justify-content: center;
align-items: center;
}
.header .three *{
border: none;
background: transparent;
display: flex;
justify-content: center;
align-items: center;
}
情况二,两栏布局
如下图:
//vue + vant demo
联系管理员
电话12341234
//css
.ConnectTheManager {
height: 100vh;
}
.header {
justify-content: space-between;
display: flex;
height: 3em;
}
.header .one {
display: flex;
justify-content: flex-start;
align-content: center;
}
.header .two {
display: flex;
justify-content: center;
align-content: center;
}
.header .three {
display: flex;
justify-content: flex-end;
align-content: center;
}
.header .one,
.two,
.three {
width: 100px;//特别注意这里。 如果不指定宽度,就无法水平三栏等距分布。
}
.header .one * {
border: none;
background: transparent;
display: flex;
justify-content: center;
align-items: center;
}
.header .info {
font-size: larger;
display: flex;
justify-content: center;
align-items: center;
}
同样的思路,四栏,五栏甚至更多,都能用这种方法实现,自适应的水平,垂直居中。