html - DIV 缩放后变为矩形

标签 html css

我有一个名为 square 的 DIV,当宽度超过 500px 时,它就像一个正方形。

.squareholder {
display: flex;
width: 80%;
justify-content: center;
margin-left: auto;
margin-right: auto;
}

.square {
width: 12vw;
height: 12vw;
margin-left: 1vw;
margin-right: 1vw;
background-color: #ff0000;
border-radius: 2vh;
align-content: center;
}

@media only screen and (max-width: 500px) {
.square {
width: 30vh;
height: 30vh;
margin-left: 1vw;
margin-right: 1vw;
background-color: #ff0000;
border-radius: 2vh;
align-content: center;
}
}
<div class ="squareholder">
<div class ="square"><h1>Text</h1></div>
</div>

正常查看时,DIV 是一个正方形。调整窗口大小时,它会拉伸(stretch)。

知道为什么高度和宽度属性保持不变吗?

我知道这与 flex 有关,但如果我连续添加三个;

.squareholder {
display: flex;
width: 80%;
justify-content: center;
margin-left: auto;
margin-right: auto;
}

.square {
width: 12vw;
height: 12vw;
margin-left: 1vw;
margin-right: 1vw;
background-color: #ff0000;
border-radius: 2vh;
align-content: center;
}

@media only screen and (max-width: 500px) {
.square {
width: 30vh;
height: 30vh;
margin-left: 1vw;
margin-right: 1vw;
background-color: #ff0000;
border-radius: 2vh;
align-content: center;
}
}
<div class ="squareholder">
<div class ="square"><h1>Text</h1></div>
<div class ="square"><h1>Text1</h1></div>
<div class ="square"><h1>Text2</h1></div>
</div>

所有的盒子都排成一排。我需要它们仍然排成一排,只是为了在变小时保持正方形。我认为这与正在使用的 display: flex; 有关,但我不确定还有什么其他解决方案会将其保留为 [] [] []

而不是;

[] [] []

调整浏览器宽度时。

提前致谢:)

最佳答案

因为对于小屏幕来说,在高度不变的情况下,宽度不断缩小。在您的媒体查询中使用 vh 试图适应这一点,它会创建一个矩形。

如果你想防止红色方 block 在较小的屏幕上变成矩形,我会让你的 css 统一。

在这里 fiddle :https://jsfiddle.net/vxgsq15u/

关于html - DIV 缩放后变为矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842739/

相关文章:

javascript - Javascript/JQuery 中的多个 DIV 碰撞检测

javascript - HTML5 地理定位不等待用户输入

css - Safari/Chrome 空白 :nowrap text over run

html - CSS:背景图像/颜色和容器不会在移动设备上拉伸(stretch)全宽

css - 是否可以在 iframe 中只显示某个 div?

html - 垂直填充空间,显示: table-cell (IE8/9)

javascript - 导航器地理定位 getCurrentPosition 不起作用

javascript - 无论设置为宽度 :100%,Div 都会增长到超过站点宽度

javascript - 检查具有相同类的所有元素是否被隐藏

html - 我的 CSS 不响应 ID 和类标识符