css - 使位置 :fixed DIV fit its parent container without javascript

标签 css

这是代码。我希望 DIV.fixed-nav (position:fixed) 完全适合其父级 DIV.container,其宽度可能会改变。是否有针对此的纯 CSS 解决方案?

CSS:

body {
    margin: 0;
    padding: 0;
}

.container {
    border: 1px solid #000000;
    margin: 0 auto;
    max-width: 600px;
    min-width: 400px;
}

.fixed-nav {
    background-color: red;
    height: 20px;
    position: fixed;
    width: 100%;
    top: 0;
    z-index: 99;
}

.content {
    background-color: green;
    height: 100px;
    margin-top: 20px;
}

HTML:

<div class="container">
    <div class="fixed-nav">
    </div>
    <div class="content">
    </div>
</div>

请检查DEMO .

最佳答案

fixed 的问题是它总是相对于浏览器窗口。因此,如果您在固定容器上设置 100% 高度,它将是浏览器窗口的 100%。

我能想到的唯一方法是使用 jQuery。或者,如果您不需要固定菜单并且它可以是绝对的,那么高度 100% 就可以了。

关于css - 使位置 :fixed DIV fit its parent container without javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954181/

相关文章:

css - HTML/CSS 问题

html - Css 目标切换 - 初始设置为可见

css - 在不更改 HTML 的情况下将当前 float 的 div 对齐到底部

jquery - ionic 范围和徽章重叠

html - 如何以特殊格式、css、html 设计段落的某些部分

css - 图片链接无法点击

html - Bootstrap 导航栏 CSS 问题

css - Bootstrap 4 - 小屏幕卡溢出

iphone - 如何修复移动设备(Wordpress 主题)的 "responsive-design mode"问题?

html - 如何避免 ReST 表中的水平滚动条?