前端开发

Flex布局

2019-06-09  本文已影响12人  意蜀

什么是flex布局呢?flex布局也称作是响应式布局,它具有灵活性和敏捷性。当你给一个容器加上这条display:flex,就意味着它是一个flex容器,也就意味着它的后代的子元素也就成为该flex容器的member。
这个容器会自默认有2个轴,一个叫做主轴,一个是与主轴垂直的交叉轴。但是默认它的方向是沿着主轴排列。

属性

  1. flex-direction代表着该flex容器内的主轴的方向,它的取值有4种。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .div{
            height:300px;
            width: 200px;
            background-color: gainsboro;
            display: flex;
            flex-wrap: nowrap;
        }
        .div1{
            height: 100px;
            margin-top: 10px;
            margin-left: 10px;
            width: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
<div class="div">
    <div class="div1">1</div>
    <div class="div1">2</div>
    <div class="div1">3</div>
    <div class="div1">4</div>
</div>
</body>
</html>
image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .div{
            height:300px;
            width: 800px;
            background-color: gainsboro;
            display: flex;
            flex-flow: column wrap;
        }
        .div1{
            height: 100px;
            margin-top: 10px;
            margin-left: 10px;
            width: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
<div class="div">
    <div class="div1">1</div>
    <div class="div1">2</div>
    <div class="div1">3</div>
    <div class="div1">4</div>
</div>
</body>
</html>
image.png
4.align-items规定了其在交叉轴的对齐方式。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .div{
            height:300px;
            width:900px;
            background-color: gainsboro;
            display: flex;
            flex-direction: row;
            align-items:stretch;
        }
        .div1{
            /* height: 100px; */
            margin-top: 10px;
            margin-left: 10px;
            width: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
<div class="div">
    <div class="div1">1</div>
    <div class="div1">2</div>
    <div class="div1">3</div>
    <div class="div1">4</div>
</div>
</body>
</html>
image.png
上一篇 下一篇

猜你喜欢

热点阅读