html - 仅使用 CSS 固定高度的方形 div

标签 html css height width

一切都在标题中!

如何仅使用 CSS 使我的 div 的宽度等于其固定高度?

HTML代码

<div class="container">
    <div class="square"></div>
</div>

CSS 代码
.square{
    height:80%;
}

我知道我可以使用相反的方法(固定宽度)
.square{
   width : 20%;
   padding-top:20%
}

因为 padding-top 是相对于容器的宽度。

我也知道我可以使用简单的 JQuery 但不想使用 javascript。

谢谢,

最佳答案

使用大众单位:

.square {
  background: #000;
  width: 50vw;
  height: 50vw;
}
<div class="square"></div>

关于html - 仅使用 CSS 固定高度的方形 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920754/

相关文章:

javascript - 单击按钮模拟单击隐藏的下拉列表

css - 使用粘性页脚,无法将主要内容的背景拉伸(stretch)到页脚

javascript - 视差/背景附件 :fixed not working in some browsers

html - 左侧元素的固定边距

html - 从所有浏览器的 HTML 按钮中删除文本?

jquery - 通过jQuery设置伪元素高度

css - 处理固定填充和灵活宽度/高度的最佳方法

css - 容器中的并排 div 填充高度

html - 如何在元素底部使用::before 选择器?

php - 第 6 行的可捕获 fatal error : Object of class mysqli could not be converted to string in/home/cabox/workspace/writing/login. php