我有一个现有页面,有两个单独的元素,每个元素的高度由它们的内容设置(此处为 CodePen)。如何确保两者在所有屏幕宽度下都具有相同的高度?问题是,我不能使用 CSS flex 或 grid :-/ 相反,这两个元素不能在同一个 flex 容器或 grid 容器中。我这么说是因为已经构建了一个大页面,具有某种 HTML 结构,不允许这些元素位于相同的 flex/grid 容器中(请参见此处的示例页面)。
我愿意接受一些黑客解决方案。
如果这可能有帮助,我也对 JavaScript 持开放态度。
选项
我想到了一个 JavaScript 解决方案来读取元素 2 的高度,并将元素 1 的高度设置为该值。但是如果用户调整他的浏览器大小会发生什么——这两个元素是否保证保持相同的高度?如果用户在移动设备上并旋转显示器会发生什么——这两个元素是否保证保持相同的高度?
谢谢。
我的 CodePen 代码:
HTML
<div class='a'>Element A</div>
<div class='b'>Element B: Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. Here is more text. </div>
CSS
.a {
display: inline-block;
background-color: yellow;
width: 45%;
border: 1px solid black;
}
.b {
display: inline-block;
background-color: red;
width: 45%;
border: 1px solid black;
}
慕田峪4524236
慕村225694
相关分类