制作一个当宽度根据百分比动态变化时为正方形
我正在开发一个网络应用程序,它将根据用户对 N 的选择生成 NxN 网格。我希望网格的总宽度是相对的(即 100% 的可用空间),以便用户可以在各种纸张尺寸上进行打印。
我可以轻松地按 %(即:100%/N)计算网格中正方形的宽度,但在计算高度时遇到问题。网页的高度总是无限的,除非我人为地限制它,就像我说的,我不想这样做。
当网格的高度和宽度约束是动态的而不是正方形时,如何使网格中的正方形成为正方形而不是矩形?
I am working on a web app that will generate an NxN grid based on the user's selection of N. I want the total width of the grid to be relative (ie 100% of the available space) so that users can print on various paper sizes.
I can easily calculate the width of the squares in the grid by % (ie: 100%/N), but I am having issues calculating the height. The height of a web page is always going to be infinite unless I artificially limit it which, like I said, I don't want to do.
How can I make the squares in my grid be square versus rectangular when the height and width constraints of my grid are dynamic and not square?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有 3 种主要技术可以保持响应式元素的纵横比。
第一个是迄今为止最好的,适用于各种用例:
aspect-ratio
CSS 属性。其他 2 个可能适合其他用例:使用 padding 或 vw 单位:(有关响应式正方形网格的完整解决方案,您可以查看此答案)
aspect-ratio 属性
这直接指定元素的长宽比。它可以基于元素的宽度或高度。 (请参阅 MDN)
这是一个例子:
使用 vw 单位
您可以使用
vw
单位使您的元素成为方形且响应式(MDN 上的视口单位)。1vw = 视口宽度的 1%
,因此您可以根据视口宽度设置元素的高度(或使用vh
单位的高度).4x4 网格示例:
使用
vh
单位根据视口高度调整元素大小可以实现相同的行为。使用填充
填充是根据容器的宽度计算的,因此您可以使用它来设置块的高度取决于其宽度。
4x4 网格示例:
There are 3 main techniques to keep the aspect ratio of a responsive element.
The first one is by far the best and will work for a variety of use cases : the
aspect-ratio
CSS property. The 2 others may suit other use cases : using padding or vw units :(for a complete solution for a responsive grid of squares, you can see this answer)
The aspect-ratio property
This specifies directly the aspect ratio of and element. It can be based on the width or height of the element. (see MDN)
Here is an example :
Using vw units
You can use
vw
units to make your elements square and responsive (viewport units on MDN).1vw = 1% of viewport width
so you can set the height of the elements according to the width of the viewport (or height withvh
units).Example with a 4x4 grid :
The same behaviour can be achieved sizing the element according to the viewport height using
vh
units.Using padding
Padding is calculated according to the container's width so you can use it to set the height of block according to its width.
Example with a 4x4 grid :
使用 CSS 将 div 变成正方形非常容易。您设置一个宽度,假设为 50%。然后添加相同值的 padding-bottom:
无论何时调整窗口大小,它都会保持方形。
。
。
您可以使用任何所需的边比来执行此操作,如果您希望框为 16:9,您可以计算:
9/16= 0.56
,然后乘以元素的宽度(在本例中为 50%(=0.5)) ,所以:
9/16*0.5= 0.28 = 28%
To make a div a square is pretty easy with CSS. You set a width, let's say 50%. Then you add a padding-bottom of the same value:
and it will stay square whenever you resize the window.
.
.
You can do this with any side ratio you want, if you want the box to be 16:9 you calculate:
9/16= 0.56
which you then multiply by the width of your element (in this case 50%(=0.5)), so:
9/16*0.5= 0.28 = 28%
这是未经测试的,我不知道如何仅在 CSS 中执行此操作,我会使用 jQuery。
This is un-tested, I do not know of how to do this in CSS only, I would use jQuery.
上述解决方案不保留面积 - 这个更好
The above solution doesn't preserve area - this one is better