Flexbox布局
Flexbox简介
以上为W3C对flexbox的部分解释,大概就是说flexbox的出现是为了解决复杂的web布局,因为这种布局方式很灵活。容器的子元素可以任意方向进行排列。此属性目前处于非正式标准,以下是各浏览器对flexbox的支持程度,在较新的浏览器中基本可以使用该属性。 Flexbox模型及术语flex布局模型不同于块和内联模型布局,块和内联模型的布局计算依赖于块和内联的流方向,而flex布局依赖于flex directions.简单的说:Flexbox是布局模块,而不是一个简单的属性,它包含父元素(flex container)和子元素(flex items)的属性。
Flexbox使用示例水平竖直居中下面这个例子是我们用的很多的完全居中(上下左右居中),我们可以用很多种方法实现,但目前只有用Flexbox实现是最为简单的。 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>center</title>
<link rel="stylesheet" href="./center.css">
</head>
<body>
<div class="parent"><div class="child"></div></div>
</body>
</html>
body{ padding: 0; margin: 0; }
.parent { display: flex; height: 300px; /* Or whatever */ background-color: black; }
.child { width: 100px; /* Or whatever */ height: 100px; /* Or whatever */ margin: auto; /* Magic! */ background-color: white; }
在Flex容器中,当项目边距设置为“auto”时,设置自动的垂直边距将使该项目完全集中两个轴。 六个子元素布局再看一个例子,将子元素的数量增加到六个。六个子元素随着浏览器大小改变布局而不需要用媒体查询。 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>six</title>
<link rel="stylesheet" href="./six.css">
</head>
<body>
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
</ul>
</body>
</html>
body{ margin: 0; padding: 0; }
ul { margin: 0; padding: 0; }
li{ list-style: none; }
.flex-container { /* We first create a flex layout context */ display: flex; /* Then we define the flow direction and if we allow the items to wrap * Remember this is the same as: * flex-direction: row; * flex-wrap: wrap; */ flex-flow: row wrap; /* Then we define how is distributed the remaining space */ justify-content: space-around; }
.flex-item { background: tomato; padding: 5px; width: 200px; height: 150px; margin-top: 10px; line-height: 150px; color: white; font-weight: bold; font-size: 3em; text-align: center; }
效果如下: 实现图中效果,需要设置三个属性:flex-direction: row; flex-wrap: wrap; justify-content: space-around;下节将介绍各属性。 属性1.display(flex container) display: other values | flex | inline-flex;
2.flex-direction(flex container) 这个主要用来创建主轴,从而定义了伸缩项目放置在伸缩容器的方向。 flex-direction: row | row-reverse | column | column-reverse
3.order(flex items) order: <integer>
4.flex-wrap(flex container) flex-wrap: nowrap | wrap | wrap-reverse
5.flex-flow(flex container) flex-flow: <‘flex-direction’> || <‘flex-wrap’>
div { flex-flow: row; }/* Initial value. Main-axis is inline,no wrap. */
div { flex-flow: row-reverse wrap-reverse; }/* Main-axis is the opposite of inline direction(right to left). New lines wrap upwards. */
6.justify-content(flex container) justify-content: flex-start | flex-end | center | space-between | space-around;
7.align-content(flex container) align-content: flex-start | flex-end | center | space-between | space-around | stretch;
8.align-items(flex container) align-items: flex-start | flex-end | center | baseline | stretch
9.align-self(flex items) align-self: auto | flex-start | flex-end | center | baseline | stretch;
10.flex-grow(flex items) flex-grow: <number>; /* default 0 */
如果所有伸缩项目的“flex-grow”设置了“1”,那么每个伸缩项目将设置为一个大小相等的剩余空间。如果你给其中一个伸缩项目设置了“flex-grow”值为“2”,那么这个伸缩项目所占的剩余空间是其他伸缩项目所占剩余空间的两倍。如下图: 11.flex-shrink(flex items) flex-shrink: <number>; /* default 1 */
12.flex-basis(flex items) flex-basis: <length> | auto; /* default auto */
如果设置为“0”,不考虑剩余空白空间。如果设置为自动,则按照flex-grow值分配剩余空白空间。如图所示: 13.flex(flex items) flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
其他资源
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |