制作一个
当宽度根据百分比动态变化时为正方形

发布于 2024-08-29 05:45:08 字数 230 浏览 22 评论 0原文

我正在开发一个网络应用程序,它将根据用户对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日久见人心 2024-09-05 05:45:08

有 3 种主要技术可以保持响应式元素的纵横比。
第一个是迄今为止最好的,适用于各种用例:aspect-ratio CSS 属性。其他 2 个可能适合其他用例:使用 padding 或 vw 单位:
(有关响应式正方形网格的完整解决方案,您可以查看此答案

aspect-ratio 属性

这直接指定元素的长宽比。它可以基于元素的宽度或高度。 (请参阅 MDN
这是一个例子:

body {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

div {
  width: 23%;
  margin-bottom: 2%;
  background: gold;
  aspect-ratio: 1/1;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

使用 vw 单位

您可以使用 vw 单位使您的元素成为方形且响应式(MDN 上的视口单位)。
1vw = 视口宽度的 1%,因此您可以根据视口宽度设置元素的高度(或使用 vh 单位的高度).
4x4 网格示例:

body{
  margin:0;
  display:flex;
  flex-wrap:wrap;
  justify-content:space-around;
}

div{
    width:23vw; height:23vw;
    margin:1vw 0;
    background:gold;  
}
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>

使用 vh 单位根据视口高度调整元素大小可以实现相同的行为。


使用填充

填充是根据容器的宽度计算的,因此您可以使用它来设置块的高度取决于其宽度。
4x4 网格示例:

.wrap {
    width:80%;
    margin:0 auto;
}
.wrap div {
    width:23%;
    padding-bottom:23%;
    margin:1%;
    float:left;
    background:gold;
}
<div class="wrap">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

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 :

body {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

div {
  width: 23%;
  margin-bottom: 2%;
  background: gold;
  aspect-ratio: 1/1;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

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 with vh units).
Example with a 4x4 grid :

body{
  margin:0;
  display:flex;
  flex-wrap:wrap;
  justify-content:space-around;
}

div{
    width:23vw; height:23vw;
    margin:1vw 0;
    background:gold;  
}
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>

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 :

.wrap {
    width:80%;
    margin:0 auto;
}
.wrap div {
    width:23%;
    padding-bottom:23%;
    margin:1%;
    float:left;
    background:gold;
}
<div class="wrap">
    <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>
</div>

倾听心声的旋律 2024-09-05 05:45:08

使用 CSS 将 div 变成正方形非常容易。您设置一个宽度,假设为 50%。然后添加相同值的 padding-bottom:

div {
width: 50%;
padding-bottom: 50%;
}

无论何时调整窗口大小,它都会保持方形。

您可以使用任何所需的边比来执行此操作,如果您希望框为 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:

div {
width: 50%;
padding-bottom: 50%;
}

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%

梦晓ヶ微光ヅ倾城 2024-09-05 05:45:08

这是未经测试的,我不知道如何仅在 CSS 中执行此操作,我会使用 jQuery。

$('div').height($('div').width());

This is un-tested, I do not know of how to do this in CSS only, I would use jQuery.

$('div').height($('div').width());
九局 2024-09-05 05:45:08

上述解决方案不保留面积 - 这个更好

        //@param {jQuery} div 
        function makeSquare(div){
            //make it square !
            var oldDimens = {
                h : div.height(),
                w : div.width()
            };
            var area = oldDimens.h * oldDimens.w;
            var l = Math.sqrt(area);
            div.height(l).width(l);
        }

The above solution doesn't preserve area - this one is better

        //@param {jQuery} div 
        function makeSquare(div){
            //make it square !
            var oldDimens = {
                h : div.height(),
                w : div.width()
            };
            var area = oldDimens.h * oldDimens.w;
            var l = Math.sqrt(area);
            div.height(l).width(l);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文