当前位置:  开发笔记 > 前端 > 正文

将min-height设置为等于width

如何解决《将min-height设置为等于width》经验,为你挑选了1个好方法。

我有一个流体宽度div,它在另一个块级元素内.

HTML

CSS

div {
    width:50%;
    min-height: /*equal to width */;
}

我希望最小高度设置等于宽度,这样它就是方形,除非内容太多.此时,它应该垂直扩展.这可以用CSS实现吗?

我尝试过的:

使用百分比填充或边距,但这只设置高度,而不是max - height并将内容向下推.

Turnip.. 8

因为填充是相对于元素宽度的,所以您可以使用伪元素通过使用padding-top: 100%;以下方法强制最小高度:

div {
  float: left;
  margin: 10px;
  width: 25%;
  background: lightGreen;
  position: relative;
}

div:before {
  content: "";
  display: block;
  padding-top: 100%;
  float: left;
}
div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content.
div with content. div with content.



1> Turnip..:

因为填充是相对于元素宽度的,所以您可以使用伪元素通过使用padding-top: 100%;以下方法强制最小高度:

div {
  float: left;
  margin: 10px;
  width: 25%;
  background: lightGreen;
  position: relative;
}

div:before {
  content: "";
  display: block;
  padding-top: 100%;
  float: left;
}
div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content.
div with content. div with content.
推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有