web前端面试题以及相关答案解析

web前端工程师面试题

CSS面试题

1.盒模型

答案: 基础概念 :  标准模型 + IE模型  包括:  margin , border ,padding ,content

    标准模型和IE模型的区别 :  内容计算方式不同

      1, IE模型 元素宽度  width  =content  + padding +border

         标准模型 元素宽度 width = content

2.如何让一个盒子水平垂直居中?

答案:  

方式一:  绝对定位 +margin (需要指定子元素的宽高 ,不推荐)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .div {
        width: 750px;
        height: 750px;
        background: violet;
        position: relative;
    }

    .div1 {
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -75px;
        margin-left: -75px;
        background: turquoise;
    }

    .big {
        width: 100%;
        height: 100%;
    }
</style>

<body>
    <div class="big">
        <div class="div">
            盒子一
            <div class="div1">盒子二</div>
        </div>
    </div>
</body>

</html>

方式二: 绝对定位 + translate(无需指定子元素的宽高,推荐)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .div {
        width: 750px;
        height: 750px;
        background: violet;
        position: relative;
    }

    .div1 {

        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: turquoise;
    }

    .big {
        width: 100%;
        height: 100%;
    }
</style>

<body>
    <div class="big">
        <div class="div">
            盒子一
            <div class="div1">盒子二</div>
        </div>
    </div>
</body>

</html>

方式三:绝对定位 + margin:auto

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .div {
        width: 750px;
        height: 750px;
        background: violet;
        position: relative;
    }

    .div1 {
        width: 100px;
        height: 100px;
        position: absolute;
        margin: auto;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: turquoise;
    }

    .big {
        width: 100%;
        height: 100%;
    }
</style>

<body>
    <div class="big">
        <div class="div">
            盒子一
            <div class="div1">盒子二</div>
        </div>
    </div>
</body>

</html>

方式四: flex 布局

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .div {
        width: 750px;
        height: 750px;
        background: violet;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .div1 {
        width: 100px;
        height: 100px;
        background: turquoise;

    }

    .big {
        width: 100%;
        height: 100%;
    }
</style>

<body>
    <div class="big">
       
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值