css - 将块 B 的内容扩展到块 A
问题描述
我有一个类red
,宽度是60%
,我想在这个块中放入另一个以green
宽度 命名的类70%
。
我的问题是,在显示中,背景颜色比背景颜色green
小red
,我不明白为什么?!
.container {
margin: 0;
padding: 0;
width: 100%;
height: 100px;
}
.red {
background-color: red;
width: 60%;
height: 50%;
}
.green {
background-color: green;
width: 70%;
height: 50%;
}
<div class="container">
<div class="red">
<div class="green">
</div>
</div>
</div>
解决方案
理解问题
green class
是孩子的,red class
这意味着宽度green
将是70 %其中red
是60%container
让我们做一些数学 -
容器- 100% -假设宽度 x
红色- 容器的 60% - 0.6x
绿色-70% 红色 - 0.42x
我们都知道
0.42x < 0.6x
解决方案
使red
和green
班二分开div
。
.container {
margin: 0;
padding: 0;
width: 100%;
height: 100px;
}
.red {
background-color: red;
width: 60%;
height: 50%;
}
.green {
background-color: green;
width: 70%;
height: 50%;
}
<div class="container">
<div class="red"></div>
<div class="green"></div>
</div>
推荐阅读
- java - Camunda:如何将变量从父进程传递到子进程。(由事件开始)
- c# - 当我没有创建类型时,C# 如何访问这个通用列表?
- excel - Excel VBA - 使用通配符检查文件是否存在并打开文件
- python - Python/Regex - 匹配两个字符之间的字符,匹配字符之前或之后的任何内容
- php - php 中 SQL SELECT 查询的返回类型是什么,是数组、对象还是什么?
- python - 在启动时自动启动python脚本?
- ios - 音乐应用程序现在在动画中播放视图控制器幻灯片
- selenium - 同时使用 .isDisplayed 和 .isEnabled?
- amazon-web-services - 在 RStudio AWS AMI 上安装 RQuantLib
- java - JTabbedPane, addChangeListener() 只在最后一个选项卡上查询数据库