用高度自动父级将高度除以百分比

编程入门 行业动态 更新时间:2024-10-26 16:32:18
本文介绍了用高度自动父级将高度除以百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想根据屏幕高度在所有div上使用相对(百分比)高度。问题是我正在使用自动高度容器元素。有什么办法可以在div上使用相对大小,而在我的父div上仍然具有自动高度。如果将容器的高度设置为100%,一切都会按预期进行,但是我需要根据其内容来扩展它。

I'd like to use relative (in percentage) heights on all my divs based to the height of the screen. Problem is that I'm using an auto-height container element. Is there any way to use relative sizes on divs and still have an automatic height on my parent div. Everything works as expected if i'll set the height of the container to 100%, but i'll need it to expand by it's content.

这基本上就是我现在已经设置好了:

This is basically how i've set things up now:

<html> <head> <style> body, html { height: 100%; width: 100%; } .container { height: auto; width: 100%; } .element1 { height: 15%; width: 80%; } .element2 { height: 25%; width: 80%; } </style> </head> <body> <div class="container"> <div class="element1">Some text here</div> <div class="element2">Some stuff here</div> </div> </body>

我们将不胜感激!

推荐答案

您不能。高度值为 auto 的父级表示父级将扩展以包含其所有子级-因此,容器的高度取决于其子级的高度。

You cannot. A parent with a height value of auto means the parent will extend to contain all its children - therefore the container's height is dependent on its children's height.

如果孩子的身高相对于父母高,则他们的身高取决于父母的身高。

If the children have a height relative to their parent, their height is dependent on the parent's height.

此循环依赖关系使您无法做你描述的是什么。

This circular dependency prevents you from doing what you describe.

更多推荐

用高度自动父级将高度除以百分比

本文发布于:2023-10-07 23:41:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高度   百分比   父级将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!