html - 如何让绝对 div 适合父宽度/填充?

标签 html css

我需要将定位设置为绝对定位,以便可以将 #bottom 固定在屏幕底部。我还需要使其适合容器 #panel 的宽度/填充。但是,当我将位置设置为绝对位置时,宽度刚好填满整个屏幕的宽度,我该如何停止呢?我需要 #bottom 以适应 #panel 的宽度/填充。

HTML:

<div id="panel">
    <div id="bottom">
        <div class="update"></div>
    </div>
</div>

CSS:

#panel {
    width: 21.25%;
    height: 100%;
    background-color: #0794ea;
    float: left;
    padding: 0 1.5%;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.update {
    width: 100%;
    background-color: #006699;
    text-align: center;
    height: 56px;
    color: white;
}

#bottom {
    position: absolute;
    bottom: 20px;
    width: 100%;
}

enter image description here

上面是正在发生的事情的图像。绿色是填充,蓝色是它应该适合的内容区域(深蓝色是我试图适合内容区域的实际 div (#panel))。我假设因为它绝对忽略了这一点,所以我正在寻找一种方法来解决这个问题。

fiddle :http://jsfiddle.net/qTJhW/

谢谢

最佳答案

您正在做的事情的问题是它采用包括填充在内的整个宽度并将其与填充的左侧宽度对齐。您可以使用具有相对位置的包装器来解决此问题。也不要忘记使#panel 位置相对。

你最终得到的代码是这样的:

<div id="panel">
    <div class="wrapper">
        <div id="bottom">
            <div class="update">
                a          
            </div>
        </div>    
    </div>
</div>

还有 CSS:

#panel {
    width: 21.25%;
    height: 100%;
    background-color: #0794ea;
    float: left;
    padding: 0 1.5%;
    box-sizing: border-box;
}

.update {
    width: 100%;
    background-color: #006699;
    text-align: center;
    height: 56px;
    color: white;
}

.wrapper {
    position: relative;
    height: 100%;
}

#bottom {
    position: absolute;
    bottom: 20px;
    left: 0;
    width: 100%;
    background: yellow;
}

举个例子: http://codepen.io/DanielVoogsgerd/pen/Lezjy

关于html - 如何让绝对 div 适合父宽度/填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922365/

相关文章:

html - 禁用 <a> 的突出显示

javascript - 创建粘性页脚

javascript - 如何一次删除一个类元素?

javascript - 无法调用未定义node.js的方法 'forEach'

html - 使用 html/css 将图像定位在菜单栏旁边

jquery - 如何从 html5 代码更改输入模式语言

jQuery 从类中更改单个元素

html - css 在固定标题上方显示内容

html - checked 和 selected 的哪些值是错误的?

javascript - 单击功能后隐藏 jquery 菜单