使用 CSS 平均分配高度

Equally distributing height with CSS(使用 CSS 平均分配高度)
本文介绍了使用 CSS 平均分配高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常棘手的 HTML 问题,我不确定是否有基于 CSS 的解决方案.基本上我有一个三列网格.第一列和第三列可以包含可变数量的行.第二列总是正好有一行.每行都有一个最小高度.

I have an HTML problem that is pretty tricky and I'm not sure there is a CSS-based solution for it. Basically I have a three column grid. The first and third columns can contain a variable number of rows in them. The second column always has exactly one row. Each row has a minimum height.

因此,行数最多的列会将高度设置为最小高度的所有行.其他列中的行应在高度上等量扩展以占用剩余空间.

So the column with the most rows will have all the rows with height set to the minimum height. The rows in the other columns should expand equally in height to take up the remaining space.

假设第一列有三行,每行 50 像素高.第二列必须有一行 150 像素高.如果第三列有 2 行,则每行必须为 75px 高.下面是一些图片来进一步说明我所追求的.

Say that column one has three rows, each 50px tall. Column two must have one row 150px tall. If column three has 2 rows they must each be 75px tall. Below are some pictures to further illustrate what I'm after.

这甚至可以通过 CSS 实现吗?

Is this even possible with CSS?

推荐答案

根据一些人的建议,我最终使用了一些简单的 javascript 来完成这项工作:

Per recommendations by a few folks, I ended up using some simple javascript to get this done:

$(document).ready(function() {
   var c1Rows = $(".col1 .row").length;
   var c3Rows = $(".col3 .row").length;

   var minHeight = 50;
   var max = Math.max(c1Rows, c3Rows);
   var totalHeight = max * minHeight;

   $(".col1 .row").css("height", totalHeight / c1Rows);
   $(".col2 .row").css("height", totalHeight);
   $(".col3 .row").css("height", totalHeight / c3Rows);
});

这篇关于使用 CSS 平均分配高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to remove this error quot;Response must contain an array at quot; . quot;.quot; while making dropdown(如何删除此错误quot;响应必须在quot;处包含数组。创建下拉菜单时(Q;))
Why is it necessary to use `document.createElementNS` when adding `svg` tags to an HTML document via JS?(为什么在通过JS为一个HTML文档添加`svg`标签时,需要使用`Document.createElementNS`?)
wkhtmltopdf print-media-type uses @media print ONLY and ignores the rest(Wkhtmltopdf print-media-type仅使用@media print,而忽略其余内容)
price depend on selection of radio input(价格取决于无线电输入的选择)
calculate price depend on selection without button(根据没有按钮的选择计算价格)
What should I consider before minifying HTML?(在缩小HTML之前,我应该考虑什么?)