前端知识总结

113 阅读1分钟

HTML

题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应
  • float布局
  • 绝对定位布局
  • flex布局
  • 表格布局
  • 网格布局
<!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>前端面试题:布局</title>
    <style media="screen">
        html * {
            padding: 0;
            margin: 0;
        }

        .layout article div {
            min-height: 100px;
        }

        .layout {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <header>
        <h4>页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应</h4>
    </header>
    <!--float布局-->
    <section class="layout float">
        <style media="screen">
            .layout.float:after {
                content: ".";
                clear: both;
                display: block;
                height: 0;
                overflow: hidden;
                visibility: hidden;
            }

            .layout.float .left {
                float: left;
                background: red;
                width: 300px;
            }

            .layout.float .right {
                float: right;
                background: pink;
                width: 300px;
            }

            .layout.float .center {
                background: yellow;
            }
        </style>
        <article class="left-right-center">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮动解决方案</h1>
                1、这是三栏布局中间部分
                2、这是三栏布局中间部分
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
            </div>
        </article>
    </section>
    <!--绝对定位布局-->
    <section class="layout absolute">
        <style>
            .layout.absolute .left-center-right>div {
                position: absolute;
            }

            .layout.absolute .left {
                width: 300px;
                left: 0;
                background: red;
            }

            .layout.absolute .center {
                left: 300px;
                right: 300px;
                background: yellow;
            }

            .layout.absolute .right {
                right: 0;
                width: 300px;
                background: pink;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>绝对定位解决方案</h1>
                1、这是三栏布局中间部分
                2、这是三栏布局中间部分
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
            </div>
            <div class="right"></div>
        </article>
    </section>
    <!--flex布局-->
    <section class="layout flexbox">
        <style>
            .layout.flexbox .left-center-right {
                display: flex;
                margin-top: 140px;
            }

            .layout.flexbox .left {
                width: 300px;
                background: red;
            }

            .layout.flexbox .center {
                flex: 1;
                background: yellow;
            }

            .layout.flexbox .right {
                width: 300px;
                background: pink;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>flex布局解决方案</h1>
                1、这是三栏布局中间部分
                2、这是三栏布局中间部分
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
            </div>
            <div class="right"></div>
        </article>
    </section>
    <!--表格布局-->
    <section class="layout table">
        <style>
            .layout.table .left-center-right {
                width: 100%;
                display: table;
                height: 100px;
            }

            .layout.table .left-center-right>div {
                display: table-cell;
            }

            .layout.table .left {
                width: 300px;
                background: red;
            }

            .layout.table .center {
                background: yellow;
            }

            .layout.table .right {
                width: 300px;
                background: pink;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>表格布局解决方案</h1>
                1、这是三栏布局中间部分
                2、这是三栏布局中间部分
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
            </div>
            <div class="right"></div>
        </article>
    </section>
    <!--网格布局-->
    <section class="layout grid">
        <style>
            .layout.grid .left-center-right {
                width: 100%;
                display: grid;
                height: 100px;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }
            .layout.grid .left{
                background: red;
            }
            .layout.grid .center{
                background: yellow;
            }
            .layout.grid .right{
                background: pink;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>网格布局解决方案</h1>
                1、这是三栏布局中间部分
                2、这是三栏布局中间部分
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
                <p>增加高度增加高度</p>
            </div>
            <div class="right"></div>
        </article>
    </section>
</body>
</html>