使用DIV保留纵横比
tl; drSquare
类应为所有测试柜中的平方。
以下HTML包括三个测试容器。 所有测试容器都包含一个弹性盒,该弹性箱可容纳三个Divs。中间的DIV具有Square
类,并且(如名称所建议的)应为所有测试柜中的平方。 Square
应尽可能多地填充容器的空间,但当然不应超过它。
我已经找到了一个解决方案在这里,但它失败了在第二和第三个测试柜上。
如果可能的话,该解决方案不应包含JavaScript,并且不应在可能的情况下使用fack-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;
}
<!-- 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.
.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您需要支持不弃用的IE,否则可以使用
expact-ratio
:Unless you need to support the deprecated IE you can use
aspect-ratio
:要使您
.square
元素要保持正确的长宽比,而无需aptive> aptive-ratio
prop,您可以使用pseudo Elements
,请参阅:另请参阅:另外,如果您不想使用
网格
方法,您可以制作此容器相对
,并且它的子跨度元素 -绝对
,如果需要,请使用正确的定位。To make you
.square
element to keep correct aspect ratio withoutaspect-ratio
prop, you could usepseudo elements
, see:Also, if you don't want to use
grid
approach, you can make this containerrelative
and it's child span element -absolute
with correct positioning if needed.