<!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

在线demo可访问查看: https://codesandbox.io/s/strange-curran-3kci7i?file=/index.html

本题目答案由“前端面试题宝典”整理,PC端可访问 https://fe.ecool.fun/