flexbox属性速览及常见布局实现
CSS3 弹性盒子(Flex Box)弹性盒子是即 CSS2 浮动布局后, CSS3 的一种新的布局模式。
CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。
引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、对齐和分配空白空间。
弹性盒子父元素属性
创建弹性盒子
<style>
.flex-container {
display: flex;
width: 300px;
height: 200px;
}
</style>
<div class="flex-container">我是一个弹性盒子</div>
flex-direction设置子元素的排列方式
属性对照:
| 属性名 | 描述 |
|---|---|
| row | 横向从左到右排列(左对齐),默认的排列方式 |
| row-reverse | 反转横向排列(右对齐,从后往前排,最后一项排在最前面 |
| column | 纵向排列 |
| column-reverse | 反转纵向排列,从后往前排,最后一项排在最上面 |
<style>
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
width: 300px;
height: 200px;
}
</style>
<div class="flex-container">我是一个弹性盒子</div>
justify-content设置子元素的水平对齐方式
属性对照:
| 属性名 | 描述 |
|---|---|
| flex-start | 左对齐(默认值) |
| flex-end | 右对齐 |
| center | 居中对齐 |
| space-between | 子元素平均分布在该行上 |
| space-around | 弹性项目平均分布在该行上,两边留有一半的间隔空间 |
<style>
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
justify-content: flex-start | flex-end | center | space-between | space-around
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
</style>
<div class="flex-container">
<div class="flex-item">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
align-items 设置子元素的垂直对齐方式
属性对照:
| 属性名 | 描述 |
|---|---|
| flex-start | 上对齐 |
| flex-end | 下对齐 |
| center | 垂直居中对齐 |
| baseline | 基线对齐 |
| stretch | 父元素设置高度,自动填充高度对齐 |
<style>
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
align-items: flex-start | flex-end | center | baseline | stretch
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
</style>
<div class="flex-container">
<div class="flex-item">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
flex-wrap 指定弹性盒子的子元素换行方式
属性对照:
| 属性名 | 描述 |
|---|---|
| nowrap | 默认,弹性容器为单行 |
| wrap | 弹性容器为多行 |
| wrap-reverse | 反转 wrap 排列 |
<style>
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap|wrap|wrap-reverse|initial|inherit;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
</style>
<div class="flex-container">
<div class="flex-item">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
align-content 设置flex-wrap属性下子元素的行对齐
属性对照:
| 属性名 | 描述 |
|---|---|
| stretch | 默认。各行将会伸展以占用剩余的空间 |
| flex-start | 行左堆叠 |
| flex-end | 行右堆叠 |
| center | 各行向弹性盒容器的中间位置堆叠 |
| space-between | 各行在弹性盒容器中平均分布 |
| space-around | 各行在弹性盒容器中平均分布,两端保留子元素与子元素之间间距大小的一半 |
<style>
.flex-container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: wrap;
align-content: center;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
</style>
<div class="flex-container">
<div class="flex-item">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
flex-flow 是flex-direction 和 flex-wrap 的简写
<style>
.flex-container {
display: flex;
flex-flow: row-reverse wrap;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
</style>
<div class="flex-container">
<div class="flex-item">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
弹性盒子元素属性
order 排序
用整数值来定义排列顺序,数值小的排在前面。可以为负值
<style>
.flex-container {
display: flex;
flex-flow: row-reverse wrap;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
.first {
order: -1;
}
</style>
<div class="flex-container">
<div class="flex-item first">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item">item-3</div>
</div>
margin对齐
设置"margin"值为"auto"值,自动获取弹性容器中剩余的空间。所以设置垂直方向margin值为"auto",可以使弹性子元素在弹性容器的两上轴方向都完全居中。
<style>
.flex-container {
display: flex;
flex-flow: row-reverse wrap;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
.first {
order: -1;
}
.three {
margin-right: auto;
}
<!--完美的居中-->
.flex-item-1 {
margin: auto;
}
</style>
<div class="flex-container">
<div class="flex-item first">item-1</div>
<div class="flex-item">item-2</div>
<div class="flex-item three">item-3</div>
</div>
<!--完美的居中-->
<div class="flex-container">
<div class="flex-item-1">完美的居中</div>
</div>
align-self 设置元素自身在X、Y轴上到对齐方式
属性对照:
| 属性名 | 描述 |
|---|---|
| auto | 如果align-self的值为auto,则其计算值为元素的父元素的align-items值,如果其没有父元素,则计算值为'stretch'。 |
| flex-start | 子元素的X轴(Y轴)起始位置对齐 |
| flex-end | 子元素的X轴(Y轴)结束位置对齐 |
| center | 子元素的X轴(Y轴)居中对齐 |
| baseline | 子元素的X轴(Y轴)基线对齐 |
<style>
.flex-container {
display: flex;
flex-flow: row-reverse wrap;
width: 300px;
height: 200px;
}
.flex-item {
width: 100px;
height: 100px;
background: #f66;
}
.item-1 {
align-self: flex-start;
}
.item-2 {
align-self: center;
}
.item-3 {
align-self: flex-end;
}
</style>
<div class="flex-container">
<div class="flex-item item-1">item-1</div>
<div class="flex-item item-2">item-2</div>
<div class="flex-item item-3">item-3</div>
</div>
flex 指定弹性子元素如何分配空间
属性对照:
| 属性名 | 描述 |
|---|---|
| none | none关键字的计算值为: 0 0 auto |
| [ flex-grow ] | 定义弹性盒子元素的扩展比率 |
| [ flex-shrink ] | 定义弹性盒子元素的收缩比率 |
| [ flex-basis ] | 定义弹性盒子元素的默认基准值 |
<style>
.flex-container {
display: -webkit-flex;
display: flex;
width: 400px;
height: 250px;
background-color: lightgrey;
}
.flex-item {
background-color: cornflowerblue;
margin: 10px;
}
.item1 {
-webkit-flex: 2;
flex: 2;
}
.item2 {
-webkit-flex: 1;
flex: 1;
}
.item3 {
-webkit-flex: 1;
flex: 1;
}
</style>
<div class="flex-container">
<div class="flex-item item1">flex item 1</div>
<div class="flex-item item2">flex item 2</div>
<div class="flex-item item3">flex item 3</div>
</div>
弹性盒子响应式布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Flexbox 响应式布局</title>
<style>
* {
margin: 0;
padding: 0;
}
.flex-container {
display: flex;
flex-flow: row wrap;
font-weight: bold;
text-align: center;
}
.flex-container > * {
padding: 10px;
flex: 1 100%;
}
.flex-header {
background: coral;
}
.flex-main {
background: cornflowerblue;
}
.flex-footer {
background: lightgreen;
}
.aside1 {
background: moccasin;
}
.aside2 {
background: violet;
}
@media all and (min-width: 600px) {
.aside {
flex: 1 auto;
}
}
@media all and (min-width: 800px) {
.flex-main {
flex: 4 0px;
}
.aside1 { order: 1;}
.flex-main { order: 2;}
.aside2 { order: 3;}
.flex-footer { order: 4; }
}
</style>
</head>
<body>
<div class="flex-container">
<header class="flex-header">头部</header>
<article class="flex-main">
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Aut atque id obcaecati quasi alias eveniet facere necessitatibus sit distinctio ipsa nihil officia architecto odit debitis natus maxime, perspiciatis maiores labore.</p>
</article>
<aside class="aside aside1">边栏1</aside>
<aside class="aside aside2">边栏2</aside>
<footer class="flex-footer">底部</footer>
</div>
</body>
</html>
flexbox属性速览及常见布局实现的更多相关文章
- CSS(二)- 属性速览(含版本、继承性和简介)
相关链接 CSS3速查表,这里面列出了所有新增的属性以及新增或者修改的属性值 css参考手册,很好地一个常用网站 CSS定位(不可继承) CSS布局(仅visibility可继承) CSS尺寸(不可继 ...
- Flexbox属性可视化指南
Flexbox 布局(国内很多人称为弹性布局)正式的全称为 CSS Flexible Box布局模块,它是CSS3新增的一种布局模式.它可以很方便地用来改善动态或未知大小的元素的对齐,方向和顺序等等. ...
- 几种常见布局的flex写法
flex布局目前基本上兼容主流的浏览器,且实现方式简单.我整理了flex的一些知识点,并且总结归纳了几种常见布局的flex写法 flex基础知识点 flex-grow和flex-shrink相关计算 ...
- Flex布局 Flexbox属性具体解释
原文:A Visual Guide to CSS3 Flexbox Properties Flex布局官方称为CSS Flexble Box布局模型是CSS3为了提高元素在容器中的对齐.方向.顺序,甚 ...
- 100 个常见错误「GitHub 热点速览 v.22.35」
本周的特推非常得延续上周的特点--会玩,向别人家的女朋友发送早安.这个错误是如何发生的呢?如何有效避免呢?自己用 daily_morning 免部署.定制一个早安小助手给女友吧. 除了生活中的错误,工 ...
- HTML5 速览
HTML5 速览 一. HTML5 元素分类 HTML赋值文档内容的结构和含义, 内容呈现由css样式控制 元素选用原则 少亦可为多 标记只应该应内容对语义的需要使用. 有条经验法则是: 问问自己打算 ...
- .NET平台开源项目速览(1)SharpConfig配置文件读写组件
在.NET平台日常开发中,读取配置文件是一个很常见的需求.以前都是使用System.Configuration.ConfigurationSettings来操作,这个说实话,搞起来比较费劲.不知道大家 ...
- .NET平台开源项目速览(8)Expression Evaluator表达式计算组件使用
在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下Expression Evaluator验证组件.那里只是概述了一下,并没有对其使用和强大功能做 ...
- .NET平台开源项目速览(20)Newlife.Core中简单灵活的配置文件
记得5年前开始拼命翻读X组件的源码,特别是XCode,但对Newlife.Core 的东西了解很少,最多只是会用用,而且用到的只是九牛一毛.里面好用的东西太多了. 最近一年时间,零零散散又学了很多,也 ...
随机推荐
- Js数据类型和运算符
1.数据类型 原始类型:数值 字符串 布尔值: 复杂类型:对象: 数值(number) 特殊的数值:NaN,NaN不等于任何 ...
- Android下载Android源码
使用Git,命令是:git clone http://android.googlesource.com/platform/frameworks/base.git
- 01二维背包+bitset优化——hdu5890
口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退 ...
- NX二次开发-UFUN获取相邻面UF_MODL_ask_adjac_faces
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> UF_initialize( ...
- NSDateFormatter 今年日期格式化成字符串是明年日期问题?
在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...
- Vue.js - 路由 vue-router 的使用详解2(参数传递)
一.使用冒号(:)的形式传递参数 1,路由列表的参数设置 (1)路由列表的 path 是可以带参数的,我们在路由配置文件(router/index.js)里以冒号的形式设置参数. (2)下面样例代码中 ...
- Go中的switch fallthrough
Go基础 switch sExpr { case expr1: some instructions case expr2: some other instructions case expr3: ...
- 使用Devstack部署neutron网络节点
本文为minxihou的翻译文章,转载请注明出处Bob Hou: http://blog.csdn.net/minxihou JmilkFan:minxihou的技术博文方向是 算法&Open ...
- iBATIS存储过程
使用iBATIS配置来调用存储过程.为了理解这一章,首先需要了解我们是如何在MySQL中创建一个存储过程. 在继续对本章学习之前,可以通过MySQL存储过程. 我们已经在MySQL下有EMPLOYEE ...
- unittest(2)
测试用例执行顺序 1.setUp和tearDown相关 setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对 ...