html - CSS height auto 在相对 DIV 中不起作用

标签 html css twitter-bootstrap-3 css-position

我有这段代码可以将 absolute div 显示为 relative div

html:

<section>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="relative">
                    <div class="absolute">Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.</div>

                </div>
            </div>
            <div class="col-md-12">
               normal text added
            </div>    
        </div>
    </div>
</section>

CSS:

.relative {
    position: relative;
    height:auto;
    background-color:#e1e1e1;
}
.absolute {
    position: absolute;
    bottom: 0;
    left: 0;
    background-color: white;
    width: auto;
    background-color:#f5f5f5;
    margin:0 15px;
}

实际上,当我为 relative div 设置 height:auto 时,这个 div 没有显示。如果我设置任何 height 值即:height:150px; relative div 它有效并显示为 true。我该如何解决这个问题?

演示 here

最佳答案

你不能用 css 做到这一点,需要用 position:relative 在你的外部 div 上设置高度为你的内心position:absolute以显示。

这是因为当你设置一个 <div>为position:absolute,外层容器不再有其宽高的概念,不会容纳其占用的空间。

关于html - CSS height auto 在相对 DIV 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098129/

相关文章:

html - 内部 DIV 未扩展到 100% 高度

jquery - 对 ScrollMagic on Leave 事件非常困惑

html - 提供一个 html 文件和 css 文件,是否有任何 IDE 可以找到并删除未使用的 css 类?

javascript - 多行上彼此相邻的元素没有边距

html - 如何阻止固定侧边栏位于页脚顶部?

css - Bootstrap面板标题,左右div,水平对齐

javascript - Bootstrap-select 插件 : how to avoid flickering

javascript - 网页代码的执行顺序是什么?我们如何更改顺序?

css - 具有 html/css 背景的 Div

HTML/CSS : scroll div without setting position to fixed or absolute