flex布局基础学习

122 阅读1分钟

flex 布局记熟以下这张图,搞懂主轴与侧轴即可

flex布局html代码

<!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> 
        * {
            margin: 0;
            padding: 0;
        }
        body,html {
            display: flex;
            flex-direction: column;
            align-items: stretch;
            width: 100%;
            height: 100%;
            background: #eee;
        }
        header {
            flex-basis: 40px;
            background: red;
        }
        main {
            display: flex;
            flex-grow: 1;
            padding-bottom: 40px;
            background: blue;
        }
        aside {
            flex-basis: 250px;
            margin-right: 20px;
            background: yellow;
        }
        section {
            
            flex-grow: 1;
            background: black;
        }
        footer {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: gray;
        }
    </style>
</head>
<body>
    <header>顶部</header>
    <main>
        <aside>左侧</aside>
        <section>右侧</section>
    </main>
    <footer>底部</footer>
</body>
</html>
  • flex-direction 主轴方向,默认是横轴,可改为纵轴
  • flex-basis 默认宽度/高度,由主轴方向决定是宽度还是高度
  • flex-grow 伸度,有多的位置自动扩张
  • flex-shrink 缩度,挤压时自动缩小空间
  • align-items 内容在交叉轴上的对齐方式,默认stretch,自动拉伸至两端对齐

学会以上即可用来做简单的两栏布局了