html怎么动态计算高度,html-在这种情况下如何计算高度?

对于第一种情况,它是关于flexBox的默认对齐方式,即stretch.因此所有元素都将拉伸以适合container1的高度,因此菜单的高度与内容的高度相同.换句话说,最高的元素将定义高度,而另一个将拉伸以适合该高度.@H_301_2@

更改对齐方式,您将不再有此行为:@H_301_2@

@H_301_2@html,body {

height: 100%;

}

.app {

display: flex;

align-items:flex-start; /*anything different from stretch*/

}

.menu {

background-color: red;

width: 100px;

height:100%;

}

.content {

flex: 1;

border: 1px solid blue;

}

content
content
content
content
content
content
content
content
content
content
content
content
content
content

通过添加height:100%,您现在可以明确地将高度应用到元素(不再应用拉伸),但这将无法自动设置值(内容的高度),因为父元素没有指定任何高度2@H_301_2@

@H_301_2@html,body {

height: 100%;

}

.app {

display: flex;

}

.menu {

background-color: red;

width: 100px;

height:100%;

}

.content {

flex: 1;

border: 1px solid blue;

}

content
content
content
content
content
content
content
content
content
content
content
content
content
content

1 If the cross size property of the flex item computes to auto,and neither of the cross-axis margins are auto,the flex item is stretched. Its used value is the length necessary to make the cross size of the item’s margin Box as close to the same size as the line as possible,while still respecting the constraints imposed by min-height/min-width/max-height/max-width.07001@H_301_2@

在这种情况下,我们只有一行,因此它与整个容器相同.@H_301_2@

@H_301_2@2 Specifies a percentage height. The percentage is calculated with respect to the height of the generated Box’s containing block. If the height of the containing block is not specified explicitly (i.e.,it depends on content height),and this element is not absolutely positioned,the value computes to ‘auto’.07002 @H_301_2@

为应用程序增加一个高度,您将看到正在发生的事情:@H_301_2@

@H_301_2@html,body {

height: 100%;

}

.app {

display: flex;

height:80%;

}

.menu {

background-color: red;

width: 100px;

height:100%;

}

.content {

flex: 1;

border: 1px solid blue;

}

content
content
content
content
content
content
content
content
content
content
content
content
content
content

更新@H_301_2@

根据评论,这里是一个特殊情况,其中百分比高度在父元素中未指定高度的情况下工作.@H_301_2@

@H_301_2@html,body {

height: 100%;

}

.app {

display: flex;

height: 80%;

}

.menu {

background-color: red;

width: 100px;

}

.container{

flex: 1;

background:green;

}

.test {

height: 80%;

border: 2px solid blue;

}

percentage height is working here!! why??

正如我在a previous answer中所解释的,.container的高度没有明确设置,但是我们没有任何循环依赖关系,因为.container的高度是由拉伸行为而不是其内容定义的,因此浏览器可以首先设置.container使用Stretch的高度,然后解析.test元素的百分比高度.@H_301_2@

没有拉伸效果,浏览器需要首先根据内容找到高度,并且内容的高度取决于容器,因此我们具有循环依赖性,这将使百分比高度无法自动显示,以便能够定义容器的高度.@H_301_2@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值