小编Luk*_*uke的帖子

在纯css中设置相对于其父高度的子宽度

由于我是网络编程的新手,我还没有在 css 方面获得太多经验。目前我正在构建一个带有可调整大小的标头的 Web 应用程序。因此我想知道是否可以设置子 div 的宽度相对于其父 div 的高度。

我知道如何处理 javascript 代码中的问题,但我更喜欢纯 css 解决方案。

css 代码如下所示:

.parent {
   position: relative;
   height: 200px;
   width: 600px;
 }
 .parent .resized {
   height: 100px;
 }

 .child {
   position: absolute;
   height: 20%;
   width: 10% [of parent height];
 } 
Run Code Online (Sandbox Code Playgroud)

HTML 代码如下所示:

<div class="parent">
  <div class="child"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

目前孩子的宽度保持不变,因为父母的宽度保持不变。当父高度调整大小时,我希望孩子的宽度变小。

提前致谢。

html css

7
推荐指数
1
解决办法
5827
查看次数

标签 统计

css ×1

html ×1