CSS 高度 = 宽度?

标签 css height width

在 CSS 中执行此操作的简单方法是什么?

selector {
    width: 75%;
    height: width; 
}

我尝试将高度设置为各种属性,但它们似乎没有达到我想要的效果。如何将一个值设置为另一个值?

最佳答案

Padding-top 百分比基于宽度,因此使用纯 CSS 很容易计算。

.wrapper {
  width: 50%;
  /* whatever width you want */
  display: inline-block;
  position: relative;
}
.wrapper:after {
  padding-top: 100%;
  /* 1:1 ratio, set from padding top being 100% */
  /* If you wanted one half as high, use 50% */
  
  display: block;
  content: '';
}
.main {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  
  /* fill parent */
  background-color: red;
  
  /* let's see it! */
  color: white;
}
<div class="wrapper">
  <div class="main">
    This is a sample 1:1 responsive DIV. 
  </div>
</div>

关于CSS 高度 = 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29617966/

相关文章:

javascript - 如何在不使用 void 的情况下重新启动 CSS 动画?

css - 设置不同高度的div?

javascript - 单击容器内部时下拉菜单关闭

运行时的 WPF 高度

html - 当 min-height 为 100% 时如何预定义 div 的高度

javascript - 计算span的scrollWidth,offsetWidth,clientWidth的值

html - 我将 div 设置为 100%,将表格的宽度设置为 1000px,但背景没有覆盖页面

html - 如何在文本旁边获取图像?

jquery - 使用 jQuery width() 和 height() 方法

html - CSS Transition Accordion radio 无法使用高度 : auto or max-height (Edited)