为什么最小高度在父div上不起作用?

时间:2018-06-28 13:21:49

标签: html css

我有以下html / css。尽管最小高度,但父div不会根据子div调整大小。我检查了有关类似问题的其他一些文章,看来问题出在位置绝对元素不是页面的正常“流”。但是,我似乎无法找到解决此问题的合适方法。我仍然需要绝对职位。有什么想法吗?

.container {
  min-height: 200px;
  border: 1px solid black;
  position: relative;
  overflow: hidden;
}

.content {
  border: 1px solid red;
  position: absolute;
  top: 4px;
  width: 50%;
  height: 900px;
}
<div class="container">
  <div class="content">
  Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

Where can I get some?
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

由于您已将子级position的{​​{1}}设置为div,因此在计算父级div的高度时不会考虑它。相反,您可以使用absoluteposition : static

margin-top : 4px
.container {
  min-height: 200px;
  border: 1px solid black;
  position: relative;
  overflow: hidden;
}

.content {
  border: 1px solid red;
  position: static;
  margin-top: 4px;
  width: 50%;
  height: 900px;
}

答案 1 :(得分:0)

如果将孩子的位置设置为绝对位置,则不能安排父母的身高。如果您确实希望将孩子的位置保持在绝对位置,则应该使用固定的高度。

您可以使用JS设置固定高度。