前端经典面试题之CSS实现三栏布局,左右宽度固定,中间宽度自适应

前端常问的面试题,题目:假设高度一定,请写出三栏布局,左右宽度300px,中间自适应。

看到这里我希望你能停下来思考几分钟,

1分钟~2分钟~3分钟~4分钟~5分钟!



好了,那么你想出了几种答案呢?

下面提供这道题的五种解决方案:

首先要写好整个页面的布局(初始化等)

    <style>
        html * {
            padding: 0;
            margin: 0;
        }

        .layout {
            margin-top: 20px;
        }

        .layout article div {
            min-height: 100px;
        }
    </style>

1.浮动的解决方案

<!-- 浮动布局解决方案 -->
    <section class="layout float">
        <style>
            .layout.float .left {
                float: left;
                width: 300px;
                background: red;
            }

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

            .layout.float .center {
                background: yellow;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="right"></div>
            <div class="center">
                <h1>浮动解决方案</h1>
                <p>1.这是布局的中间部分</p>
                <p>2.这是布局的中间部分</p>
            </div>
        </article>
    </section>

2.绝对定位的解决方案

<!-- 绝对定位的解决方案 -->
    <section class="layout absolute">
        <style>
            .layout.absolute .left-center-right>div {
                position: absolute;
            }

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

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

            .layout.absolute .right {
                right: 0;
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>绝对定位的解决方案</h1>
                <p>1.这是布局的中间部分</p>
                <p>2.这是布局的中间部分</p>
            </div>
            <div class="right"></div>
        </article>
    </section>

3.flexbox的解决方案

<!-- flexbox解决方案 -->
    <section class="layout flexbox">
        <style>
            .layout.flexbox {
                margin-top: 140px;
            }

            .layout.flexbox .left-center-right {
                display: flex;
            }

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

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

            .layout.flexbox .right {
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>flexbox的解决方案</h1>
                <p>1.这是布局的中间部分</p>
                <p>2.这是布局的中间部分</p>
            </div>
            <div class="right"></div>
        </article>
    </section>

4.表格布局的解决方案

<!-- 表格布局的解决方案 -->
    <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: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>表格布局的解决方案</h1>
                <p>1.这是布局的中间部分</p>
                <p>2.这是布局的中间部分</p>
            </div>
            <div class="right"></div>
        </article>
    </section>

5.网格布局的解决方案

<!-- 网格布局的解决方案     -->
    <section class="layout grid">
        <style>
            .layout.grid .left-center-right {
                display: grid;
                width: 100%;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }

            .layout.grid .left {
                background: red;
            }

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

            .layout.grid .right {
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h1>网格布局的解决方案</h1>
                <p>1.这是布局的中间部分</p>
                <p>2.这是布局的中间部分</p>
            </div>
            <div class="right"></div>
        </article>
    </section>

最终的效果图:



  • 11
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: <div style="width:100%;"> <div style="float:left;width:30%;">左栏内容</div> <div style="float:left;width:40%;margin-left:2%;">中间栏内容</div> <div style="float:right;width:30%;">右栏内容</div> </div> ### 回答2: 可以使用HTML和CSS来创建一个三栏布局,其中左右两列的宽度固定,而中间列的宽度自适应。 首先,我们需要使用HTML创建一个三列的容器,可以使用`<div>`标签来实现。代码如下: ```html <div class="container"> <div class="left-column"></div> <div class="middle-column"></div> <div class="right-column"></div> </div> ``` 接下来,我们可以使用CSS来设置样式,以实现固定宽度自适应宽度的效果。首先,我们设置三列容器的样式,将其设置为一行布局,并使用`display: flex;`属性,使其自动伸缩。同时,设置容器的宽度为100%,以确保占据整个容器的宽度。代码如下: ```css .container { display: flex; width: 100%; } ``` 接下来,我们为左中右三列设置样式。设置左右两列的宽度固定值,例如200像素。代码如下: ```css .left-column { width: 200px; } .right-column { width: 200px; } ``` 为了让中间自适应宽度,我们可以使用`flex-grow`属性,设置中间列的伸缩比例为1,使其自动填充剩余的宽度。代码如下: ```css .middle-column { flex-grow: 1; } ``` 最后,为了使布局更加美观,我们可以为三列容器和内部的列添加一些样式,例如背景色和内边距。代码如下: ```css .container { background-color: #f1f1f1; padding: 10px; } .left-column, .middle-column, .right-column { background-color: #e0e0e0; padding: 10px; margin: 5px; } ``` 通过这样设置,我们就可以得到一个具有固定宽度左右列和自适应宽度中间列的三栏布局。 请注意,以上代码只是一个示例,你可以根据自己的需求进行调整和修改。 ### 回答3: 要实现一个左右宽度固定中间自适应三栏布局,可以使用HTML和CSS进行编写。 首先,在HTML中,我们可以使用```<div>```标签来创建三个元素,分别代表左栏、中栏和右栏。例如: ``` <div class="left-column">左栏内容</div> <div class="middle-column">中栏内容</div> <div class="right-column">右栏内容</div> ``` 然后,我们需要使用CSS来设置这些元素的样式。首先,设置左栏和右栏的宽度固定,可以使用```width```属性进行设置,例如: ``` .left-column { width: 200px; } .right-column { width: 200px; } ``` 接下来,设置中栏的宽度自适应,可以使用```flexbox```布局实现。在CSS中,将父元素设置为```display: flex;```,然后将中栏设置为```flex-grow: 1;```,这样中栏的宽度将会自适应。例如: ``` body { display: flex; } .middle-column { flex-grow: 1; } ``` 最后,可以添加一些样式来美化布局,如设置背景颜色、边框样式等。 完整的HTML和CSS代码如下: ``` <!DOCTYPE html> <html> <head> <style> .left-column { width: 200px; background-color: lightgray; } .middle-column { flex-grow: 1; background-color: white; } .right-column { width: 200px; background-color: lightgray; } </style> </head> <body> <div class="left-column">左栏内容</div> <div class="middle-column">中栏内容</div> <div class="right-column">右栏内容</div> </body> </html> ``` 通过上述HTML和CSS代码,我们实现了一个左右宽度固定中间自适应三栏布局
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值