← 返回完整题库
CSS困难

下面这段代码中,class为content的元素,实际高度是100px吗?

<!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>Static Template</title>

    <style>
      .parent {
        display: flex;
        flex-direction: column;
        height: 600px;
        width: 300px;
        background: yellow;
      }
      div {
        width: 100%;
      }
      .header {
        height: 200px;
        background: red;
      }
      .content {
        height: 100%;
        background: blue;
      }
      .footer {
        height: 200px;
        background: black;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="header"></div>
      <div class="content"></div>
      <div class="footer"></div>
    </div>
  </body>
</html>

参考答案

不是

首先,content元素的 height 设置为 “100%”,在父级的高度为固定值时,直接继承该高度,也就是600px。

但父级设置了 display:flex ,在高度固定的前提下,子元素的高度会按比例进行缩放,所以content元素最后的高度应该是 600 * (600/(200+600+200)) = 360px

本题来自 计算机知识面试题库,共 921 道题。