使用DIV保留纵横比

发布于 2025-02-01 09:55:39 字数 1767 浏览 6 评论 0原文

tl; dr
Square类应为所有测试柜中的平方。

以下HTML包括三个测试容器。 所有测试容器都包含一个弹性盒,该弹性箱可容纳三个Divs。中间的DIV具有Square类,并且(如名称所建议的)应为所有测试柜中的平方。 Square应尽可能多地填充容器的空间,但当然不应超过它。

我已经找到了一个解决方案在这里,但它失败了在第二和第三个测试柜上。

如果可能的话,该解决方案不应包含JavaScript,并且不应在可能的情况下使用fack-Ratio,因为当前它的浏览器支持不良。

感谢任何帮助。

codepen

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  background-color: yellow;
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; height: 400px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

TL;DR
The square class should be a square in all testcases.

The following html includes three testcases.
All testcases contain a flex-box which holds three divs. The div in the middle has a square class and (as the name suggests) should be a square in all testcases. The square should fill up as much space of the container as possible, but shouldn't exceed it of course.

I already found a solution here, but it fails on the second and third testcase.

The solution shouldn't include javascript if possible and shouldn't use aspect-ratio if possible, because it has bad browser support currently.

Any help is gratefully appreciated.

Codepen

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  background-color: yellow;
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; height: 400px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱给你人给你 2025-02-08 09:55:39

除非您需要支持不弃用的IE,否则可以使用expact-ratio

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  background-color: yellow;
  aspect-ratio: 1 / 1;
  width: 100%;
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; min-height: 400px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

Unless you need to support the deprecated IE you can use aspect-ratio:

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  background-color: yellow;
  aspect-ratio: 1 / 1;
  width: 100%;
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; min-height: 400px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square">World</div>
  <div class="simple">Hello</div>
</div>

2025-02-08 09:55:39

要使您.square元素要保持正确的长宽比,而无需aptive> aptive-ratio prop,您可以使用pseudo Elements,请参阅:

另请参阅:另外,如果您不想使用网格方法,您可以制作此容器相对,并且它的子跨度元素 - 绝对,如果需要,请使用正确的定位。

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  display: grid;
  grid-template-areas: 'content';
  background-color: yellow;
  width: 100%;
}

/* place text content and square pseudo el. to same cell */
.square:before,
.square > span {
  grid-area: content;
}

.square:before {
  content: '';
  display: block;
  width: 100%;
  padding-bottom: 100%; /* make it square */
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; min-height: 400px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

To make you .square element to keep correct aspect ratio without aspect-ratio prop, you could use pseudo elements, see:

Also, if you don't want to use grid approach, you can make this container relative and it's child span element - absolute with correct positioning if needed.

.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  background-color: red;
}

.simple {
  width: 100%;
  background-color: green;
}

.square {
  display: grid;
  grid-template-areas: 'content';
  background-color: yellow;
  width: 100%;
}

/* place text content and square pseudo el. to same cell */
.square:before,
.square > span {
  grid-area: content;
}

.square:before {
  content: '';
  display: block;
  width: 100%;
  padding-bottom: 100%; /* make it square */
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; min-height: 400px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

<!-- 2. Testcase -->
<div class="container" style="width: 400px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

<!-- 3. Testcase -->
<div class="container" style="width: 200px; min-height: 200px">
  <div class="simple">Hello</div>
  <div class="square"><span>World</span></div>
  <div class="simple">Hello</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文