前端布局面典型案例 面试题

前端布局典型案例

一、题目要求及页面效果示意图

  • 1)如下图布局,分为上下两栏,下面一栏又分左右两栏。上栏宽度为100%、高度为200px,下栏左侧栏宽度为100px,下栏右侧栏根据页面宽度自适应。请写出具体的HTML和css代码。
  • 2)当浏览器宽度减少到700px后,顶部栏高度变为50px,下栏左侧栏消失,下栏右侧栏宽度为100%。
    在这里插入图片描述

二、考点分析

  • 基础的布局知识,可以用伸缩布局和定位等多种方法实现。
  • 媒体查询,媒体查询根据屏幕尺寸的变化进行动态适配,语法为 @media (条件) {}。

三、解答代码

1)定位写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>面试题</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        @media (min-width: 701px) {
            .topbox {
                width: 100%;
                height: 200px;
                background-color: #33ccff;
            }
            .leftbox {
                width: 100px;
                position: absolute;
                top: 200px;
                left: 0;
                bottom: 0;
                background-color: #ffcc99;
            }
            .rightbox {
                position: absolute;
                top: 200px;
                left: 100px;
                right: 0;
                bottom: 0;
                background-color: #da84db;
            }
        }
        @media (max-width: 700px) {
            .topbox {
                width: 100%;
                height: 50px;
                background-color: #33ccff;
            }
            .leftbox {
                display: none;
            }
            .rightbox {
                width: 100%;
                position: absolute;
                top: 50px;
                bottom: 0;
                background-color: #da84db;
            }
        }
    </style>
</head>
<body>
    <div class="topbox">上栏</div>
    <div class="bottombox">
        <div class="leftbox">下栏左侧栏</div>
        <div class="rightbox">下栏右侧栏</div>
    </div>
</body>
</html>

2)伸缩布局写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>面试题</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            height: 100%;
        }
        body{
            display: flex;
            flex-direction:column;
            justify-content: space-between;
            height: 100%;
        }
        @media (min-width: 701px) {
            .topbox {
                width: 100%;
                height: 200px;
                background-color: #33ccff;
            }
            .bottombox{
                display: flex;
                justify-content: space-between;
                flex-grow: 1;
            }
            .leftbox {
                width: 100px;
                height: 100%;
                background-color: #ffcc99;
            }
            .rightbox {
                height: 100%;
                background-color: #da84db;
                flex-grow: 1;
            }
        }
        @media (max-width: 700px) {
            .topbox {
                width: 100%;
                height: 50px;
                background-color: #33ccff;
            }
            .leftbox {
                display: none;
            }
            .rightbox {
                width: 100%;
                flex-grow: 1;
                background-color: #da84db;
            }
        }
    </style>
</head>
<body>
    <div class="topbox">上栏</div>
    <div class="bottombox">
        <div class="leftbox">下栏左侧栏</div>
        <div class="rightbox">下栏右侧栏</div>
    </div>
</body>
</html>

四、页面展示

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初辰ge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值