【问题标题】:CSS auto expand header and footer div width to match content div width, when content goes extra wide (beyond screen)当内容变得超宽(超出屏幕)时,CSS 自动扩展页眉和页脚 div 宽度以匹配内容 div 宽度
【发布时间】:2014-08-10 00:24:54
【问题描述】:

我正在尝试构建一个带有页眉、内容和页脚的网站布局。 页眉和页脚都应扩展为 100% 宽度。他们做到了。但是当我在内容 div 中放置一个非常宽的元素时,页眉和页脚保持在我的屏幕宽度,并且不会进一步扩展以匹配内容宽度。请帮助实现这种行为。

在下面的示例中,我希望蓝色页眉和黄色页脚扩展并匹配灰色内容的宽度(在现实生活中将包含一个宽度未知的表格)

jsfiddle:http://jsfiddle.net/StormBlast/z4hegp1o/3/

HTML:

<div id="wrapper">
    <div id="header">Header</div>
    <div id="content">
        <div style="width: 2500px; background-color: gray;">
            here comes very wide table with some results and expands beyond screen borders (in width) 
            here comes very wide table with some results and expands beyond screen borders (in width) 
            here comes very wide table with some results and expands beyond screen borders (in width) 
            here comes very wide table with some results and expands beyond screen borders (in width) 
            here comes very wide table with some results and expands beyond screen borders (in width) 
        </div>
    </div>
    <div id="footer">Footer</div>
</div>

CSS:

html,
body {
    margin:0;
    padding:0;
    height:100%;
}
#wrapper {
    min-height:100%;
    position:relative;
}
#header {
    padding:10px;
    background:#5ee;
}
#content {
    padding:10px;
    padding-bottom:80px;   /* Height of the footer element */
}
#footer {
    width:100%;
    height:80px;
    position:absolute;
    bottom:0;
    left:0;
    background:#ee5;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可能想尝试使用 jQuery 进行这种大小匹配。

    使用 jQuery,您只需获取内容 div 的宽度,并在页面加载时更改页眉和页脚宽度。

        $('#header,#footer').width($('#content div').width());
    

    http://jsfiddle.net/z4hegp1o/4/

    【讨论】:

    • 谢谢。是的,它有效。如果没有其他方法可以仅使用 CSS 和 HTML 实现它,我肯定会这样做。我知道使用表格进行主布局会起作用,但它甚至比 JS 更糟糕。
    • 恐怕它不会像 JS 那样工作。我永远不会知道内容 div 中的 html 结构是什么。将在其中加载子应用程序。我将不得不遍历内容中的整个 DOM 树以找到最大宽度,然后将其设置为页眉和页脚。我可以自然地做到这一点,但现在我尝试通过纯 CSS 和 HTML 来实现它的动力更大。
    • 我改变了策略 - 我不再想调整页眉和页脚的大小。它们将始终保持在 100% 的屏幕宽度,位置:绝对;并且会在滚动时移动: $(window).scroll(function(){ $('#header, #footer').css({'left': $(this).scrollLeft()});});
    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多