gpt4 book ai didi

HTML 布局 : How to get arbitrary amounts of floating divs to have the same height?

转载 作者:行者123 更新时间:2023-11-28 18:00:40 24 4
gpt4 key购买 nike

我想使用包含任意数量文本的可变数量的“框”创建网页设计,并使设计看起来有点像表格。但是,如果屏幕尺寸较小(即手机),我希望单元格在下一行“继续”。

我的想法是通过使用带 float:left 的 div 创建单元格,但是各种 div 的高度不同,因此使整个东西看起来很丑。

似乎有很多人使用这种基于 div 的概念来创建固定数量的列,但我该如何让它解决这个特定问题?

最佳答案

我只是使用 javascript 遍历相关 div 列表,获取最高的 div 的高度。然后我会通过并明确地确定所有这些人的高度。

考虑以下几点:

CSS:

#container
{
display: inline-block;
width: 25%; /* for the sake of the example only. serves no purpose other than forcing two rows of 3 items, when viewed on a 1366px wide monitor */
}
.floatLeft
{
float: left;
border: solid 2px red; /* also only for the sake of the example */
}

HTML:

<body>
<div id='container'>
<div class='floatLeft'>
This has 1 line
</div>
<div class='floatLeft'>
This has 1 line<br>
This has 1 line
</div>
<div class='floatLeft'>
This has 1 line<br>
This has 1 line<br>
This has 1 line
</div>
<div class='floatLeft'>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line
</div>
<div class='floatLeft'>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line
</div>
<div class='floatLeft'>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line<br>
This has 1 line
</div>
</div>
</body>

最后,“魔法”发生的地方:JavaScript:

function setHeights()
{
var divList = document.getElementsByClassName('floatLeft');
var i, n = divList.length;
var maxHeight = 0;
for (i=0; i<n; i++)
{
if (divList[i].clientHeight > maxHeight)
maxHeight = divList[i].clientHeight;
}
for (i=0; i<n; i++)
divList[i].style.height = maxHeight + "px";
}

关于HTML 布局 : How to get arbitrary amounts of floating divs to have the same height?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com