【问题标题】:Make div stretch to height of content, not height of page使 div 拉伸到内容的高度,而不是页面的高度
【发布时间】:2012-12-20 22:02:47
【问题描述】:

我有一个高度为 100% 的 div:

height: 100%;

另外我添加了一个绝对位置:

position: absolute;

当我添加绝对位置属性时,div 会拉伸到页面的高度,而不是像以前一样在其中的内容的高度。我怎样才能使 div 延伸到内容的高度,而不是页面的高度?

如果有帮助的话,这里是完整的代码:

.blah {
    height: 100%;
    width: 200px;
    position: absolute; 
    border-radius: 15px 15px 15px 15px; 
    -moz-border-radius: 15px 15px 15px 15px; 
    -webkit-border-radius: 15px 15px 15px 15px; 
    border: 0px solid .800000;
    padding: 0.5em;
    background-color: #ffffff;
    -webkit-box-shadow:  1px 1px 1px 1px rgba(0, 0, 0, 0.3);
    box-shadow:  1px 1px 1px 1px rgba(0, 0, 0, 0.3);
}

【问题讨论】:

  • 去掉height:100%,默认取内容高度

标签: html css


【解决方案1】:

只是不要使用height:100%,高度会根据内容自动调整。

您描述的行为源于这样一个事实,即父元素 - 根据该元素确定 100% - 一旦您分配 body 元素将成为 position:absolute; (假设您没有定位该 DIV 的其他父元素)。

【讨论】:

  • 哇,我真傻!那一定是因为我粘贴了另一个项目的代码。
猜你喜欢
  • 2012-08-16
  • 2010-10-17
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2011-01-24
  • 2020-12-05
  • 2012-09-18
  • 1970-01-01
相关资源
最近更新 更多