浮动在移动布局中不再重要,flex盒模型越来越重要。

flexbox经历过三个版本,主要区别是2009年到2012年之间的语法变化。

最新的语法和现在规范是同步的(例display:flex和“flex-{*}”属性)。
在这之间的语法是2011年出现的非官方语法,只能被IE识别(例display:flexbox;display: -ms-flexbox)。
最老的语法产生于2009年(例display: box;或者“box-{*}”属性)
- W3C 2009年第1次草案:[display:box;]((https://www.w3.org/TR/2012/WD-css3-flexbox-20120612/)

二、flex常用属性

1、用于父元素的样式

-webkit-box模型【旧】

  • display:-webkit-box 该属性会将此元素及其直系子代加入弹性框模型中。
  • box-orient:horizontal|vertical|inline-axis|block-axis|inherit;该属性定义父元素中的子元素是如何排列的。horizontal对父元素的宽度分配划分。
  • box-pack:start|end|center|justify;box-pack表示父容器里面子容器的水平对齐方式
  • box-align:start|end|center|baseline|stretch;表示父容器里面子容器的垂直对齐方式。

 flex模型 【新】

  • display:flex; flexbox模型只适用于直系子代
  • flex-direction: row | row-reverse | column | column-reverse;子元素是如何排列
  • justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;子元素水平排列方式
  • align-items: flex-start | flex-end | center | baseline | stretch;子元素垂直排列方式
  • flex-wrap: nowrap | wrap | wrap-reverse
  • align-content: flex-start | flex-end | center | space-between | space-around | stretch;

2、用于子元素的样式

【旧】box-flex:0|任意数字;该属性让子容器针对父容器的宽度按一定规则进行划分。 

【新】flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ],默认值 0 1 auto。

flex布局简介flex布局简介

flex布局简介

三、快速入门demo

1、子元素水平排列,按比例分割父元素宽度

.parent宽度500px,其子元素水平排列,child-one占1/6,child-two占2/6,child-three占了3/6。

<!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>Document</title>
    <style>
        .parent{
            width: 500px;
            height: 200px;
            display: flex;
            flex-direction: row;/* 虽然默认的排列方式是水平的,但是为了区分起见,加上该属性 */
        }
        .child-one{
            background: lightblue;
            flex: 1;
        }
        .child-two{
            background: lightgray;
            flex: 2;
        }
        .child-three{
            background: lightgreen;
            flex: 3;
        }
    </style>
</head>
<div style="display:flex;flex-direction:row;justify-content:center;border: 1px solid #000;"><!---box-pack:center让.parent水平居中-->
    <div class="parent">
        <div class="child-one">1</div>
        <div class="child-two">2</div>
        <div class="child-three">3</div>
    </div>
</div>
</body>
</html>

flex布局简介

2、子元素水平排列,一个子元素定宽,剩余子元素按比例分割

<!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>Document</title>
    <style>
        .parent{
            width: 500px;
            height: 200px;
            display: flex;
            background-color:pink;
            flex-direction: row;/* 虽然默认的排列方式是水平的,但是为了区分起见,加上该属性 */
        }
        .child-one{
            background: lightblue;
            flex: 1;
        }
        .child-two{
            background: lightgray;
            flex: 2;
        }
        .child-three{
            background: lightgreen;
            /*定宽,并加上左右margin,父元素加上粉色背景色更好理解*/
            width:150px;
            margin:0 15px;
        }
    </style>
</head>
<div style="display: flex;justify-content: center;border: 1px solid #000;"><!--justify-content:center让.parent水平居中-->
    <div class="parent">
        <div class="child-one">1</div>
        <div class="child-two">2</div>
        <div class="child-three">3</div>
    </div>
</div>
</body>
</html>
View Code

相关文章:

  • 2021-09-28
  • 2021-08-12
  • 2021-12-29
猜你喜欢
  • 2021-08-19
  • 2021-12-18
  • 2022-12-23
  • 2021-12-25
  • 2021-11-21
  • 2021-04-20
  • 2022-12-23
相关资源
相似解决方案